Article View: comp.sys.next.programmer
Article #1310Re: cc problem and cure: comparison among doubles can produce wrong results
From: Mark Wagner
Date: Fri, 06 Nov 1992 00:56
Date: Fri, 06 Nov 1992 00:56
32 lines
1657 bytes
1657 bytes
> > However, given that the conversion
> > algorithm from external to internal format is the same, I would
> argue that
> > ALL instances of the SAME decimal value will result in the SAME
> binary
> > floating point value internally and can therefore be compared for
> equality
> > with impunity.
>
> Actually, it's worse than this. Sometimes floating point values can be
> different depending on whether or not they've been in and out of
> registers, etc. Moral: Use something like this:
>
> #define EQU(_x,_y) (((_x) > (_y)) ? (((_x)-(_y))<EPSILON) :
> (((_y)-(_x))<EPSILON))
> #define EPSILON .00001 // or whatever
One can use the -ffloat-store option to "cure" this problem but at some
cost in terms of performance (depending, of course, on the application).
-ffloat-store
Don't store floating-point variables in registers.
This prevents undesirable excess precision due to the
floating registers keeping more precision than a double
is supposed to have.
For most programs, the excess precision does no harm,
but a few programs rely on the precise definition of
IEEE floating point. Use -ffloat-store for such pro-
grams.
mwagner@next.com
Technical Details
Message-ID:
<5773@rosie.NeXT.COM>Path:
arc1-ca.pugleaf.net!sparky!uunet!rosie!NeXT.COMReferences:
<1ccm61INNhoq@agate.berkeley.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!rosie!NeXT.COM From: Mark_Wagner@NeXT.COM (Mark Wagner) Newsgroups: comp.sys.next.programmer Subject: Re: cc problem and cure: comparison among doubles can produce wrong results Message-ID: <5773@rosie.NeXT.COM> Date: 6 Nov 92 00:56:19 GMT References: <1ccm61INNhoq@agate.berkeley.edu> Sender: news@NeXT.COM Lines: 32