Floating point puzzle
Ken "Turk" Turkowski
turk at Apple.COM
Thu Aug 11 09:29:13 AEST 1988
In article <3117 at emory.uucp> riddle at emory.uucp (Larry Riddle) writes:
> 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
>
>Notice that x and y, which have been declared as floats, and thus have
>a 32 bit representation (according to the manual this obeys IEEE
>floating point arithmetic standards), both are printed the same in hex,
>but give different values when printed as floats. I believe that the
>hex is a straight translation of the internal bit representation. The
>division in computing x and y is done in double precision (64 bits) and
>then converted to floats.
The problem is that floats are converted to doubles (or extendeds on the
macintosh). A double has an 11-bit exponent, whereas a float only has 8.
If you print out the next word, you'll see that the two hex representations
differ somewhere in the next 3 bits.
Ken Turkowski @ Apple Computer, Inc., Cupertino, CA
UUCP: {mtxinu,sun,decwrl,nsc,voder}!apple!turk
CSNET: turk at Apple.CSNET
ARPA: turk%Apple at csnet-relay.ARPA
Domain-based address: turk at apple.com
More information about the Comp.lang.c
mailing list