I figured out that double type on my machine corresponds to this Wikipedia article, and long double corresponds to this text: x86 Extended Precision Format.
That’s why floor(52/log2(10)) or 15 digits of double should be correct and floor(63/log2(10)) or 19 digits of long double are trusted.
The code:
int main()
{
double d=0.1;
long double ld=0.1;
std::cout.precision(19);
std::cout.setf(std::ios_base::scientific);
std::cout << d << std::endl;
std::cout << ld << std::endl;
return 0;
}
gives the output:
1.0000000000000000555e-01
1.0000000000000000555e-01
If we set cout.precision to 16, the output will be:
1.0000000000000001e-01
1.0000000000000001e-01
It’s allright, that 17th digit of double in 1st output and 16th digit of double in second output is incorrect. But why is it incorrect for long double? Is there a way to get all 19 correct digits of long double variables?
If I try printf("%.19Le\n",ld); for long double, I get exactly the same result.
I use OpenSUSE 12.1 and g++ 4.6.2.
The
0.1literal constant is not a long double, it is probably a double .You may want to initialize
ldwithSo that the division involves two long double operands, so hopefully is done in long double (I am not sure of that, you have to double check the appropriate C++ standards).
And you probably want to compile with GCC supporting the latest standard. With a GCC 4.7 I would suggest to compile with
g++ -Wall -std=c++11but with your 4.6 you may need to say-std=c++0x