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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14221612/
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-27 18:07:38  来源:igfitidea点击:

Difference between long double and double in C and C++

c++c

提问by user1543957

Possible Duplicate:
long double vs double

可能的重复:
long double vs double

I am new to programming and I am unable to understand the difference between between long double and double in C and C++. I tried to Google it but was unable to understand it and got confused. Can anyone please help.?

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

回答by greyfade

To quote the C++ standard, §3.9.1 ?8:

引用 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.

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

That is to say that doubletakes at least as much memory for its representation as floatand long doubleat least as much as double. That extra memory is used for more precise representation of a number.

这是说,double至少需要尽可能多的内存供其作为代表floatlong double至少不亚于double。额外的内存用于更精确地表示数字。

On x86 systems, floatis typically 4 bytes long and can store numbers as large as about 3×103? and about as small as 1.4×10???. It is an IEEE 754 single-precision numberthat stores about 7 decimal digits of a fractional number.

在 x86 系统上,float通常是 4 个字节长,可以存储大约 3×103 的数字?大约小到 1.4×10???。它是一个 IEEE 754单精度数,可存储小数的大约 7 位十进制数字。

Also on x86 systems, doubleis 8 bytes long and can store numbers in the IEEE 754 double-precision format, which has a much larger range and stores numbers with more precision, about 15 decimal digits. On some other platforms, doublemay not be 8 bytes long and may indeed be the same as a single-precision float.

同样在 x86 系统上,double长度为 8 个字节,可以以 IEEE 754双精度格式存储数字,该格式具有更大的范围并以更高的精度存储数字,大约为 15 位十进制数字。在其他一些平台上,double可能不是 8 个字节长,并且可能确实与单精度float.

The standard only requires that long doubleis at least as precise as double, so some compilers will simply treat long doubleas if it is the same as double. But, on most x86 chips, the 10-byte extended precision format80-bit number is available through the CPU's floating-point unit, which provides even more precision than 64-bit double, with about 21 decimal digits of precision.

该标准只要求long double至少是一样精确的double,所以有些编译器会简单地对待long double,就好像它是一样的double。但是,在大多数 x86 芯片上,10 字节扩展精度格式80 位数字可通过 CPU 的浮点单元获得,它提供比 64 位更高的精度double,具有大约 21 位十进制数字的精度。

Some compilers instead support a 16-byte (128-bit) IEEE 754 quadruple precision number formatwith yet more precise representations and a larger range.

一些编译器反而支持 16 字节(128 位)IEEE 754四倍精度数字格式,具有更精确的表示和更大的范围。

回答by hmatar

It depends on your compiler but the following code can show you the number of bytes that each type requires:

这取决于您的编译器,但以下代码可以显示每种类型所需的字节数:

int main() { 
    printf("%d\n", sizeof(double)); // some compilers print 8
    printf("%d\n", sizeof(long double)); // some compilers print 16
    return 0;
}

回答by alk

A long <type>data type mayhold larger values then a <type>data type, depending on the compiler.

long <type>数据类型可以保持较大的值,则一个<type>数据类型,这取决于编译器。