C 和 C++ 中 float 和 double 的大小是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25524355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:20:17  来源:igfitidea点击:

What is the size of float and double in C and C++?

c++cfloating-pointx8664-bit

提问by mans

I was looking to see if there is any standard type similar to uint32_twhich always would map into a 32-bit unsigned integral type but I could not find any.

我想看看是否有任何标准类型类似于uint32_t总是映射到 32 位无符号整数类型,但我找不到任何。

Is the size of floatalways 4 byte on all platform?
Is the size of doublealways 8?

float所有平台上的大小总是 4 字节吗?
大小double总是 8 吗?

Does either standard say anything on the matter?

任何一个标准都对此事有任何说明吗?

I want to make sure that my size is always the same on all platforms (x86 and x64) so I am using standard int types, but I could not find any similar typedef for floatand double.

我想确保我的大小在所有平台(x86 和 x64)上总是相同的,所以我使用标准的 int 类型,但我找不到任何类似的 typedeffloatdouble

回答by Deduplicator

Excerpt from the C99 standard, normative annex F (The C++-standard does not explicitly mention this annex, though it includes all affected functions without change per reference. Also, the types have to match for compatibility.):

摘自 C99 标准,规范性附录 F(C++ 标准没有明确提到这个附录,尽管它包括所有受影响的函数,每个参考都没有改变。此外,类型必须匹配以保持兼容性。):

IEC 60559 floating-point arithmetic

F.1 Introduction

1 This annex specifies C language support for the IEC 60559 floating-point standard. The IEC 60559 floating-point standard is specifically Binary floating-point arithmetic for microprocessor systems, second edition (IEC 60559:1989), previously designated IEC 559:1989 and as IEEE Standard for Binary Floating-Point Arithmetic (ANSI/IEEE 754?1985). IEEE Standard for Radix-Independent Floating-Point Arithmetic (ANSI/IEEE 854?1987) generalizes the binary standard to remove dependencies on radix and word length. IEC 60559 generally refers to the floating-point standard, as in IEC 60559 operation, IEC 60559 format, etc. An implementation that defines __STDC_IEC_559__shall conform to the specifications in this annex.356) Where a binding between the C language and IEC 60559 is indicated, the IEC 60559-specified behavior is adopted by reference, unless stated otherwise. Since negative and positive infinity are representable in IEC 60559 formats, all real numbers lie within the range of representable values.

IEC 60559 浮点运算

F.1 简介

1 本附件规定了对 IEC 60559 浮点标准的 C 语言支持。IEC 60559 浮点标准是专门用于微处理器系统的二进制浮点运算,第二版 (IEC 60559:1989),之前指定为 IEC 559:1989 和作为二进制浮点运算的 IEEE 标准 (ANSI/IEEE 754?1985 )。IEEE Standard for Radix-Independent Floating-Point Arithmetic (ANSI/IEEE 854?1987) 概括了二进制标准以消除对基数和字长的依赖。IEC 60559 一般是指浮点标准,如 IEC 60559 操作、IEC 60559 格式等。__STDC_IEC_559__应符合本附件中的规范。356) 如果指出 C 语言和 IEC 60559 之间的绑定,则 IEC 60559 指定的行为通过引用采用,除非另有说明。由于负无穷大和正无穷大可以用 IEC 60559 格式表示,因此所有实数都在可表示值的范围内。

So, include <math.h>(or in C++ maybe <cmath>), and test for __STDC_IEC_559__.

因此,包括<math.h>(或在 C++ 中<cmath>),并测试__STDC_IEC_559__.

If the macro is defined, not only are the types better specified (floatbeing 32bits and doublebeing 64bits among others), but also the behavior of builtin operators and standard-functions is more specified.
Lack of the macro does not give any guarantees.

如果定义了宏,不仅可以更好地指定类型(float32 位和double64 位等),而且更指定内置运算符和标准函数的行为。
缺少宏不提供任何保证。

For x86 and x86_64 (amd64), you can rely on the types floatand doublebeing IEC-60559-conformant, though functions using them and operations on them might not be.

对于 x86 和 x86_64 (amd64),您可以依赖类型floatdouble符合 IEC-60559,尽管使用它们的函数和对它们的操作可能不符合。

回答by Blaz Bratanic

Does not say anything about the size.

没有说大小。

3.9.1.8

3.9.1.8

There are three floating point types: float, double, and long double. The type double provides at least as much precision as float, and the type long double provides at least as much precision as double. The set of values of the type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set of values of the type long double. The value representation of floating-point types is implementation-defined. Integral and floating types are collectively called arithmetic types. Specializations of the standard template std::numeric_limits (18.3) shall specify the maximum and minimum values of each arithmetic type for an implementation.

