Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!cbatt!ihnp4!houxm!hjuxa!catnip!ben From: ben@catnip.UUCP Newsgroups: comp.lang.c,comp.unix.xenix Subject: Are the floating point routines on my machine broken? Message-ID: <442@catnip.UUCP> Date: Sun, 28-Dec-86 23:57:00 EST Article-I.D.: catnip.442 Posted: Sun Dec 28 23:57:00 1986 Date-Received: Tue, 30-Dec-86 04:37:16 EST Organization: The Broder Residence, Holmdel, NJ 07733 Lines: 47 Xref: watmath comp.lang.c:559 comp.unix.xenix:30 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: #includemain(){ 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}/