floating point comparisons illegitimate?

uunet!cup.portal.com!PLS uunet!cup.portal.com!PLS
Thu Mar 21 21:34:01 PST 1991


> Date: Fri, 15 Mar 91 07:37:00 PST
> From: dghavalidgh.com (David G. Hough on validgh)
> Subject: floating-point comparisons illegitimate?
> 
> 
> While in general I don't think programmers should expect 
> 	A = expr
> 	if (A .eq. expr)
> to always be true, I don't see any reason to expect
> 	A = B
> 	if (A .eq. B)
> to fail. There's nothing illegitimate about comparison of floating-point valu
e
> s,
> in general, and I don't see how programs can avoid them efficiently.
> 

I never said that floating comparisons in general are illegitimate. Just that
compares for equality are. Floating point equality comparisons should always
be done as ABS(a-b).LT.tolerance, IMHO. I've often wished for an operator
and a machine instruction that did that, while avoiding the overflow problems
inherent in actually computing a-b.

David's asking for different treatments for the two examples above amounts to
saying that there is some fundamental difference between B and expr. In a good
optimizing compiler, I don't see that there is. B may be the result of a
previous expr that still is stored in an overlength register. Unless the 
compiler is good enough to optimize the IF out of existance, I don't see how
you can avoid these problems at an acceptable cost.

Perhaps I have too much experiene with non-IEEE architectures. I've seen 
machines where 
    if (A .eq. A) 
could be false, because of interactions between automatic normalization and
overlength registers. Again, IMHO, testing for actual equality between 
floating point operands isn't good practice, and it certainly isn't portable.

    ++PLS



More information about the Numeric-interest mailing list