I have to store quite a precise double and writing std::cout << std::numeric_limits<double>::digits10 << std::endl; I learned that the precision of a double is 15.
So when I write double a = 1.23456789012345 the number should be completely stored but it's not.
double a = 1.23456789098765412345;
longdouble b = 1.23456789098765412345L; // Note the 'L' suffix.
cout.precision(19);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
a = 1.234567890987654071
b = 1.234567890987654123
You should not rely on the accuracy of the last digits, as the value is stored internally in binary format. Thus it may not be possible to accurately represent decimal digits.
For example the decimal value 0.1 doesn't have an exact binary representation, it is a recurring value, in much the same way that the fraction 1/3 is a recurring decimal 0.333...
The number will be unchanged when passed to the function.
Either the debugger is showing an abbreviated value rather than the full number, or you may have used a lower-precision type such as a float instead of a double somewhere in the chain.
You can insert temporary cout messages in the function (or log your diagnostic messages to a file) in order to see what the full precision values really are, just in case the debugger is showing misleading information.
edit:I just checked my own debugger, it seems to display the double values correctly, but the longdouble is displayed inaccurately. Your environment may behave differently of course.
I don't know what's wrong with my compiler but it actually stores a double with only 7 digits of precision as if it was a float but using the cout.precision(15) it prints the complete double number.
Anyways I decided that I am going to use an arbitrary-precision floating point library as JLBorges told me to do.
I found out that GMP is one of the best ones isn't it?
I tried to install it but I didn't actually understand anything of how to use it in my C++ programmes.
I don't know what's wrong with my compiler but it actually stores a double with only 7 digits of precision as if it was a float but using the cout.precision(15) it prints the complete double number.
That is impossible.
There's no way the program could output the value which has not been stored. You must be misunderstanding something.
Perhaps you should post the code which is giving you the problem
Edit: It's a bit of a warning sign when the compiler is blamed for something not working as expected. Almost certainly the compiler is innocent, and the problem is elsewhere.
Ok guys thank you everyone for your help but I found out that there was no problem with my compiler.
It was just the debugger that showed only the first 6 digits but it actually stored all of them.
I don't know why in the first place my function didn't work.