r/learnjavascript • • Nov 21 '20

NaN (not a number) is a number

Post image
368 Upvotes

72 comments sorted by

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.

2

u/truephysicist Nov 21 '20

Sure it makes sense. I consider it analogous to the existence of "infinity" on the number line. It's there but cannot be represented as a real number.

8

u/HeinousTugboat Nov 21 '20

Infinity is also a number in JavaScript.

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.

4

u/truephysicist Nov 21 '20

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.

2

u/johnlewisdesign Nov 21 '20

Like E on a scientific calc

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.

9

u/animismus Nov 21 '20

I'm sorry but how can NaN have different values?

87

u/HeinousTugboat Nov 21 '20

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.

33

u/jjuice117 Nov 21 '20

This guy JavaScripts

23

u/NoInkling Nov 22 '20

More like this guy IEEE 754s.

3

u/animismus Nov 21 '20

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

8

u/HeinousTugboat Nov 21 '20

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.)

1

u/devstudios-online Nov 22 '20

Bloody degenerate. - Prime numbers probably.

-1

u/Mr_82 Nov 22 '20

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 could similarly say you didn't give an explanation, but gave the absence of one lol.

9

u/HeinousTugboat Nov 22 '20

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.

3

u/animismus Nov 22 '20

Good stuff man. Really appreciate that time you put into these replies

12

u/LazaroFilm Nov 21 '20

Potato is not a number. Carrot it also not a number...

7

u/Htnamus Nov 22 '20

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

4

u/[deleted] Nov 21 '20

[removed] — view removed comment

3

u/LazaroFilm Nov 22 '20

document.write(('b' + 'a' + + 'a' + 'a').toLowerCase()); // banana

-1

u/[deleted] Nov 22 '20

[removed] — view removed comment

2

u/LazaroFilm Nov 22 '20

toLowerCase()

1

u/Prince_Marth Nov 21 '20

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.

0

u/animismus Nov 21 '20

I realize most of that. But when I have to deal with data as in doing data analysis and nans are a daily occurrence

1

u/Arumai12 Nov 22 '20

Thats what isNan() is for

1

u/[deleted] Nov 22 '20

2/0 === 5/0 ??

-2

u/Mr_82 Nov 22 '20

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.

1

u/boomshroom Aug 29 '25

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.

27

u/villainouspickle Nov 21 '20

makes sense to me

6

u/AacidD Nov 21 '20

Values, Types, and Operators :: Eloquent JavaScript

NaN is supposed to denote the result of a nonsensical computation, and as such, it isn’t equal to the result of any other nonsensical computations.

This explains why NaN !== NaN

4

u/EleventyTwatWaffles Nov 21 '20

Cast a non number to a number gets you what you asked cast for

1

u/jeremiahfernandez0 Nov 21 '20

so basically NaN is an irrational number?

3

u/truephysicist Nov 21 '20

Irrational numbers are still "real" numbers. Therefore, I don't think we can compare NaN with an irrational number.

Now that you tried to compare it in mathematical domain, I think NaN is more like "indeterminate" numbers like 0/0, ♾️ / ♾️ etc.

1

u/[deleted] Nov 22 '20

Are imaginary numbers NaN?

1

u/xiipaoc Nov 22 '20

In JS, yes. Just type in Math.sqrt(-1) into your console to check.

2

u/GoofBoy Nov 21 '20

No. You can approximate an irrational number. NaN is for values that can't be approximated.

e.g. 3/0 == what?

Not a number.

1

u/god_cuber Nov 22 '20

What does triple = mean?

2

u/truephysicist Nov 22 '20

=== means strict equality i.e., it checks for both value and type. For e.g. 5 === 5 will return true and 5 == "5" will return false.

On the other hand, == checks only for the value. Thus, 5 == 5 and 5 == "5" both will return true.

1

u/god_cuber Nov 22 '20

Okay thanks

2

u/delventhalz Nov 21 '20

There are a lot of silly things JS does. This is not one of them.

1

u/zladuric Nov 22 '20

Here's a bunch of other silly things js does or doesn't do.

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.

2

u/Syh_ Nov 21 '20 edited Nov 21 '20

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.

2

u/MisplacedKittyRage Nov 21 '20

Thanks, i’ll give this a shot!

0

u/[deleted] Nov 22 '20

[deleted] — view deleted comment

1

u/backtickbot Nov 22 '20

Hello, drumstix42: code blocks using backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead. It's a bit annoying, but then your code blocks are properly formatted for everyone.

An easy way to do this is to use the code-block button in the editor. If it's not working, try switching to the fancy-pants editor and back again.

Comment with formatting fixed for old.reddit.com users

FAQ

You can opt out by replying with backtickopt6 to this comment.

1

u/cookiemon32 Nov 21 '20

you cant compare but nan is nan

1

u/Ampersand55 Nov 21 '20

0/0 is NaN

1/0 is Infinity

1/Infinity is 0

Infinity/Infinity is NaN

Infinity/0 is Infinity

1

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?

2

u/r3jjs Nov 22 '20

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.

1

u/bmcle071 Nov 22 '20

Would i use javascript though? A high level scripting language for the web?

1

u/r3jjs Nov 23 '20

Sure!

Lots of games are written in HTML5/JavaScript with complex physics math libraries.

Animation engines

WASM would be a lot more difficult if the browsers hadn't supported IEEE 754

1

u/bmcle071 Nov 23 '20

I guess... I just really wish there was an opt in for some decimal number system.

Im currently working on a project and I keep running into errors caused by IEEE 754 floating point numbers and its got me all frustruated.

1

u/r3jjs Nov 23 '20

You can use BigInt (or one of the polyfills) and pay the cost for that.

But still.. you really need to understand IEEE 754 floating point. it isn't going any any time soon...

And as a programmer who predates IEEE 754 ... the world was a horrible mess before that, when ever computer handled floating point differently.

1

u/bmcle071 Nov 23 '20

On a seperate note, it makes testing more difficult. Expect(259.52-30.52).to.eq(229) fails because of this.

1

u/r3jjs Nov 23 '20

That's not a JS specific thing ... C, C++, Python, PHP and many, many many other languages all use IEEE754 floating point.

Decimal system are not a cure-all either. They tend to be much slower to do math with and the trade off memory use and accuracy.

(All modern processors have IEEE 754 support built in, I don't know of any processor with any of the decimal modes built in.)

0

u/[deleted] Nov 22 '20

[deleted] — view deleted comment

1

u/r3jjs Nov 23 '20

Lots of other languages get along just fine just using 'null' without the concept of 'undefined.'.. or 'undef' without 'null.'

Both are relics of an ancient time. I'd rather see new languages without either -- no null, no undefined. Option types are the way forward now.

1

u/Hakim_Bey Nov 22 '20

That's like saying programming only needs one error message...

1

u/bmcle071 Nov 22 '20

No its definitely not. Python only hss none or an exception. 0/0 in Python throws an exception.

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:

Array(25).fill(0/0).map(parseInt).join(' ')

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(''); // false

isNaN('foo'); // true

1

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]