Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does setprecision in c++ round? If so why am I seeing this?

The following snippet outputs 0.29847 when I would have expected 0.29848:

double f = 0.298475;
cout << setprecision(5) << f << endl;

For other examples, however, I observe rounding:

double f = 0.123459;
cout << setprecision(5) << f << endl;

outputs: 0.12346

and

double f = 0.123454;
cout << setprecision(5) << f << endl;

outputs: 0.12345

like image 664
Palace Chan Avatar asked Jun 06 '12 21:06

Palace Chan


2 Answers

The number 0.298475 isn't representable exactly in a double (since it's not a fraction whose denominator is a power of two, being 11939/40000), and the actual number that is stored is actually closer to 0.29847 than to 0.29848.

like image 165
Kerrek SB Avatar answered Nov 01 '22 05:11

Kerrek SB


As far I can see from the documentation setprecision() doesn't perform any rounding, but just sets the maximum number of digits displayed after the decimal point. Possible representations of double and float numbers may differ from the constant initializers you use.

like image 22
πάντα ῥεῖ Avatar answered Nov 01 '22 04:11

πάντα ῥεῖ