共有三种浮点类型:float、double 和 long double。double 类型提供的精度至少与 float 一样,long double 类型提供的精度至少与 double 一样。float 类型的值集是 double 类型的值集的子集;double 类型的值集是 long double 类型的值集的子集。浮点类型的值表示是实现定义的。整型和浮点型统称为算术类型。标准模板 std::numeric_limits (18.3) 的特化应指定实现的每个算术类型的最大值和最小值。

回答by danielfranca

The C++ standard doesn't say anything, but in most of the platforms C++ use the single/double precision standard from IEEE, which define single precision as 4 bytes, and double precision as 8 bytes.

C++ 标准没有说明,但在大多数平台中,C++ 使用 IEEE 的单/双精度标准,将单精度定义为 4 个字节,双精度定义为 8 个字节。

http://en.wikipedia.org/wiki/Single-precision_floating-point_formathttp://en.wikipedia.org/wiki/Double-precision_floating-point_format

http://en.wikipedia.org/wiki/Single-precision_floating-point_format http://en.wikipedia.org/wiki/Double-precision_floating-point_format

I'm not sure about the exceptions for these cases.

我不确定这些情况的例外情况。

回答by Bathsheba

As floating point operations are implemented at a low level by CPUs, the C++ standard does not mandate a size for either a float, doubleor long double. All it says is that the order I specified them is in equal or increasing order of precision.

浮点操作是在通过CPU的低电平来实现,C ++标准并没有规定的尺寸为任何一个floatdoublelong double。它只是说我指定它们的顺序是相同或递增的精度顺序。

Your best bet is to use static_assert, sizeof, typedefand #definecarefully in order to define cross platform floating point types.

最好的办法是使用static_assertsizeoftypedef#define特别小心,以定义跨平台的浮点类型。

回答by GreenScape

I want to point out that even if you have same size floats you can notbe sure these floats are equally interpreted on different platforms. You can read a lot of papers about 'floats over network'. Floats non-determinism is a known problem.

我想指出的是,即使您有相同大小的浮点数,您也无法确定这些浮点数在不同平台上的解释是否相同。你可以阅读很多关于“网络浮动”的论文。浮动不确定性是一个已知问题。

回答by rcgldr

In the case of X86, even if using IEEE single and double precision numbers, the internal calculations are affected by a floating point control word (FCW). The internal calculations are normally 64 bit or 80 bit (long double). You can override this using inline assembly code, but there's no guarantee that some double precision library function won't set it back.

在 X86 的情况下,即使使用 IEEE 单精度和双精度数,内部计算也会受到浮点控制字 (FCW) 的影响。内部计算通常是 64 位或 80 位(长双精度)。您可以使用内联汇编代码覆盖它,但不能保证某些双精度库函数不会将其设置回原位。

Microsoft supported 80 bit long doubles with their 16 bit compilers, but dropped support for them with their 32 bit and 64 bit compilers, and long doubles are now the same as doubles at 64 bits.

Microsoft 的 16 位编译器支持 80 位长双精度,但他们的 32 位和 64 位编译器放弃了对它们的支持,现在长双精度与 64 位双精度相同。

回答by HRold

You can try to use a library offering cross-platform data types compatibility.

您可以尝试使用提供跨平台数据类型兼容性的库。

"The integral types C++ inherited from C are a cross-platform hazard. int, long and friends have different sizes on different platforms (32-bit and 64-bit on today's systems, maybe 128-bit later). For some applications it might seem irrelevant because they never approach the 32-bit limit (or rather 31-bit if you use unsigned integers), but if you serialize your objects on a 64-bit system and deserialize on a 32-bit system you might be unpleasantly surprised.
APR provides a set of typedefs for basic types that might be different on different platforms. These typedefs provide a guaranteed size and avoid the fuzzy built-in types. However, for some applications (mostly numerical) it is sometimes important to use the native machine word size (typically what int stands for) to achieve maximal performance."

“从 C 继承的整数类型 C++ 是一个跨平台的危险。int、long 和朋友在不同的平台上有不同的大小(今天的系统上是 32 位和 64 位,以后可能是 128 位)。对于某些应用程序,它可能似乎无关紧要,因为它们从不接近 32 位限制(或者,如果使用无符号整数,则为 31 位),但是如果您在 64 位系统上序列化对象并在 32 位系统上反序列化,您可能会感到不快。
APR 为在不同平台上可能不同的基本类型提供了一组 typedef。这些 typedef 提供了有保证的大小并避免了模糊的内置类型。但是,对于某些应用程序(主要是数字),有时使用本机机器很重要字大小(通常是 int 代表)以实现最大性能。”

Gigi SAYFAN - Building Your Own Plugin Framework(From http://philippe.ameline.free.fr/techytechy/071125_PluginFramework.htm)

Gigi SAYFAN -构建您自己的插件框架(来自http://philippe.ameline.free.fr/techytechy/071125_PluginFramework.htm