c++ - C和C++中long double和double的区别

标签 c++ c long-double

Possible Duplicate:
long double vs double

我是编程新手,我无法理解 C 和 C++ 中 long double 和 double 之间的区别。我试图谷歌它,但无法理解它并感到困惑。有人可以帮忙吗?

最佳答案

引用 C++ 标准,§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.

也就是说 double 占用的内存至少和 float 一样多,long double 至少和 。额外的内存用于更精确地表示数字。

在 x86 系统上,float 通常有 4 个字节长,可以存储大约 3×10³⁸ 和 1.4×10⁻⁴⁵ 的数字。它是 IEEE 754 single-precision number存储大约 7 位小数的小数。

同样在 x86 系统上,double 是 8 字节长,可以在 IEEE 754 double-precision format 中存储数字,它具有更大的范围并存储更精确的数字,大约 15 个十进制数字。在其他一些平台上,double 可能不是 8 字节长,并且可能确实与单精度 float 相同。

标准只要求 long double 至少与 double 一样精确,因此一些编译器会简单地将 long double 视为与 double 相同。但是,在大多数 x86 芯片上,10 字节 extended precision format 80 位数字可通过 CPU 的浮点单元获得,它提供比 64 位 double 更高的精度,大约为 21 位十进制数字。

有些编译器支持 16 字节(128 位)IEEE 754 quadruple precision number format具有更精确的表示和更大的范围。

关于c++ - C和C++中long double和double的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14221612/

相关文章:

c++ - 在 C++ 中从 double 更改为 long double 不再给出正确的结果

C++ 原生类型 char 可以保存文件结尾字符吗?

c++ - OpenMP 并行部分的 firstprivate 中是否允许非 POD 数据类型?

c - 循环的执行速度随变量位置而变化

c++ - 数组基址指针及其地址相同。为什么?

c++ - 为什么我的 C++ 程序从 long double 切换到 float128 时如此慢?

c++ - PIMPL 习惯用法,用于指向 C++ 中的类

c++ - 在 CentOS for Windows 上交叉编译 C++11/14

c - 如何知道我的项目在构建代码时使用哪个头文件

c - 如何从科学记数法解析长双