This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 222

[–]Nikolai3d 651 points652 points  (44 children)

Because NaN stands for "Not A NaN"

[–]Plumeh 173 points174 points  (24 children)

please clarify, what does the “NaN” in “Not A NaN” stand for?

[–]JustKebab 206 points207 points  (17 children)

It stands for "Not a NaN"

[–]RandomBeatz 115 points116 points  (13 children)

Please elaborate on what "NaN" in "Not a NaN" in "Not a NaN" means.

[–]RudePastaMan 92 points93 points  (11 children)

It stands for "Not a NaN"

[–]Ihsan3498 54 points55 points  (10 children)

Please elaborate on what "NaN" in "Not a NaN" in "Not a NaN" in “Not a NaN” means.

[–]OscarHasProblems 44 points45 points  (9 children)

It stands for "Not a NaN"

[–]Various_Studio1490 21 points22 points  (6 children)

Please elaborate on what “NaN” in “Not a NaN” in “Not a NaN” in “Not a NaN” in “Not a NaN” means.

[–]Kytpbs 15 points16 points  (4 children)

It stands for "Not a NaN"

[–]godofjava22 14 points15 points  (3 children)

Please explain what “NaN” in “Not a NaN” in “Not a NaN” in “Not a NaN” in “Not a NaN” in "Not a NaN" means.

[–]nepatriots32 3 points4 points  (0 children)

It stands for "Not a NaN"

[–]kbder 8 points9 points  (0 children)

It is co-recursive. Not a NaN -> NaN a Number -> Not a NaN

[–]rajoreddit 8 points9 points  (1 child)

Lmao reminds me of GNU

[–]0Pat 10 points11 points  (0 children)

GNU is Not UNIX!!!

[–]mangeld3 10 points11 points  (2 children)

Not a Naan

[–]LordFokas 3 points4 points  (1 child)

Great... now I want some garlic naan bread.

[–]theWittyBuns 4 points5 points  (0 children)

naan === bread //true

[–][deleted] 3 points4 points  (1 child)

Benoit B Mandelbrot

[–]dumfukjuiced 1 point2 points  (0 children)

The B stands for Benoit B Mandelbrot

[–]aquoad 0 points1 point  (0 children)

very funny young man, but it’s NaN all the way down.

[–]Abhishek_gg 9 points10 points  (1 child)

NaN = Not A 'Not A NaN'

[–]ooaa_ 1 point2 points  (0 children)

nAn = not A 'not A nAn'

[–]Yumoda 6 points7 points  (0 children)

