Floating Point Expectations
Andrew P. Mullhaupt
amull at Morgan.COM
Fri May 25 10:55:31 AEST 1990
In article <411 at yonder.UUCP>, michael at yonder.UUCP (Michael E. Haws) writes:
> Is it reasonable to expect that the following code should
> work the same on all platforms, and produce "good" results?
>
> if ((45.0 / 100.0) == .45)
> printf("good\n");
> else
> printf("bad\n");
No. Thou shalt not compare floating point results for equality.
They might depend on compiler options for optimization, etc. even
on the same machine.
> I remember seeing somewhere (possible a different group) an article which
> made reference to some software which analyzed the accuracy of floating
> point arithmetic on whatever host it was running. Anyone know where
> this code can be found?
You may be referring to paranoia, which I recommend for investigating
your floating point reliability. However, it does not work by doing
computations and then simply comparing results to fixed values. It
usually computes various relationships which should be true among its
results and reports on discrepancies.
You might wonder if there are clever ways around this kind of problem,
like subtracting and comparing to zero, but it if you are expecting to
get the code to agree bit for bit across "all platforms", you're going
to have to something like resorting to emulating the floating point
arithmetic in integer C code. Consider that IBM 360 style floating point
is not bit-normalized, and that Cray double precision is 64 bit, etc.
Later,
Andrew Mullhaupt
More information about the Comp.unix.wizards
mailing list