Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!dayton!rosevax!sds!dave From: dave@sds.UUCP Newsgroups: comp.lang.c Subject: Re: ANSI really does want you to == float values. Message-ID: <142@sds.UUCP> Date: Wed, 15-Apr-87 16:31:48 EST Article-I.D.: sds.142 Posted: Wed Apr 15 16:31:48 1987 Date-Received: Sat, 18-Apr-87 00:05:50 EST References: <6909@watmath.UUCP> Distribution: comp Organization: SciCom Data Services, Minnetonka, MN Lines: 42 Summary: If correctly implemented, this is fine In article <6909@watmath.UUCP>, rbutterworth@watmath.UUCP (Ray Butterworth) writes: > Now does that mean that HUGE_VAL (and -HUGE_VAL) are defined so > that the comparisons will work correctly regardless of registers, > guard bits, optimization, etc.? [...] > > On the other hand if there isn't supposed to be anything special > about these numbers, does that mean that the compiler should > generate correct code for all doubles? [...] > Again, probably not. At least not unless you are willing to put > up with some very inefficient code. (e.g. whenever any two floating > values are compared they are first normalized and stored in temporary > memory somewhere.) I would argue that unless instructed otherwise, the compiler should ALWAYS produce code to normalize floating point numbers before comparisons. Yes, I know this grossly inefficient, but it is correct. Consider: #define PI 3.14159265 static double sin_pi_4; double sin(); sin_pi_4 = sin(PI/4.0); /* some code which doesn't change sin_pi_4 */ if ( sin_pi_4 != sin(PI/4.0) ) printf( "your compiler should be ashamed.\n" ); Since we can (hopefully :-) agree that sin(PI/4.0) is constant, the condition in the if *must* be false; if it's not, I would say the compiler is producing incorrect code. Making the unwary programmer (and we all were at one time), having to know & worry about 80-bit registers vs. 64 bit doubles, guard bits, etc. is absurd. Granted, Joe Number Cruncher Floating Point God may demand that his program not normalize floating point numbers and perform all [reasonable] optimizations; an appropriate compiler directive should be available for experts of this type who need or want every bit of possible speed at the expense of worrying about the floating point hardware. Dave Schmidt [ This space intentionally left blank. ]