Should we compare floating point numbers for equality against a *relative* error?

前端 未结 5 1408
闹比i
闹比i 2020-11-30 09:13

So far I\'ve seen many posts dealing with equality of floating point numbers. The standard answer to a question like \"how should we decide if x and y are equal?\" is

<
相关标签:
5条回答
  • 2020-11-30 09:28

    The problem is that with very big numbers, comparing to epsilon will fail.

    Perhaps a better (but slower) solution would be to use division, example:

    div(max(a, b), min(a, b)) < eps + 1
    

    Now the 'error' will be relative.

    0 讨论(0)
  • 2020-11-30 09:33

    It all depends on the specific problem domain. Yes, using relative error will be more correct in the general case, but it can be significantly less efficient since it involves an extra floating-point division. If you know the approximate scale of the numbers in your problem, using an absolute error is acceptable.

    This page outlines a number of techniques for comparing floats. It also goes over a number of important issues, such as those with subnormals, infinities, and NaNs. It's a great read, I highly recommend reading it all the way through.

    0 讨论(0)
  • 2020-11-30 09:41

    Using relative error is at least not as bad as using absolute errors, but it has subtle problems for values near zero due to rounding issues. A far from perfect, but somewhat robust algorithm combines absolute and relative error approaches:

    boolean approxEqual(float a, float b, float absEps, float relEps) {
        // Absolute error check needed when comparing numbers near zero.
        float diff = abs(a - b);
        if (diff <= absEps) {
            return true;
        }
    
        // Symmetric relative error check without division.
        return (diff <= relEps * max(abs(a), abs(b)));
    }
    

    I adapted this code from Bruce Dawson's excellent article Comparing Floating Point Numbers, 2012 Edition, a required read for anyone doing floating-point comparisons -- an amazingly complex topic with many pitfalls.

    0 讨论(0)
  • 2020-11-30 09:42

    Most of the time when code compares values, it is doing so to answer some sort of question. For example:

    1. If I know what a function returned when given a value of X, can I assume it will return the same thing if given Y?

    2. If I have a method of computing a function which is slow but accurate, I am willing to accept some inaccuracy in exchange for speed, and I want to test a candidate function which seems to fit the bill, are the outputs from that function close enough to the known-accurate one to be considered "correct".

    To answer the first question, code should ideally do a bit-wise comparison on the value, though unless a language supports the new operators added to IEEE-754 in 2009 that may be less efficient than ideal. To answer the second question, one should define what degree of accuracy is required and test against that.

    I don't think there's much merit in a general-purpose method which regards as equal things which are close, since different applications will have differing requirements for both absolute and relative tolerance, based upon what exact questions the tests are supposed to answer.

    0 讨论(0)
  • 2020-11-30 09:51

    As an alternative solution, why not just round or truncate the numbers and then make a straight comparison? By setting the number of significant digits in advance, you can be certain of the accuracy within that bound.

    0 讨论(0)
提交回复
热议问题