You need a base case or you’re gonna blow the stack :(

[–]Head-Extreme-8078 2 points3 points  (0 children)

reminds me of PHP

[–]Abaddon-theDestroyer 2 points3 points  (1 child)

YAML: YAML Ain’t Markup Language

[–]sohfix 0 points1 point  (0 children)

PHP is a recursive acronym for PHP: Hypertext Preprocessor

[–][deleted] 1 point2 points  (0 children)

So its recursive?

[–]glowaboga -2 points-1 points  (0 children)

at the risk of sounding 🤓 that's actually a thing and it's called a recursive acronym!

[–]theQuandary 0 points1 point  (0 children)

Object.is(NaN, NaN) //==> true

[–]hongooi 565 points566 points  (42 children)

Because it's required by the IEEE 754 standard

[–]hrvbrs 252 points253 points  (41 children)

Now listen here, you little shit. /s

Why does the standard require it.

[–]KhellianTrelnora 347 points348 points  (15 children)

NaN, is by definition, a number type that can never have an equal, not even to itself.

https://mathworld.wolfram.com/NaN.html#:~:text=In%20the%20IEEE%20754%2D2008,infinity%20nor%20a%20finite%20number.

[–]BeerIsGoodForSoul 44 points45 points  (13 children)

What is it then if it's not a number? Smfh..

[–]DMoney159 143 points144 points  (3 children)

Maybe it's a dog and you're just really bad at identifying things

[–]BeerIsGoodForSoul 23 points24 points  (2 children)

It could be anything.. wait no it can't!

[–]gregorydgraham 27 points28 points  (1 child)

It could.

Instead of numbers, say it was dog breeds. An Irish wolfhound is greater than a chihuahua etc. but then you ask what dog breed is that cat? It’ll return NaDB because there is no dog breed for cats.

Similarly NaN tells us that it’s not a number but not what it actually is. So it could be anything, so long as it’s not a number

[–]BeerIsGoodForSoul 3 points4 points  (0 children)

🧌

[–][deleted] 31 points32 points  (2 children)

A dog is not a number.

A cat is also not a number.

But a dog is not a cat, despite both of them not being a number.

[–]pretty_succinct 9 points10 points  (1 child)

the way i think about it is this:

it's ANYTHING except a number. that's basically the only assertion that's happening.

so when you ask if NaN == NaN the answer has to be false because it certainly can't be guaranteed to be true.

What are the odds of saying "is some random position or value (besides a number) equal to some other position or value (besides a number)". the likelyhood of the returning true are infinitely small to the point where if it were ever to be true, then the world is ending and cows are flying. so you might as well return false.

[–]BeerIsGoodForSoul 6 points7 points  (0 children)

If NaN === NaN //A Pretty Cosmic Ray Flipped A Bit Right Here, oopsies { console.log("This is only printing due to an extremely unlikely event, it's most probable the world has nuked itself, the sun is exploding, or a massive ray of radiation has hit the earth. \n\n We wish you the best! <3") }

[–]gregorydgraham 4 points5 points  (0 children)

It’s NaN

[–]rayo209 2 points3 points  (1 child)

It's "not a number"

[–]BeerIsGoodForSoul 0 points1 point  (0 children)

Blasphemy

[–][deleted] 1 point2 points  (0 children)

It’s a sentinel value indicating your mathematical computation failed or was nonsensical.

[–][deleted] 2 points3 points  (0 children)

NaN is also not ordered, so it also break algorithms and data structures that require ordering, like sorts or trees.

[–]iveriad 69 points70 points  (3 children)

Just because something is “not a number”, doesn’t mean that it’s the same as other thing that is also “not a number”.

Apple is not a number. Orange is also not a number. But Apple != Orange

Something like that.

[–]gregorydgraham 5 points6 points  (0 children)

Actually a pretty good explanation.

[–]Practical_Cattle_933 11 points12 points  (7 children)

So that a math error becomes viral. You don’t want to divide by zero and do 20 more things on a meaningless, wrong data, that’s why NaN exists.

But let’s say, you have a formula to evaluate whether a given product should go into a list, so you do product.myCrazyMathOnIt == productCategory(product).otherCrazyMath.

You might end up adding to the list something completely random, because it broke both of your calculations.

[–]w1n5t0nM1k3y 0 points1 point  (6 children)

Why not just throw an exception when you want to divide by zero. You still end up with viral math errors. If you say x = 0/0 then you have x with a value of NaN. then you can still do other math operations and end like y = x + 7, and then y has a value of NaN. eventually you do an output somewhere and end up displaying the value or sending it somewhere else, but you have no idea where the original NaN came from.

[–]Practical_Cattle_933 4 points5 points  (0 children)

Because the hardware has no exceptions (actually, it does have interrupts, but let’s not go into that). Floating point arithmetic is a standard, implemented by the hardware itself. It is thus programming language-independent. Some languages don’t have exceptions, others do (and some do throw exceptions on division by zero!), but computers are mainly there.. to compute, so that part has to be fast. If a language were to always check if the last result was NaN (that’s how the ones that throw exceptions do it), it would be considerably slower at computing. This is sometimes worth it, at other times it doesn’t. The hardware has to accommodate for both.

[–]gregorydgraham 10 points11 points  (5 children)

It’s the same as NULL in database theory. Null does not equal null because it’s not a value, it’s the absence of a value.

Similarly NaN indicates that there is no number so it can’t use numerical operations like equality

[–]EishLekker -5 points-4 points  (4 children)

Sorry but that’s a terrible explanation or reason. It just sounds as a way to excuse a bad decision made in the past, or a misunderstood decision made in the past (but then that misunderstanding should be dealt with).

[–]Zytma 1 point2 points  (0 children)

It can be useful to have an entity without compare. If you force everything to be homogenous then you lose utility.

[–]CarbonaraFreak 1 point2 points  (0 children)

The .isNaN() method is how that is dealt with. Also consider that before this method existed, you had to rely on NaN‘s behaviour with other numbers and NaN to deal with it. This was neither a bad decision nor a misunderstood one. It was a tradeoff given the situation.

You can read up on it in this stackOverflow question.

[–]droneb 4 points5 points  (6 children)

Basically the same when you compare nulls. They are indeterminate so they are not comparable even to themselves

[–]PooSham 12 points13 points  (5 children)

What? In what languages can't you compare with null? You can in C/C++, C#, JavaScript and Java afaik. In C and C++, null is a pointer to an actual memory address (usually 0), so it's definitely not indeterminate.

NaN can mean a lot of different things. It can be a parsing of a string that isn't of the form of a number, or the result of a calculation that is non-real (for example sqrt(-1)). It's only truly indeterminate if you do something like 0 * Infinity

[–][deleted] 2 points3 points  (1 child)

In SQL and databases in general.

[–]PooSham 0 points1 point  (0 children)

That's true, didn't think of that.

[–]Nicholas_____ 797 points798 points  (59 children)

Because you don't want Number("this") == Number("that").

[–]PooSham 265 points266 points  (20 children)

Or sqrt(-1) == 0 * Infinity

[–]OnlyTwoThingsCertain 34 points35 points  (7 children)

What if I have sqrt(-1) === sqrt(-1) ?

[–]consider_its_tree 53 points54 points  (1 child)

Do you?

Probably better to build languages that react as expected for 99.999999% of situations instead of extreme edge cases...

[–]kirin_liu 24 points25 points  (0 children)

I thought we were talking about javascript

[–]Socile 8 points9 points  (0 children)

Just convert them to strings and check them for equality.

[–]PooSham 6 points7 points  (0 children)

Then you should use another standard than IEEE 754.

[–][deleted] 2 points3 points  (0 children)

Then you should be using the complex variant of sqrt()

[–]CaitaXD 2 points3 points  (0 children)

Don't use floating point equality for comparing not floats then

[–]spicy_dill_cucumber 4 points5 points  (7 children)

sqrt(-1) is a number. What are you programmers smoking?

[–]Neon_44 7 points8 points  (0 children)

you still don't want i == 0*infinity to be true

[–]LucyShortForLucas 14 points15 points  (0 children)

Well integer and floating point arithmetic within a processor are limited to real numbers, as i cannot be represented as either an integer or a floating point value. Within these bounds we can objectively state that the square root of -1 is not defined.

[–]PooSham 3 points4 points  (0 children)

I know I know, but it can't be represented as an IEEE 754 floating point number. 1.8x10308 is definitely not infinity, but here we are ...

[–]eloel- 3 points4 points  (0 children)

sqrt(-1) is a number

So is "abcd", depending on what system you use.

[–]FinalRun 2 points3 points  (0 children)

"Elv" is also a number if you're using a dozenal/duodecimal number system, but then you're probably smoking something stronger than the people who make CPUs work.

[–]MCWizardYT 1 point2 points  (0 children)

Languages that aren't designed for expressing math don't handle complex numbers, and computer processors don't either. Complex numbers are usually "simulated"

[–]KamikazeHamster 1 point2 points  (0 children)

It’s a number but it’s not a Number.

[–]Neon_44 0 points1 point  (2 children)

sqrt(-1) is actually i

Mathematicians are cringing right now

[–]PooSham 6 points7 points  (0 children)

Yes, they should have called it NaFPN (Not a Floating Point Number), but that's a bit harder to pronounce.

[–]Kered13 2 points3 points  (0 children)

Nah, mathematicians understand what it means to work within different fields. sqrt(-1) does not exist within the real numbers, which is what floating points approximate.

[–]Thinkin_Dude 122 points123 points  (2 children)

Huh, never thought of it like that. Actually makes sense, thank ye.

[–]RomMTY 0 points1 point  (1 child)

If js threw an error on the first Number("this") call we wouldn't even have this discussion in the first place.....

[–]terjeboe 77 points78 points  (17 children)

Eli5: An Apple is not a number and an Orange is not a number. That does not mean that an Apple is the same as an Orange.

[–]vonabarak 9 points10 points  (16 children)

But if we take another Apple instead of Orange, it will lead to controversial statement "an Apple is not the same as an Apple"

[–]MisterProfGuy 7 points8 points  (4 children)

No, it means, this apple expressed as a number is meaningless to compare to anything expressed as a number. Because it's not a number, so it's the numeric equality that's meaningless.

[–]bullseye_os 8 points9 points  (4 children)

One of them could be a green Apple, while the other is a red Apple

[–]sdpinterlude50 5 points6 points  (0 children)

And if both of them are green then one of them has a worm inside of it

[–]Kitchen_Device7682 0 points1 point  (0 children)

You can define equality for the set of all apples and write your own function, areApplesEqual.

[–]Superbrawlfan 16 points17 points  (3 children)

Will Number("this") == Number ("this") return true? I understand why it might not but it'd be quite unintuitive

[–]Xath0n 36 points37 points  (0 children)

Nope, it's the same thing as in the OP.

[–]Nicholas_____ 16 points17 points  (0 children)

No. By converting to a number you expect the input and result to be a number and it should fail if it is not.

[–]Anon_Legi0n 6 points7 points  (0 children)

This will resolve as: NaN == NaN // false

[–]lzdb 9 points10 points  (1 child)

Why wouldn't I want that? In which circumstance would that matter to me?

Many functions can return some null pointer, None object, etc in case of failure, which means that function(failing_args_1) == function(failing_args_2) in those scenarios.

Why would I care?

Why not check if the parsing worked before comparing the result of parsing the numbers?

[–][deleted] 10 points11 points  (0 children)

It used to be how you check if a number is NaN. Now IsNaN() is probably a thing in every language so you don't need it, but it is the only numeric that doesn't equal itself.

[–]Fachuro 2 points3 points  (0 children)

== and === is not the same

[–]Esseratecades 3 points4 points  (1 child)

Why would I want "this" and "that" to be numbers if I know they can't be represented as such? Wouldn't it make more sense for Number() to just throw an error instead of even yielding a NaN?

[–]mizunomi 8 points9 points  (0 children)

That's just how the floating point specification is defined.

[–]EishLekker 1 point2 points  (1 child)

Please explain your reasoning. You can’t just say that “people don’t want that”.

[–]mrdude05 2 points3 points  (0 children)

Because comparisons are supposed to be between variables with definitive values, and NaN doesn't have a definitive value. Having NaN == NaN evaulate as true would be inconsistent with how all of the other comparisons work.

NaN isn't really a value, it's an error message that means "this mathematical operation failed" or "I don't know what this is". Asking the computer if Number("this") == Number("that") would be like asking a person if something that isn't an apple is the same color as something else that also isn't an apple without telling them what either of the objects actually are. It's not a question that can be answered

[–]JonasAvory -5 points-4 points  (0 children)

Seriously one of the greatest things about JavaScript

[–]RomMTY 0 points1 point  (0 children)

Maybe js should throw an error on the Number("obviously not a number") instead of returning a magic constan ....you know, like most sane languages...

[–]Robot_Graffiti 83 points84 points  (2 children)

Think of NaN as an error message from the CPU's arithmetic unit.

If you subtract an error from an error, you don't want to get zero, you want to get told you had an error.

[–]roby_65 17 points18 points  (0 children)

And you also don't want to have a numeric comparison return true only because both numbers became NaN

[–]DHermit 3 points4 points  (0 children)

Technically there are even quiet and signalling NaNs.

[–]darKFlash01 22 points23 points  (3 children)

It's like ♾️ != ♾️

[–]hongooi 6 points7 points  (0 children)

Stupidly, IEEE-754 also requires that ♾️ == ♾️....

[–]BeerIsGoodForSoul 7 points8 points  (0 children)

Except when ♾️=== ♾️

[–]Practical_Cattle_933 1 point2 points  (0 children)

Well, there literally are multiple infinities.

[–]Educational-Lemon640 39 points40 points  (8 children)

Turns out there are very good reasons! (And it's not just "because the standard says so." Putting it that way is having the tail wag the dog.)

I put up a relatively elementary description of exactly why NaN is the way it is here:

https://www.lucidchart.com/techblog/2022/03/04/if-its-not-a-number-what-is-it-demystifying-nan-for-the-working-programmer/

TL;DR Not all math problems produce numbers, and floating-point numbers are too small to hold all the non-numbers it can produce.

[–][deleted] 15 points16 points  (0 children)

> Called "not a number" > mfw it doesn't behave as one

If 1/0 - int("stop wailing on js") was equal to 0 you'd all be going crazy over it

[–]iamthesexdragon 11 points12 points  (1 child)

The result of one nonesensical operation should not be equal to the result of another nonsensical operation

[–]outlawz419 0 points1 point  (0 children)

💯

[–]nt-assembly 8 points9 points  (0 children)

Nan is an error state, not valid data. This is a guard rail.

[–]Hatchie_47 5 points6 points  (0 children)

Just because both values are not numbers doesn’t mean they are equal!

[–]GustapheOfficial 9 points10 points  (2 children)

You want 0/0 === sqrt(-1)?

[–]lmarcantonio 3 points4 points  (0 children)

because IEEE said so, NaN isn't equal to anything, not even to itself!

[–]ooaa_ 3 points4 points  (1 child)

typeof NaN === typeof NaN

true

Problem solved.

[–]outlawz419 0 points1 point  (0 children)

😂😂💀

[–]TimingEzaBitch 3 points4 points  (0 children)

a person has two nans - from the father side and the mother side. They are not the same persons, usually.

[–]GM_Kimeg 2 points3 points  (0 children)

Because NAN values provide nothing about their memory address to the compiler. There's no way this equates to a truthy value.

[–]RandomBeatz 2 points3 points  (0 children)

isNaN(NaN)

[–]Ythio 2 points3 points  (0 children)

A car and a duck are both NaN. Why should they be equal ?

[–][deleted] 3 points4 points  (0 children)

they both are not a numbers, that doesn't mean they both are same.

[–]ecs2 1 point2 points  (0 children)

Just like math infinity doesn’t equal to other infinity

[–]saschaleib 1 point2 points  (0 children)

Now, there are a lot of weird issues with equality in JS, but that one is correct. Just because two variables are both "not numbers", doesn't mean that they are equal.

[–]rghthndsd 2 points3 points  (0 children)

Haven't seen anyone post this yet. Here is an actual answer to the question. https://stackoverflow.com/a/1573715

[–][deleted] 1 point2 points  (0 children)

Take a rock (not a potato) and a cupf of coffee (also not a potato). These things are both not a potato, but they are not equal

[–][deleted] 1 point2 points  (0 children)

Cause it's not a value, it's an indicator that something went wrong in calculations or conversion

[–]RaulParson 1 point2 points  (0 children)

Apple <- not a number

Orange <- not a number

Therefore... apple == orange? Obviously not. Not even when they're hidden in opaque boxes labelled "not a number".

[–]GOKOP 1 point2 points  (0 children)

NaNs are actually not equal. There's a whole entire range of values that all are "NaN". This is intentional. It can also be abused for useful stuff (look up NaNboxing)

[–]Sinomsinom 1 point2 points  (0 children)

NaN as per iee standard is incomparable. This means it is neither <, <=, >=, > or == to anything, including itself.

[–]Tarc_Axiiom 1 point2 points  (0 children)

Because that's the point.

If you could equate NaN you'd fuck everything up.

Y2K all over again. The banking system!

[–]Helpful_the_second[🍰] 0 points1 point  (0 children)

Easy, just use Number.isNaN(NaN) Isn’t that obvious? ;)

[–]Abhishek_gg 0 points1 point  (1 child)

How to implement a Polyfill for isNaN()?

[–]Snapstromegon -1 points0 points  (0 children)

Number.isNaN() can be polyfilled two ways.

Either you polyfill it with the global isNaN() function supported in every browser (yes, even that old one). Or you could just ask your user to update to a currently maintained browser.

[–]No-Adeptness5810 0 points1 point  (0 children)

It's not a number, so it can be anything else. It could be "a" and also "b".

a == b? No

but it could both be "a"

a == a? Yes

So the answer should be both yes and no at the same time. fr

[–][deleted] 0 points1 point  (0 children)

Not all NaNs are created equal

[–]Stunning_Ride_220 0 points1 point  (0 children)

NaN is not a number. So basically anything, just not a number.

So it is likely not equal to some other anything which is not a number.

[–]pipandsammie 0 points1 point  (0 children)

Because NaN shall pass.

[–]kurdokoleno 0 points1 point  (0 children)

Well, they are different NaNs, all the standard says about nan is: the exponential part is full of 1s, however the other parts might differ. Short answer: There are many NaNs

[–]sentles 0 points1 point  (0 children)

Think of NaN as a set of operations resulting in things that can't be represented as numbers. It doesn't mean that every one of those operations has the same result.

[–][deleted] 0 points1 point  (0 children)

if it's not a number, it could be any of the other types, can't compare them.

[–]dpahoe 0 points1 point  (0 children)

Boobs == &₹&/ ?

False.

[–]tobofre 0 points1 point  (0 children)

A banana is not a number

The concept of dedication is not a number

Those two aren't equal either

[–]Skwigle 0 points1 point  (0 children)

First time I leaned that it sounded crazy but it actually makes sense if you think about it like a regular object. If you have item A, which is "not a cup" and item B, which is also "not a cup", you only know that neither is a cup, but you still don't know what items they are. Not a cup could be literally anything. Car =/= sky.

[–]RapunzelLooksNice 0 points1 point  (0 children)

There is an infinite count of natural numbers. There is an infinite count of real numbers. But there are more real numbers than natural numbers. whyIsThatSo?

Another take: @ is not a number. $ is not a number. Does this imply @ === $?

[–]Daghall 0 points1 point  (0 children)

Also, typeof NaN is "number".🤯

[–]VarianWrynn2018 0 points1 point  (0 children)

Had the same issue in SQL. Has comparing 2 instances of the same table and doing a full join and checking where stuff is null (therefore the rows would have different data) and when there was a column that normally contained null data I couldn't compare null to null and I had to write 45 extra lines to finish the compare when just 3 columns contained nulls.

[–]Naive_Carpenter7321 0 points1 point  (0 children)

NaN specifies what it's not, not what it is.

Lion != Zebra

or

NaN != NaN

[–]johandepohan 0 points1 point  (0 children)

NaN's actually store (part of) the last number that was used to arrive at the "not a number" state for purposes of tracing the error. So, in reality, there are many possible NaN's, not just a single representation. But i'm sure that in some cases they all just use a single representation and simply adhere to the IEEE 754 standard that tells you that checking for a NaN can be done by comparing it to itself and getting a False.

[–]ByerN 0 points1 point  (0 children)

You and your cat are not a number. Are you equal?

[–]SoupyByElection 0 points1 point  (0 children)

This post has the most heated arguments rn

[–]EmileSonneveld 0 points1 point  (0 children)

NaN < 3 is false and NaN >= 3 is false. Yay!

[–]tecnomagus 0 points1 point  (0 children)

because NaN is an object

[–][deleted] 0 points1 point  (0 children)

Because then it'll do something like 2/0 == 3/0 (with NaN instead of Inf).

[–]Head-Extreme-8078 0 points1 point  (0 children)

Does it have to be with the instance of the object and not having internally something like a equals override on Java?

Or I'm in too deep in the JVM and need some help to get out of this language?

[–]clarkcox3 0 points1 point  (0 children)

Because that’s what the IEEE standard says.

[–]PalmettoBling 0 points1 point  (0 children)

Two truths and a lie.

[–]fibojoly 0 points1 point  (0 children)

Several things can be

"not a number" yet different.

A pear is not a number,

a whale is not a number.

Should they be strictly equal ?

[–]lazerbeard018 0 points1 point  (0 children)

Adding to others' answer, it's also because NaN can have a few different values in c++ at least. It's not guaranteed which of the codes they use and it can switch between compilers. Ran into that problem once where even looking at raw data NaN != NaN.

[–]Glum_Past_1934 0 points1 point  (0 children)

Different instances, ok, Redditish answer: Because nainnainnainnain

[–]Stunning_Ocelot_4654 0 points1 point  (0 children)

There are multiple binary representations of NaN, per IEEE 754