Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of precision digits for double in C++ different in windows and Linux. Why? Linux shows more than 20 non-zero precision digits

Tags:

c++

c

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.

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.

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.

like image 284
gandalf34 Avatar asked Aug 05 '11 02:08

gandalf34


People also ask

What is the precision of double in C?

double has 15 decimal digits of precision.

How many digits is the accuracy of double precision?

Double precision numbers are accurate up to sixteen decimal places but after calculations have been done there may be some rounding errors to account for. In theory this should affect no more than the last significant digit but in practice it is safer to rely upon fewer decimal places.

What is the precision of double data type?

Precision: 15 to 17 significant digits, depending on usage. The number of significant digits does not depend on the position of the decimal point. Representation: The values are stored in 8 bytes, using IEEE 754 Double Precision Binary Floating Point format.

How many digits can double hold C?

The C++ double should have a floating-point precision of up to 15 digits as it contains a precision that is twice the precision of the float data type.


1 Answers

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.

like image 139
Mark Ransom Avatar answered Sep 30 '22 18:09

Mark Ransom