fp equality

David Hough uunet!Eng.Sun.COM!David.Hough
Tue Nov 30 10:25:49 PST 1993


> It amazes me that there are still people (not necessarily meaning RFG)
> who think that comparing f.p. representations for exact equality is a
> reasonable thing to be doing.

This statement is reasonable from the point of view of determining whether
two uncertain quantities are probably equal - e.g. if x and y are subject
to error bounds bx and by, then it may be that 

	if (x == y)

is not as good an expression of an algorithm's intent as

	if (fabs(x-y) <= (bx+by))

But if x and y are both considered to be exact, then these two expressions
are equivalent, and the first is a lot more efficient.    And there are more
general occasions where equality tests are appropriate, as in computing
divided differences:

	if (x == y) 
		z = fprime(x) ;
	else
		z = (f(x)-f(y))/(x-y) ;

If f(x)-f(y) can be computed with low relative roundoff error when x is close
to y, one would prefer to do so if the computation of the derivative
fprime is very much more expensive.
Otherwise one might choose a test like

        if (fabs(x-y) <= (bound))

if a suitable bound could be determined a-priori; more typical perhaps would
be code like

		fx = f(x) ;
		bfx = ... bound on fx computed simultaneously with fx ...
		fy = f(y) ;
		bfy = ... bound on fy computed simultaneously with fy ...
		d = fx - fy ;
		if (abs(d) <= (fx+fy))
			z = fprime(x) ;
		else
			z = d/(x-y) ;
		fi

In the particular case of evaluating functions with a singularity, 
it's certainly appropriate to test the arguments
for exact equality with the singular point.   Elementary transcendental
function libraries do this all the time.   Any uncertainty in the argument
is irrelevant in this case; the standard library, not knowing all the
uses to which it will be put, must assume the arguments are exact and return
a result as close to the exact result as the set of representable numbers
and economics allow.



More information about the Numeric-interest mailing list