Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!jarthur!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c Subject: Re: == comparison of two doubles on an 80386 not right? Keywords: equality Message-ID: <640@taumet.com> Date: 28 Mar 91 16:39:16 GMT References: <1159@gistdev.gist.com> Distribution: comp Organization: Taumetric Corporation, San Diego Lines: 24 flint@gistdev.gist.com (Flint Pellett) writes: >I came across something today which has me quite baffled: some old >code I found was trying to test equality of two successive long >values by testing for equality of a union'ed double on top of them. >Such a thing obviously isn't portable, (and I fixed it to use integer >comparisons) but what has me confused is what it _was_ doing: it works >differently on a 3B2 from the way it works on a 386 (both SYSV UNIX). >If the endian-ness difference is the explanation, I don't see how. Floating-point formats often reserve several bit patterns for special purposes. Some of these patterns are distinguished only by the most significant bits. If two numbers are the same in these bits (which could be at either end of a number, depending on the endian-ness), they could compare equal even though other bits differ. Similarly, some bit patterns could cause an exception, and some will always compare unequal even if the bit patterns are identical. As you noted, the code was not at all portable, and may not have worked properly even on the machine where it was originally written. It's not nice to fool the compiler. -- Steve Clamage, TauMetric Corp, steve@taumet.com