If operator< works properly for floating-point types, why can't we use it for equality testing?

前端 未结 5 1339
借酒劲吻你
借酒劲吻你 2021-01-01 16:55

Properly testing two floating-point numbers for equality is something that a lot of people, including me, don\'t fully understand. Today, however, I thought about how some s

5条回答
  •  -上瘾入骨i
    2021-01-01 17:06

    Float and double are both in the binary equivalent of scientific notation, with a fixed number of significant bits. If the infinite precision result of a calculation is not exactly representable, the actual result is the closest one that is exactly representable.

    There are two big pitfalls with this.

    1. Many simple, short decimal expansions, such as 0.1, are not exactly representable in float or double.
    2. Two results that would be equal in real number arithmetic can be different in floating point arithmetic. For example, floating point arithmetic is not associative - (a + b) + c is not necessarily the same as a + (b + c)

    You need to pick a tolerance for comparisons that is larger than the expected rounding error, but small enough that it is acceptable in your program to treat numbers that are within the tolerance as being equal.

    If there is no such tolerance, it means you are using the wrong floating point type, or should not be using floating point at all. 32-bit IEEE 754 has such limited precision that it can be really challenging to find an appropriate tolerance. Usually, 64-bit is a much better choice.

提交回复
热议问题