'IEEE-754 floating point comparisons with special cases
Solution 1:[1]
The relevant portion of the IEEE 754-2008 standard, and a late 754-2019 draft, is:
Four mutually exclusive relations are possible: less than, equal, greater than, and unordered. The last case arises when at least one operand is NaN. Every NaN shall compare unordered with everything, including itself. Comparisons shall ignore the sign of zero (so +0 = ?0). Infinite operands of the same sign shall compare equal.
From this, we see that none of the comparisons with a NaN would yield less than or greater than, and therefore any test of less than or greater than should yield false, so the results in the table conform in this regard.
Similarly, the comparisons of an infinity to the same infinity and of either zero to either zero should yield false, so the table conforms in this regard too.
The standard does not explicitly detail comparisons of other values; they are inherited from ordinary arithmetic, and we can see the table conforms in this regard too.
See the links in the Standards section of the Wikipedia IEEE 754 page for sources for official versions of the standard.
Solution 2:[2]
The standard says:
nancomparisons are always evaluated as false (evennan == nan)+inf > xis true ifxis neither+infnornan, and false otherwise-inf < xis true ifxis neither-infnornan, and false otherwise
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eric Postpischil |
| Solution 2 | bbbbbbbbb |

