Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!husc6!seismo!mcvax!unido!qtecmuc!ger From: ger@qtecmuc.UUCP Newsgroups: comp.lang.c Subject: Re: Floating point equality - (nf) Message-ID: <16800001@qtecmuc.UUCP> Date: Mon, 16-Mar-87 12:45:00 EST Article-I.D.: qtecmuc.16800001 Posted: Mon Mar 16 12:45:00 1987 Date-Received: Wed, 18-Mar-87 03:50:24 EST References: <4915@brl-adm.UUCP> Lines: 57 Nf-ID: #R:brl-adm:-491500:qtecmuc:16800001:000:1703 Nf-From: qtecmuc!ger Mar 16 18:45:00 1987 I had a similar problem today, with Microsoft C 4.0 on an IBM-AT compatible. The two programs #include double a=0.01, b=0.01; int c; main() { a=floor(1.e5*a/0.5)/1.e5; if(a!=b) printf("not equal"); else printf("equal"); } /****/ and /****/ /* declarations as above ... */ main() { a=floor(1.e5*a+.5)/1.e5; c=1; /* This is the only difference */ if(a!=b) printf("not equal"); else printf("equal"); } /*****/ yielded different results. the first one says: 'Not equal', while the other one prints out 'equal'. With a debugger I found the reason for this strange behaviour: The compiler tries to hold the result of the floor... statement in the 80 bit register of the 8087 or the emulator and loads just b into another register for the comparison, if there is no other statement inbetween. Thus the compiler compares the 80-bit value resulting from previous operations and b, which is extended from 64 bit double precision to the internal 80 bit format, giving a!=b. If there is another statement like 'c=1' or anything else between the evaluation of floor .... and the comparison, the result of floor... is written to the variable 'a' correctly in 64 bit format, then loaded and extended again to 80 bit format, which results in a==b. My question: is this behaviour legal C ???? I always thought, double variables could only be compared with double precision, not with the precision of some internal format, depending of other statements, like 'c=1'. Too much optimization in my eyes. Gerhard Pehland Quantec Tonstudiotechnik, Munich W-Germany UUCP: ....!mcvax!unido!qtecmuc!ger Sorry for my english...