为什么 c/c++ 浮点类型的命名如此奇怪?

发布于 2024-07-11 03:15:35 字数 402 浏览 16 评论 0原文

C++ 提供三种浮点类型:float、double 和 long double。 我很少在代码中使用浮点,但是当我这样做时,我总是会被无害的行上的警告所困扰,例如

float PiForSquares = 4.0;

The Problem is that theliteral 4.0 is a double, not a float - 这很烦人。

对于整数类型,我们有短整型、整型和长整型,这非常简单。 为什么C不只有短浮点数、浮点数和长浮点数? 而“双”到底从何而来?

编辑:浮点类型之间的关系似乎与整数之间的关系类似。 double 必须至少与 float 一样大,而 long double 至少与 double 一样大。 不做任何其他精度/范围保证。

C++ offers three floating point types: float, double, and long double. I infrequently use floating-point in my code, but when I do, I'm always caught out by warnings on innocuous lines like

float PiForSquares = 4.0;

The problem is that the literal 4.0 is a double, not a float - Which is irritating.

For integer types, we have short int, int and long int, which is pretty straightforward. Why doesn't C just have short float, float and long float? And where on earth did "double" come from?

EDIT: It seems the relationship between floating types is similar to that of integers. double must be at least as big as float, and long double is at least as big as double. No other guarantees of precision/range are made.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

人事已非 2024-07-18 03:15:36

在定点表示中,小数点后有固定数量的数字(十进制表示中小数点的推广)。 与此相对的是浮点表示,其中小数点可以在所表示的数字的数字内移动或浮动。 因此得名“浮点表示法”。 这被缩写为“浮动”。

在 K&RC 中,float 指的是具有 32 位二进制表示的浮点表示,double 指的是具有 64 位二进制表示的浮点表示,或者是 double大小和名称的由来。 然而,最初的 K&R 规范要求所有浮点计算都以双精度完成。

在最初的 IEEE 754 标准 (IEEE 754-1985) 中,浮点表示和算术的黄金标准,为单精度和双精度浮点数的二进制表示提供了定义。 双精度数字的名称很恰当,因为它们表示的位数是单精度数字的两倍。

有关浮点表示的详细信息,请阅读 David Goldberg 的文章每个计算机科学家都应该了解浮点运算

In fixed-point representation, there are a fixed number of digits after the radix point (a generalization of the decimal point in decimal representations). Contrast to this to floating-point representations where the radix point can move, or float, within the digits of the number being represented. Thus the name "floating-point representation." This was abbreviated to "float."

In K&R C, float referred to floating-point representations with 32-bit binary representations and double referred to floating-point representations with 64-bit binary representations, or double the size and whence the name. However, the original K&R specification required that all floating-point computations be done in double precision.

In the initial IEEE 754 standard (IEEE 754-1985), the gold standard for floating-point representations and arithmetic, definitions were provided for binary representations of single-precision and double-precision floating point numbers. Double-precision numbers were aptly name as they were represented by twice as many bits as single-precision numbers.

For detailed information on floating-point representations, read David Goldberg's article, What Every Computer Scientist Should Know About Floating-Point Arithmetic.

长亭外,古道边 2024-07-18 03:15:36

它们被称为单精度和双精度,因为它们与处理器的自然大小(不确定该术语)有关。 因此,32 位处理器的单精度长度为 32 位,双精度长度为 64 位长度的两倍。 他们只是决定在 C 中将单精度类型称为“float”。

They're called single-precision and double-precision because they're related to the natural size (not sure of the term) of the processor. So a 32-bit processor's single-precision would be 32 bits long, and its double-precision would be double that - 64 bits long. They just decided to call the single-precision type "float" in C.

枯寂 2024-07-18 03:15:36

double 是“双精度”的缩写。
我猜想,long double 是因为当具有更高精度的浮点类型开始出现在处理器上时,不想添加另一个关键字。

double is short for "double precision".
long double, I guess, comes from not wanting to add another keyword when a floating-point type with even higher precision started to appear on processors.

梦在深巷 2024-07-18 03:15:36

好吧,从历史上看,它曾经是这样的:

用于 C 的原始机器将 16 位字分成 2 个字节,一个 char 是一个字节。 地址是 16 位,所以 sizeof(foo*) 是 2,sizeof(char) 是 1。 int 是 16 位,所以 sizeof(int)< /code> 也是 2。然后 VAX(扩展寻址)机器出现了,地址是 32 位。 char 仍然是 1 个字节,但 sizeof(foo*) 现在是 4 个字节。

存在一些混乱,这在 Berkeley 编译器中得到了解决,因此,short 现在是 2 个字节,而 int 是 4 个字节,因为它们非常适合高效的代码。 long 变成了 8 个字节,因为有一种针对 8 字节块的有效寻址方法 --- 称为双字。 4 字节块是,当然,2 字节块是半字

