>
> just a numerical question: to test the equality of two floating point numbers
> 'a' and 'b', which of these 2 expressions you prefer:
>
> abs(b-a) <= k * spacing(a)
>
> abs(b-a) <= k * a * epsilon(a)
^ abs(a)
>
>
> I've seen recommendations for the former, but I tend to prefer the latter, since
> spacing(a) varies with step functions, while a*epsilon(a) is linear with respect
> to 'a' ( a refinement is max(a*epsilon(a),tiny(a)) )
>
> Also, is there a recommended value for k ?
>
> Pierre
For floating-point "approximately equal" I tend to use a variation
on the second formula:
(B-A)**2 <= (2*K)**2 * (A**2 + B**2)
which avoids difficulties with your second formulation when A is
itself zero. I choose K's which is still appropriate after allowing
for conversion between different binary machine formats (including
IBM mainframe -- ugh!!) and after a bit of round-off error:
(2*K)**2 = 1.0E-10 for REAL, and = 1.0E-20 for DOUBLE PRECISION.
Moreover, this formulation avoids two absolute-value calls (which may
lead to pipeline stalls on microprocessors, so on some hardware may
be faster but will be slower on other hardware with predicated
instructions that may be used to construct the ABS()'s -- e.g. Alpha
or POWER).
Carlie J. Coats, Jr. [log in to unmask]
MCNC Environmental Programs phone: (919)248-9241
North Carolina Supercomputing Center fax: (919)248-9245
3021 Cornwallis Road P. O. Box 12889
Research Triangle Park, N. C. 27709-2889 USA
"My opinions are my own, and I've got *lots* of them!"
|