Q: The relevant IEEE standard defines a numeric constant NaN
(not a number) and prescribes that NaN should compare as not equal to itself. Why is that? All the languages I'm familiar with implement this rule. I am sure there is a good reason why NaN comparing as equal to itself would be bad. I just can't figure out what it is.
Why is NaN not equal to NaN?
There's a very easy answer to this: NaN
is supposed to indicate that there's an error in your math calculations. If NaN === NaN
were true, then NaN
could easily be converted back into a number like so:
If NaN === NaN
then z
would be 1, hiding that there was a problem in the calculation of both x
and y
. Instead, z
is NaN
, which should indicate that a problem happened somewhere along the line.
Ian