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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With