Double precision

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.

What do you suggest?
> What do you suggest?

Use an arbitrary-precision floating point library.
http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic#Libraries
hi fpiro07

the number should be fully stored, however it may be truncated when passed to streams (ie. cout). To change this to show n digits, use this

out.precision(n);

See this:

http://www.cplusplus.com/reference/ios/ios_base/precision/
Last edited on
1
2
3
4
5
6
    double      a = 1.23456789098765412345;
    long double 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...

Ok with the cout.precision() it prints the complete number but I don't want print it, I want to pass it to a function as a parameter.

Using the debugger I saw that 1.23456789098765 becomes 1.23457 and with that number my function enters a infinite loop.

Is using an arbitrary-precision floating point library the only solution?

I could actually write a restriction on the user manual to limit the floating point digits number but I wouldn't like that...
I want to pass it to a function as a parameter

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 long double is displayed inaccurately. Your environment may behave differently of course.
Last edited on
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.

Can anyone explain me how to use it on Macintosh?
fpiro07 wrote:
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.
Last edited on
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.
Topic archived. No new replies allowed.