Red Hat Bugzilla – Bug 141848
Weird rounding of floats
Last modified: 2007-11-30 17:10:56 EST
Also happens with gcc 3.2 and 4.0-pre (as shipped with FC3)
$ cat test.c
test (float zoom)
printf ("%d %f\n", (int) (zoom * 100.0), (zoom * 100));
int main (int argc, char **argv)
$ gcc -o test test.c
It's supposed to print:
There is nothing wrong on this, that's how floating point works.
printf does no rounding. 1.01f is rounded when stored in float and the result
is inexact too.
E.g. 1.01 rounded to fit into float and double is:
and the exact number is actually 0x1.028f5c with period 0x28f5c.
As you can see, when rounded to float it is slightly smaller and double is
slightly bigger than the exact value. Therefore it is no surpise if this
0x0.0000028f5c28f5c28f5c28f5c28f5c28f5c28f5c28f5c... difference multiplied
by 100 already shows up among the important bits.