2 ms·
And your point is? The idea of NaN is that it represents an unknown value, because it essentially says "we don't really know what the result is, but it surely
by pgaddict 11y ago
And your point is?
The idea of NaN is that it represents an unknown value, because it essentially says "we don't really know what the result is, but it surely is not a number". And if you have two unknown values, you can hardly check they are equal.
That's why NaN is similar to NULL in SQL, because the concept is basically the same. The difference of course is that C does not have NaN for all the other data types, so it's impossible to say (NaN == NaN) is unknown, because bool, char (or whatever type you use for true/false) does not have the equivalent for NaN. So the next sane option is "false".
The fact that the representation for NaN is the same (so memcmp of two NaN values) is a completely different thing.
- kazinator 11y ago> And if you have two unknown values, you can hardly check they are equal. If you want two instances of unknown not to be equal, then you can arrange that by actually instantiating different objects with a different identity to represent these unknowns (and print them differently to make it clear: NaN0, NaN1, ... or whatever. (Yet, if one instance of those objects is propagated into two variables, those variables should still compare equal: NaN0 != NaN1, but NaN0 == NaN0.) > The fact that the representation for NaN is the same (so memcmp of two NaN values) is a completely different thing. It tells us it's the same object. In fact, if x is NaN, even x == x fails, and then we have do not even have two bit patterns to compare: it's just one bit pattern. One object at one address, not equal to itself. In other words, this is garbage that violates "A is A": an object is not itself. This is the work of imbeciles who chose to be ignorant of the philosophical underpinnings of what they are doing. That some features of SQL were produced by similar such is neither here nor there.
- rcthompson 11y agoNaN 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.