Synopsis: | Floating point values shall not be compared using the == or != operators |
Language: | C++ |
Severity Level: | 2 |
Category: | Portability |
Description: |
Due to rounding errors, most floating-point numbers end up being slightly imprecise. As long as this imprecision stays small, it can usually be ignored. However, it also means that numbers expected to be equal (e.g. when calculating the same result through different correct methods) often differ slightly, and a simple equality test fails. For example: float a = 0.15 + 0.15 float b = 0.1 + 0.2 if (a == b) // can be false! Solutions to this problem that are not correct include: !(a < b) && !(a > b) and Please read [Dawson] to understand how to solve this issue.fabs(a - b) < Double.Epsilon |
Literature References: |
Dawson |