Floating point non-exactness
carlos at urz.unibas.ch
carlos at urz.unibas.ch
Tue Jul 31 19:18:03 AEST 1990
Oooops, sorry for message nr. 2653, it was our first try ever....
Hope this time works......
In article <622 at .tetrauk.UUCP>, rick at .tetrauk.UUCP (Rick Jones) writes:
> fpcmp (double a, double b)
> returns 0 if a equals b within the reliable precision,
> else returns 1 if a > b, or -1 if a < b
>
> Real problem: how do you write fpcmp() ?
Since there is only a finite quantity of fp numbers on the computer,
an Increase(x) resp. Decrease(x) (to get the next resp. previous fp
of x) procedure for fp numbers could do it. The tolerance that you
build in your function fpcmp() depends on the fp system you use.
You can implement the above functions very easily for IEEE fp formats:
Just regard the bit-sequence representing your fp number as an integer,
and increase resp. decrease it by 1. The new integer (interpreting
it as double) is the next resp. previous possible fp number.
This holds automatically in case of changes to other exponents and
changes between normalized and denormalized numbers.
You must only watch out for a change in sign and that decreasing
negative numbers means increasing the mantissa (and exponent).
> Can you ensure that "fpcmp (a, b)" and "fpcmp (a-b, 0.0)" yield the same resul
t
> when a and b are very close?
Yes, if your fp system conforms IEEE Std. 754 due to the existence of
denormalized numbers.
Good luck, Stefan & Carlos
More information about the Comp.lang.c
mailing list