浮点数的实现方式是使其适合单字或双字。 为了保持一致,双字浮点数被称为“double”。

Okay, historically here is the way it used to be:

The original machines used for C had 16 bit words broken into 2 bytes, and a char was one byte. Addresses were 16 bits, so sizeof(foo*) was 2, sizeof(char) was 1. An int was 16 bits, so sizeof(int) was also 2. Then the VAX (extended addressing) machines came along, and an address was 32 bits. A char was still 1 byte, but sizeof(foo*) was now 4.

There was some confusion, which settled down in the Berkeley compilers so that a short was now 2 bytes and an int was 4 bytes, as those were well-suited to efficient code. A long became 8 bytes, because there was an efficient addressing method for 8-byte blocks --- which were called double words. 4 byte blocks were words and sure enugh, 2-byte blocks were halfwords.

The implementation of floating point numbers were such that they fit into single words, or double words. To remain consistent, the doubleword floating point number was then called a "double".

喵星人汪星人 2024-07-18 03:15:36

应该注意的是,double 不必能够保存比 float 更大的值; 它只需更加精确即可。

It should be noted that double does NOT have to be able to hold values greater in magnitude than those of float; it only has to be more precise.

娇纵 2024-07-18 03:15:36

因此 %f 表示浮点类型,%lf 表示长浮点类型,与 double 相同。

hence the %f for a float type, and a %lf for a long float which is the same as double.

樱娆 2024-07-18 03:15:35

术语“单精度”和“双精度”起源于 FORTRAN,并且在 C 发明时就已经被广泛使用。 在 20 世纪 70 年代早期的机器上,单精度的效率明显更高,并且与今天一样,使用的内存是双精度的一半。 因此,它是浮点数合理的默认值

long double 是在 IEEE 标准考虑到 Intel 80287 浮点芯片时添加的,该芯片使用 80 位浮点数而不是经典的 64 位浮点数。位双精度。

发问者关于保证的说法不正确; 如今,几乎所有语言都保证以单精度(32 位)和双精度(64 位)实现 IEEE 754 二进制浮点数。 有些还提供扩展精度(80 位),在 C 中显示为long double。 由 William Kahan 领导的 IEEE 浮点标准是优秀工程战胜权宜之计的胜利:在当时的机器上,它看起来昂贵得令人望而却步,但在今天的机器上它却非常便宜,而且 IEEE 浮点的可移植性和可预测性- 点数每年必须节省数百万美元。

The terms "single precision" and "double precision" originated in FORTRAN and were already in wide use when C was invented. On early 1970s machines, single precision was significantly more efficient and as today, used half as much memory as double precision. Hence it was a reasonable default for floating-point numbers.

long double was added much later when the IEEE standard made allowances for the Intel 80287 floating-point chip, which used 80-bit floating-point numbers instead of the classic 64-bit double precision.

Questioner is incorrect about guarantees; today almost all languages guarantee to implement IEEE 754 binary floating-point numbers at single precision (32 bits) and double precision (64 bits). Some also offer extended precision (80 bits), which shows up in C as long double. The IEEE floating-point standard, spearheaded by William Kahan, was a triumph of good engineering over expediency: on the machines of the day, it looked prohibitively expensive, but on today's machines it is dirt cheap, and the portability and predictability of IEEE floating-point numbers must save gazillions of dollars every year.

未蓝澄海的烟 2024-07-18 03:15:35

您可能知道这一点,但是您可以使文字浮点/长双精度

 float f = 4.0f;
 long double f = 4.0l;

Double 是默认值,因为这是大多数人使用的。 长双精度可能会造成杀伤力过大,或者浮点数的精度非常差。 Double 几乎适用于所有应用程序。

为何命名? 有一天,我们拥有的只是 32 位浮点数(实际上我们拥有的只是定点数,但我离题了)。 无论如何,当浮点成为现代体系结构中的流行功能时,C 可能是当时的语言,并且给出了“float”这个名称。 似乎有道理。

当时可能已经想到了 double,但在当时的 16 或 32 位 cpu/fp cpu 中并未真正实现。 一旦双精度数被用于更多的体系结构中,C 可能就会抽出时间来添加它。 C 需要一个两倍于浮点数大小的名称,因此我们得到了一个 double。 然后有人需要更精确,我们认为他疯了。 不管怎样我们还是添加了它。 四元组(?)这个名字太过分了。 长双打已经足够好了,没有人发出太大的噪音。

部分混乱在于,good-ole“int”似乎随着时间而变化。 过去,“int”表示 16 位整数。 然而,Float 绑定到 IEEE std 作为 32 位 IEEE 浮点数。 因此,C 将 float 定义为 32 位,并使用 double 和 long double 来引用更长的标准。

