Xref: utzoo comp.lang.c:11750 comp.std.c:249 sci.math:4360 Path: utzoo!attcan!uunet!husc6!mailrus!cornell!batcomputer!braner From: braner@batcomputer.tn.cornell.edu (braner) Newsgroups: comp.lang.c,comp.std.c,sci.math Subject: Re: Floating point puzzle Summary: Beware of conversion to double in passing to function Keywords: floating point representation Message-ID: <5810@batcomputer.tn.cornell.edu> Date: 7 Aug 88 23:35:05 GMT References: <3117@emory.uucp> Reply-To: braner@tcgould.tn.cornell.edu (braner) Organization: Cornell Theory Center, Cornell University, Ithaca NY Lines: 33 In article <3117@emory.uucp> riddle@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