The number of precision digits for doubles in C ++ is different in Windows and Linux. What for? Linux shows over 20 non-zero precision digits

Just did it:

double val1=numeric_limits<double>::max(); cout.precision(70); cout<<"\nVal1: "<<val1; 

On Windows, I start getting 0 s after 17 digits (16 digits after the decimal point). However, on Linux, as I continue to increase cout.precision (NUMBER), more and more digits continue to show, and they are not zeros.

In addition, running the following code shows "15" on Windows and Linux. Windows is 32 bits, and Linux is 64 bits, if that matters.

 typedef std::numeric_limits< double > dl; cout << "\tdigits (decimal):\t" << dl::digits10 << endl; 

Can someone help with an explanation of what is going on here? I thought that the number of precision digits would be the same on Windows and Linux, since sizeof (double) is 8 on both of them.

+6
source share
4 answers

Once you go through the number of digits contained in the double, you will be at the mercy of the implementation of the compiler library. Different binary to decimal conversion algorithms will lead to different conclusions. None of them can be more accurate than the other.

+8
source

When you print a double , you often have to print many, many numbers before printing the exact double value. You can accurately print a double . For example, double closest to 1/3 has the value:

 0.333333333333333314829616256247390992939472198486328125 

Printing this value requires exactly 54 digits after the decimal point. But people say double has about 16 digits of precision. What gives?

When you say that double has 16 digits of precision, that means you need at least 16 digits to double survive both ways. That is, the following process saves the input data:

 double -> 16 digit decimal -> double 

Thus, additional numbers minus 16 are not necessarily garbage, they are just superfluous. And according to the standard, they can be almost any - as long as reading the result will give you the same double back.

Summary: I assume that your standard Linux library prints the exact double value, and the Windows library truncates the result. Both actions are permitted by the standard.

You almost certainly don't need the exact double value anyway, since the arithmetic of floating point numbers is usually inaccurate.

+8
source

Wikipedia's entry on double precision greatly limits limiting errors for translating decimal digits and double values:

This gives 15 to 17 significant decimal digits. If a decimal string containing no more than 15 significant decimal places is converted to IEEE 754 double precision and then converted back to the same number from the significant decimal, then the final string must match the original; and if the double precision of IEEE 754 is converted to a decimal string with at least 17 significant decimal places, and then converted back to double, then the final number must match the original.

+2
source

I think you installed g ++ 32-bit on windows and 64-bit on Linux. Just check the running program if it is 32 or 64 bit (you can check it by observing in the task manager)

-1
source

Source: https://habr.com/ru/post/894340/