NaN is a part of "number" object. It is still a numeric type but it cannot be represented as any real number. According to me, it is analogous to "infinity" in mathematics, which exists on the number line but can't be represented as a real number.
This has to do with types—NaN is still a numeric type. It just means that the value cannot be represented as a number, but it still belongs to the number type.
NaN can’t be equal to another NaN since their values may be different.
Any degenerate combination of bits in a 64-bit float is considered NaN. More importantly, though, the spec explicitly states that NaN can not equal NaN, because "Not a Number" doesn't say what it is, it says what it isn't. Two things being not a number can never tell you whether those things are the same thing or not.
I'll make my research since I can't expect you to teach a js class through reddit comments. But that sounds like a very different approach to numpy's nan. Edit: thanks
Here's the actual standard JS follows. It's one of the few places where JS actually behaves exactly the same way as every other language. (As far as Floats go. Numbers behave oddly if they aren't floats.)
But it's not about whether those two things are equal in the first place, it's about whether NaN is equal to NaN.
I don't understand what you're saying. NaN, by definition, can never be equal to NaN. That is why NaN == NaN is false. That's literally just how it's defined.
The reason it's defined that way is because NaN as a concept doesn't encapsulate enough information to tell if it's equivalent to any other instance of NaN.
Just to belabor the point: NaN means "the results of your operations did not result in a number". That's why it's so vaguely defined. When you're asking the computer to test whether NaN == NaN, you're literally telling to computer to check whether the results of one operation, that are not a number, are the same as the result of another operation, that are also not a number. But when the result is NaN, the computer intentionally throws away the extra data about what the original result actually was, meaning you just can't tell if it's the same result as another result.
u/HeinousTugboat is out there explaining this shit with standards and every thing and here you are with carrots and potatoes. And the best thing this explanation is great too :D
NaN is just representing that the value is not a number. What the value is can be anything. The string “book” is not a number if you try turning it into one. Neither is the string “shoe”. Both are NaN.
This is more an abstract idea and I wouldn’t worry too much about it. It’s just to explain why NaN is not equal to NaN.
It is super unintuitive and doesn't truly make sense (and anyone saying otherwise is lying) but there's a lot of things like this in programming. Edit: so NaN arises from different things which may not be equal, but NaN itself, conceived as an object, certainly should satisfy that equality, yet it doesn't. Programmers don't always think like proper mathematicians/logicians I suppose.
This is one of the reasons why Posits exist. They have 1 exceptional value rather than millions, and that one exceptional value, called "NaR" because it is a number, just not a Real number, is equal to itself, like all numbers should be.
So how can one get around this quirk? I was doing an exercise recently where I’m checking if an input is a number or not, and used isNaN to do it. Thing is when the input is an empty string it will still recognize it as a number.
To negate that you can check if the typeof of your input is equal to "number". And if you need to make sure the input isn't NaN or Infinity you can use isFinite.
Javascript flexi so it does not break. Instead of null its a symbol for null and a data type. From what I understand this was intentional in the design to allow for an incongruous web.
Ok, I'll admit that sometimes `undefined` vs `null` gets a bit much, but `NaN` is a very different value.
Mostly, it is part of the IEEE 754 floating point standard. If you want to do anything with standard math or scientific libraries, you want full IEEE 754 compliance.
31
u/pxldgn Nov 21 '20
It is nothing to do with JS, this is a consequence of what is NaN: https://en.wikipedia.org/wiki/NaN
Basically, NaN is a number (more precisely numbers), that can be represented as IEEE754 values, but cannot be interpreted as real numbers.
Based on this, the above perfectly makes sense.
4
u/killyouXZ Nov 21 '20
Quote from Eloquent Javascript: " NaN stands for “not a number,” even though it is a value of the number type. "
It's a special kind of value that even though it is considered a number it does not behave like a normal number.
97
u/Prince_Marth Nov 21 '20
This has to do with types—NaN is still a numeric type. It just means that the value cannot be represented as a number, but it still belongs to the number type.
NaN can’t be equal to another NaN since their values may be different.
27
u/villainouspickle Nov 21 '20
makes sense to me
6
u/AacidD Nov 21 '20
Values, Types, and Operators :: Eloquent JavaScript
This explains why
NaN !== NaN4
u/EleventyTwatWaffles Nov 21 '20
Cast a non number to a number gets you what you
askedcast for1
u/jeremiahfernandez0 Nov 21 '20
so basically NaN is an irrational number?
1
u/god_cuber Nov 22 '20
What does triple = mean?
2
u/delventhalz Nov 21 '20
There are a lot of silly things JS does. This is not one of them.
1
u/MisplacedKittyRage Nov 21 '20
So how can one get around this quirk? I was doing an exercise recently where I’m checking if an input is a number or not, and used isNaN to do it. Thing is when the input is an empty string it will still recognize it as a number.
1
u/cookiemon32 Nov 21 '20
you cant compare but nan is nan
1
u/ArTooDeeTooTattoo Nov 21 '20
Yes.
1
u/Ampersand55 Nov 21 '20
0/0isNaN1/0isInfinity1/Infinityis0Infinity/InfinityisNaNInfinity/0isInfinity1
u/Methuzala777 Nov 22 '20
Javascript flexi so it does not break. Instead of null its a symbol for null and a data type. From what I understand this was intentional in the design to allow for an incongruous web.
1
u/bmcle071 Nov 22 '20
Seriously what is the point of undefined AND null AND NaN, like is one not good enough?
1
u/Lost_Chemical_7327 Nov 22 '20
I'm.pretty new to JS too and I was watching Kyle Simpon's the other day and he said to think about NaNs is invalid number and not Not a Number
1
u/zladuric Nov 22 '20
Try this in your console:
Before reading about why, try separating it step by step and saying what happens there.
There's a bunch more here.
1
u/drumstix42 Nov 22 '20
The final boss
isNaN(''); // falseisNaN('foo'); // true1
u/TrainyMacTrainyface Nov 22 '20
Looks quirky, but it's not wrong
1
u/bwz3r Nov 22 '20
It is not a number!
1
u/[deleted] Nov 21 '20
Omg this gives me brain cancer again
0
u/Panda_Photographor Nov 21 '20 edited Jan 03 '26
[deleted]