== comparison of two doubles on an 80386 not right?

Stephen Clamage steve at taumet.com
Fri Mar 29 02:39:16 AEST 1991


flint at 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 at taumet.com



More information about the Comp.lang.c mailing list