Having problems with rounding in c
While trying to figure out how to round a float 1.255
to the nearest hundredth, I found something interesting. I am using gcc 4.4.5 on Debian 6.
int x = (1.255 * 100) + 0.5; // gives me back 125 instead of 126.
float y = (1.255 * 100) + 0.5; // gives me back 126.000000.
Why does this happen when I save int
, I go back 125
instead of 126
? On Fedora, when I save the above expression to int
, I am back 126
. Is this a gcc bug in debian? Any help would be greatly appreciated. Thank.
source to share
While this looks like a "typical" floating point question, it's harder than that.
This is a combination of three things:
- "Normal" floating point material.
- Implicit casting.
- Implicit type promotion.
Let's break this down:
Floating point literals are by default of type double
. Hence it 1.255
is of type double
.
So the expression:
(1.255 * 100) + 0.5
is performed using the type double
.
But since binary floating point cannot represent exactly 1.255
, the expression evaluates as:
(1.255 * 100) + 0.5 = 125.99999999999999000000
and has a type double
.
Since it is less than 126
, storing it as an integer will result in 125
. Saving it to float
rounds it to the nearest float
, which results in 126
.
int x = (1.255 * 100.) + 0.5;
float y = (1.255 * 100.) + 0.5;
double z = (1.255 * 100.) + 0.5;
cout << fixed;
cout << x << endl;
cout << setprecision(20) << y << endl;
cout << setprecision(20) << z << endl;
Output:
125
126.00000000000000000000
125.99999999999999000000
source to share