You probably knew this, but you can make literal floats/long doubles

 float f = 4.0f;
 long double f = 4.0l;

Double is the default because thats what most people use. Long doubles may be overkill or and floats have very bad precision. Double works for almost every application.

Why the naming? One day all we had was 32 bit floating point numbers (well really all we had was fixed point numbers, but I digress). Anyway, when floating point became a popular feature in modern architectures, C was probably the language dujour then, and the name "float" was given. Seemed to make sense.

At the time, double may have been thought of, but not really implemented in the cpu's/fp cpus of the time, which were 16 or 32 bits. Once the double became used in more architectures, C probably got around to adding it. C needed something a name for something twice the size of a float, hence we got a double. Then someone needed even more precision, we thought he was crazy. We added it anyway. The name quadtuple(?) was overkill. Long double was good enough, and nobody made a lot of noise.

Part of the confusion is that good-ole "int" seems to change with the time. It used to be that "int" meant 16 bit integer. Float, however, is bound to the IEEE std as the 32-bit IEEE floating point number. For that reason, C kept float defined as 32 bit and made double and long double to refer to the longer standards.

錯遇了你 2024-07-18 03:15:35

文字

问题是文字 4.0 是双精度型,而不是浮点型 - 这很烦人。

对于常量,整数和浮点数之间有一个重要区别。 虽然决定使用哪种整数类型相对容易(您选择足够小的整数类型来保存该值,但有符号/无符号会增加一些复杂性),但对于浮点数来说却不是那么容易。 许多值(包括像 0.1 这样的简单值)无法用浮点数精确表示,因此类型的选择不仅会影响性能,还会影响结果值。 在这种情况下,C 语言设计者似乎更喜欢稳健性而不是性能,因此他们决定默认表示应该是更精确的表示。

历史

为什么C不只有短浮点数、浮点数和长浮点数? 而“双”到底从何而来?

术语“单精度”和“双精度”起源于 FORTRAN,在 C 语言发明时就已经被广泛使用。

Literals

The problem is that the literal 4.0 is a double, not a float - Which is irritating.

With constants there is one important difference between integers and floats. While it is relatively easy to decide which integer type to use (you select smallest enough to hold the value, with some added complexity for signed/unsigned), with floats it is not this easy. Many values (including simple ones like 0.1) cannot be exactly represented by float numbers and therefore choice of type affects not only performance, but also result value. It seems C language designers preferred robustness against performance in this case and they therefore decided the default representation should be the more exact one.

History

Why doesn't C just have short float, float and long float? And where on earth did "double" come from?

The terms "single precision" and "double precision" originated in FORTRAN and were already in wide use when C was invented.

唯憾梦倾城 2024-07-18 03:15:35

首先,这些名称并非特定于 C++,但对于实现 IEEE 754 的任何浮点数据类型来说都是常见做法。

名称“double”指的是“双精度”,而 float 通常被称为“单精度” 。

First, these names are not specific to C++, but are pretty much common practice for any floating-point datatype that implements IEEE 754.

The name 'double' refers to 'double precision', while float is often said to be 'single precision'.

情丝乱 2024-07-18 03:15:35

两种最常见的浮点格式使用 32 位和 64 位,较长的浮点格式是第一个浮点格式的“双倍”,因此称为“双精度”。

The two most common floating point formats use 32-bits and 64-bits, the longer one is "double" the size of the first one so it was called a "double".

橪书 2024-07-18 03:15:35

双精度型之所以如此命名,是因为它是浮点型“精度”的两倍。 实际上,这意味着它使用浮点值空间的两倍——如果您的 float 是 32 位,那么您的 double 将是 64 位。

双精度这个名称有点用词不当,因为双精度浮点数的尾数精度为 52 位,而单精度浮点数的尾数精度为 23 位(双精度浮点数为 56 位)。 有关浮点的更多信息,请参见:浮点 - 维基百科,包括
底部链接到有关单精度和双精度浮点数的文章。

名称 long double 可能与整型的长整数与短整数遵循相同的传统,但在这种情况下,它们颠倒了它,因为“int”相当于“long int”。

A double is named such because it is double the "precision" of a float. Really, what this means is that it uses twice the space of a floating point value -- if your float is a 32-bit, then your double will be a 64-bit.

The name double precision is a bit of a misnomer, since a double precision float has a precision of the mantissa of 52-bits, where a single precision float has a mantissa precision of 23-bits (double that is 56). More on floating point here: Floating Point - Wikipedia, including
links at the bottom to articles on single and double precision floats.

The name long double is likely just down the same tradition as the long integer vs. short integer for integral types, except in this case they reversed it since 'int' is equivalent to 'long int'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文