C++ 中 double 的精度位数在 windows 和 Linux 中不同。为什么?Linux 显示超过 20 个非零精度数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6950738/
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
Number of precision digits for double in C++ different in windows and Linux. Why? Linux shows more than 20 non-zero precision digits
提问by gandalf34
Just did this:
刚做了这个:
double val1=numeric_limits<double>::max();
cout.precision(70);
cout<<"\nVal1: "<<val1;
In Windows I start getting 0s after 17 digits(16 digits after decimal point). However in Linux, as I keep increasing the cout.precision(NUMBER), more and more digits keep showing and they are not zeros.
在 Windows 中,我开始在 17 位(小数点后 16 位)后得到 0。但是在 Linux 中,随着我不断增加 cout.precision(NUMBER),越来越多的数字不断显示并且它们不是零。
Also, running the following code shows '15' on both Windows and Linux. Windows system is 32 bits and Linux one is 64 bit if that makes any difference.
此外,运行以下代码在 Windows 和 Linux 上都显示“15”。Windows 系统是 32 位,Linux 系统是 64 位,如果这有什么区别的话。
typedef std::numeric_limits< double > dl;
cout << "\tdigits (decimal):\t" << dl::digits10 << endl;
Can any one help with an explanation as to what is going on here? I thought number of precision digits will be same in Windows & Linux since sizeof(double) is 8 on both of them.
任何人都可以帮助解释这里发生的事情吗?我认为 Windows 和 Linux 中的精度位数是相同的,因为它们的 sizeof(double) 都是 8。
回答by Mark Ransom
Once you get past the number of digits contained in a double, you're at the mercy of your compiler's library implementation. Different algorithms for converting from binary to decimal will result in different output. Neither can be more accurate than the other.
一旦超过了 double 中包含的位数,您就会受到编译器库实现的支配。从二进制转换为十进制的不同算法将导致不同的输出。两者都不可能比另一个更准确。
回答by Dietrich Epp
When you print out a double
, you often have to print out many, many digits before you print out the exact value of the double
. It is possible to print out a double
exactly. For example, the double
closest to 1/3 has the value:
当您打印出 a 时double
,您通常必须先打印出许多数字,然后才能打印出double
. 可以double
准确地打印出一个。例如,double
最接近 1/3 的值为:
0.333333333333333314829616256247390992939472198486328125
Printing out this value exactly requires 54 digits past the decimal point. But people say a double
only has about16 digits of precision. What gives?
打印出这个值需要小数点后 54 位数字。但是人们说 adouble
只有大约16 位的精度。是什么赋予了?
When you say that a double has 16 digits of precision, that means that you need at least 16 digits to make the double survive a round trip. That is, the following process preserves the input data:
当您说 double 具有 16 位精度时,这意味着您至少需要 16 位数字才能使 double 在往返过程中幸存下来。即,以下过程保留输入数据:
double -> 16 digit decimal -> double
So the extra digits past 16 aren't necessarily garbage, they're just unnecessary. And according to the standard, they can be almost anything -- as long as reading the result will give you the same double
back.
所以超过 16 的额外数字不一定是垃圾,它们只是不必要的。根据标准,它们几乎可以是任何东西——只要阅读结果会给你同样的double
回报。
The Summary:My guess is that your standard library on Linux is printing out the exact value of the double, and the Windows library is truncating the result. Both actions are permitted by the standard.
总结:我的猜测是 Linux 上的标准库正在打印出双精度值的确切值,而 Windows 库正在截断结果。标准允许这两种操作。
You almost certainly don't need the exact value of a double anyway, since arithmetic on floating point numbers is usually inexact.
无论如何,您几乎肯定不需要 double 的确切值,因为浮点数的算术通常是不准确的。
回答by Stu Anderson
The Wikipedia entry on double precisiondefines the bounding errors for translation between decimal digits and double values very succinctly:
关于双精度的维基百科条目非常简洁地定义了十进制数字和双精度值之间转换的边界误差:
This gives from 15 - 17 significant decimal digits precision. If a decimal string with at most 15 significant decimal is converted to IEEE 754 double precision and then converted back to the same number of significant decimal, then the final string should match the original; and if an IEEE 754 double precision is converted to a decimal string with at least 17 significant decimal and then converted back to double, then the final number must match the original.
这给出了 15 - 17 位有效十进制数字的精度。如果将最多有 15 位有效十进制数的十进制字符串转换为 IEEE 754 双精度,然后再转换回相同数量的有效十进制数,则最终字符串应与原始字符串匹配;如果将 IEEE 754 双精度转换为具有至少 17 位有效十进制数的十进制字符串,然后再转换回双精度,则最终数字必须与原始数字匹配。
回答by Vahagn
I think you have installed g++ 32 bit version on windows, and 64 bit on linux. Just verify the program you are running, if it's 32 or 64 bit (you can check it by watching in task manager)
我认为您已经在 Windows 上安装了 g++ 32 位版本,在 linux 上安装了 64 位版本。只需验证您正在运行的程序,如果它是 32 位或 64 位(您可以通过在任务管理器中查看来检查它)