gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 22:08:21 32 4
gpt4 key购买 nike

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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com