问题描述
我想看看是否有任何类似于 uint32_t
的标准类型,它总是会映射到 32 位无符号整数类型,但我找不到.
I was looking to see if there is any standard type similar to uint32_t
which always would map into a 32-bit unsigned integral type but I could not find any.
float
的大小在所有平台上总是 4 字节吗?double
的大小总是 8 吗?
Is the size of float
always 4 byte on all platform?
Is the size of double
always 8?
这两个标准是否对此事有任何说明?
Does either standard say anything on the matter?
我想确保我的大小在所有平台(x86 和 x64)上始终相同,所以我使用标准的 int 类型,但是我找不到任何类似的 float
和 双.
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 float
and double
.
推荐答案
摘自 C99 标准,规范性附录 F(C++ 标准没有明确提到这个附录,尽管它包括所有受影响的功能,每个参考都没有改变.另外,为了兼容性,类型必须匹配.):
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.):
1 本附件规定了对 IEC 60559 浮点标准的 C 语言支持.这IEC 60559 浮点标准专门用于二进制浮点运算微处理器系统,第二版(IEC 60559:1989),以前指定IEC 559:1989 和作为二进制浮点运算的 IEEE 标准(ANSI/IEEE 754-1985).独立于基数的浮点数的 IEEE 标准算术 (ANSI/IEEE 854−1987) 概括了二进制标准以删除依赖于基数和字长.IEC 60559 一般是指浮点数标准,如 IEC 60559 操作、IEC 60559 格式等.定义 __STDC_IEC_559__
应符合本附件中的规范.356)如果指明了 C 语言和 IEC 60559 之间的绑定,则除非另有说明,否则 IEC 60559 指定的行为通过引用采用.自从负无穷大和正无穷大可以用 IEC 60559 格式表示,所有实数都在在可表示的值范围内.
因此,包括 (或在 C++ 中可能是
),并测试
__STDC_IEC_559__
.
So, include <math.h>
(or in C++ maybe <cmath>
), and test for __STDC_IEC_559__
.
如果定义了宏,不仅可以更好地指定类型(float
为 32 位,double
为 64 位等),而且内置运算符的行为和标准功能更详细.
缺少宏不提供任何保证.
If the macro is defined, not only are the types better specified (float
being 32bits and double
being 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.
对于 x86 和 x86_64 (amd64),您可以依赖类型 float
和 double
符合 IEC-60559,尽管使用它们的函数和对它们的操作可能不是.
For x86 and x86_64 (amd64), you can rely on the types float
and double
being IEC-60559-conformant, though functions using them and operations on them might not be.
这篇关于C 和 C++ 中 float 和 double 的大小是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!