Floating point puzzle
braner
braner at batcomputer.tn.cornell.edu
Mon Aug 8 09:35:05 AEST 1988
In article <3117 at emory.uucp> riddle at emory.uucp (Larry Riddle) writes:
>
>main()
>{
> float x,y;
> x = 1.0/10.0;
> y = 1677721.0/16777216.0;
> printf("x: %x",x);
> printf("%20.17f\n",x);
> printf("y: %x",y);
> printf("%20.17f\n",y);
>}
>
>Here is the output:
>
>x: 3fb99999 0.10000000149011612
>y: 3fb99999 0.09999996423721313
Seems that the calculation of y is just different enough from 1/10 to cause
a difference in the last bit or so of the floats. In the printf() calls the
floats are converted to doubles before being passed to the function, and
since the doubles have a larger exponent field the difference in the
mantissa is out of sight within the (long) int referred to in the "%x".
I suggest you try:
float x;
long *p;
...
p = (long *) &x;
printf ("representation of x: %lx\n", *p);
...and so on...
- Moshe Braner
More information about the Comp.lang.c
mailing list