6 ms·
NaN doesn't actually represent any value at all, so having it not equal itself its not a violation of "A is A". Sometimes you get NaN because you got infinity,
by rcthompson 11y ago
NaN doesn't actually represent any value at all, so having it not equal itself its not a violation of "A is A". Sometimes you get NaN because you got infinity, or sqrt(-1), or just a really big number (overflow). In this case, there's a unique answer, but it just can't be represented by the number type being used. In this case, you could theoretically implement NaN instances that are equal to themselves but not each other. But NaN also represents things like sin(1/0), which has no meaningful value at all. You can't even say that sin(1/0) equals itself.
Really, the answer for any comparison to NaN shouldn't be true or false, it should be something like NaB, Not A Boolean. This is basically what R does:
> 1 == 1
[1] TRUE
> NaN == NaN
[1] NA
But the major disadvantage of that is that a boolean now has 3 possible states, so it can't be represented in one bit.
- rcthompson 11y ago(Whoops, the formatting of the R output got messed up. It's supposed to show that "1 == 1" returns TRUE, while "NaN == NaN" returns NA.