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

标签 c++ c floating-point x86 64-bit

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

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

这两个标准对此事有任何说明吗?

我想确保我的大小在所有平台(x86 和 x64)上始终相同,因此我使用标准 int 类型,但我找不到 float

最佳答案

摘自 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.

所以,包括 <math.h> (或者在 C++ 中可能是 <cmath> ),并测试 __STDC_IEC_559__ .

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

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

关于c++ - C 和 C++ 中 float 和 double 的大小是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25524355/

相关文章:

c++ - vector<char> 到字符串段错误

c - 打印时动态分配的矩阵会出现段错误

c++ - TCP 状态转换是如何发生的?

c++ - 返回语句中 vector 初始化的编译错误

c - 什么是 LD_PRELOAD 技巧?

c - 是否可以用空格而不是 C 中的零来填充前导零?

java - 如何准确判断 double 是否为整数?

c - 理解 c 中的 float 有问题。为什么它会进入无限循环?

c++ - Ostream << 运算符重载及其返回类型

在开关盒中类型转换成更大的类型