Floating point non-exactness
Andrew Koenig
ark at alice.UUCP
Wed Aug 1 22:46:24 AEST 1990
In article <5467 at quanta.eng.ohio-state.edu>, rob at baloo.eng.ohio-state.edu (Rob Carriere) writes:
> In other words, usually what you want is something like
> int fcmp( double a, double b, double eps )
> {
> if ( fabs( a-b ) < eps ) return 0;
> if ( a > b ) return 1;
> return -1;
> }
Probably not.
The trouble is that if a and b are big enough, then a-b will either be
exactly 0 or will be larger than eps. (Here, `big enough' is approximately
eps * 2^n, where n is the number of significant bits in a floating-point
freaction.) There is also the possibility that a-b might overflow,
in which case the comparison would surely have been valid if done directly.
Floating point arithmetic is *hard*.
--
--Andrew Koenig
ark at europa.att.com
More information about the Comp.lang.c
mailing list