New floating point puzzle

Matthew Barkley barkley at unc.cs.unc.edu
Thu Aug 18 12:13:13 AEST 1988


In trying out the problem program from the "floating point puzzle" posting,
I came across some strange behavior in trying to make the program more con-
cise.  Here's what I ran and what I got:


/* Puzzling floating-point program */
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);
	printf("\n");
	printf("x: %x %20.17f\n",x,x);
	printf("y: %x %20.17f\n",y,y);
}
/* Here is the output: (from VAX 11/780)  */

/* x: cccd3ecc 0.10000000149011612 */
/* y: ccc83ecc 0.09999996423721313 */
 
/* x: cccd3ecc  0.00000000000000000 */
/* y: ccc83ecc  0.00000000000000000 */

So, please gurus, tell me where the bug (if any) is.  Is it unreasonable to
expect the 2 sets of output to be the same?  (BTW, the results are more bizarre
if x and y are of type double.)

Matt Barkley                               barkley at unc.cs.unc.edu
Any opinions expressed are not necessarily shared by anyone else, and may 
not even be my own. How an organization can have an opinion is beyond me.



More information about the Comp.lang.c mailing list