First Haitian Coder In Suriname
This will require additional research to further understand. But I'll post a general explanation to get you started.
2. NaN is a special number value meant to indicate a value known to NOT be a number. It is the only way to identify a value for an invalid calculation without forcing an exception.
Check out these links regarding IEEE 754 on the equality of NaN with itself:
Sorry for the long explanations. I encourage you to do further research of your own for a deeper understanding.
3. The standard for floating point arithmetic (IEEE 754) stipulates that NaN will not preserve reflextivity of equality (NaN==NaN) or comparison operators (<, >, >=, <=). The reasons are a bit more involved than you may care to know.
A simple explanation could be that while NaN states that it is Not a Number, it doesn't state what it is or could be. Since NaN can occur from an infinite number of invalid arithmetic scenarios that should essentially throw an exception, a comparison of equality cannot be asserted.
Example: Although 0/0, 0/"c", and Math.sqrt(-3) all result in NaN, none of the following would make sense:
Math.sqrt(-3) == 0/0
0/0 == 0/"c"
However, notice that inequality can be asserted to be true:
NaN != NaN
Math.sqrt(-3) != 0/0
Truth be told, this is an over simplification that fails to explain the real mathematical axioms considered in the decision of IEEE 754.
Fermi I am envious of your conciseness as I can be a bit long winded. 🤓
One minor correction in your answer for #1:
"because the standard says so."
According to the article below, typeof null returned "object" because of a bug that went undiscovered for a while in the typeof function written in C.
As you can see... this is a rather major bug that never got fixed because it was too late by the time it was discovered and much code on the internet has already been using this check.
typeof null => object
because the standard says so.
Everything which is compared to NaN will always return false. This is part of the standard defined by IEEE-754.
The IEEE-754 also states that 'Numbers' are floats, and NaN is a possible float value, so NaN is of type 'number' (although yes, NaN stands for Not-a-Number :>) .
David Carroll Ah, yes, I missed on that one. I often wonder how many of such major bugs went unfixed due to the sheer amount of code adapting it as a 'feature'.
... and not at all about conciseness! I always hope I could have researched more before posting, but it always ended with me trying to get fast answers.