4 ms·
NaN behavior in JS is defined in the IEEE standard for floating-point numbers and should be the same in any language. Any two values can be less than, equal, g
by mubou 1y ago
NaN behavior in JS is defined in the IEEE standard for floating-point numbers and should be the same in any language.
Any two values can be less than, equal, greater than, or unordered. The > operation, for example, evaluates to true if its operands are greater than. Per IEEE 754, "every NaN shall compare unordered with everything, including itself." By making NaN comparisons unordered, positive comparisons always evaluate as false. "Is 5 > NaN? No. Is NaN > 5? No." Same for ==. But negated comparisons are always true by consequence; since A != B is the same as !(A == B), anything != NaN is true.
One could argue that the spec should have special-cased NaN for the == operator (which as defined is only true if the comparison is equal, and not unordered), but they did not.
- senfiaj 1y agoYeah, you are right. The same thing with 0.2 + 0.1 = 0.30000000000000004
- hajile 1y agoThe biggest number comparison oddity IMO is actually negative zero (in my experience, you usually get one by multiplying a negative number by zero on accident). 0 === -0 //=> true Object.is(0, -0) //=> false