Are the floating point routines on my machine broken?

ben at catnip.UUCP ben at catnip.UUCP
Mon Dec 29 14:57:00 AEST 1986


I have noticed what I believe is incorrect behavior on the part of the
floating point routines on my machine.  The problem is best demonstrated
with a small program:

#include <stdio.h>

main(){
	double x;

	for (x=0.;x <= 3.;x += .2)
		printf("%f\n",x);
	printf("Final value: %f less 3 is %e\n",x,x-3.);
}

When this program is run, it produces:
0.000000
0.200000
0.400000
0.600000
0.800000
1.000000
1.200000
1.400000
1.600000
1.800000
2.000000
2.200000
2.400000
2.600000
2.800000
Final value: 3.000000 less 3 is 4.440892e-16

It seems to be going through the loop one time less than I expected.
I don't understand this behavior.  3.0 can certainly be represented
precisely in binary, so only the .2 should subject to roundoff error.
The crazy thing is, it appears that the floating point routines are
rounding this number up instead of down!  It is the only explanation
I can think of to explain this output.  Is roundup considered acceptable
behavior for floating point routines?  Or am I somehow misinterpreting
the problem?

-- 

Ben Broder
{ihnp4,decvax} !hjuxa!catnip!ben
{houxm,clyde}/



More information about the Comp.unix.xenix mailing list