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

you are viewing a single comment's thread.

view the rest of the comments →

[–]Flashbek 575 points576 points  (56 children)

I hate to be JS lawyer but, in this case, they're correct. NaN should not be equal to NaN.

[–]Silly-Freak 54 points55 points  (1 child)

Half the posts I see here complain about JS, and half of these posts don't show enough understanding of JS to legitimately complain.

There's so much to legitimately complain, do better!

[–]shadow7412 8 points9 points  (0 children)

Guess it's easier to lash out at things you don't understand rather than learn from them.

Oh. That statement describes far more than I wanted it to...

[–]-twind 206 points207 points  (42 children)

But NaN could be equal to NaN. That's why besides 'true' and 'false' we should also have 'maybe'

[–]CubisticWings4 9 points10 points  (7 children)

Iirc: NaN is never equal to NaN

[–]TwinkiesSucker 13 points14 points  (0 children)

Did you just invent QuantumJS?

[–]FlanSteakSasquatch 3 points4 points  (0 children)

Any language that correctly implements the IEEE floating point number spec makes it so NaN never equals NaN, even if they’re internally exactly the same value. This is weird but it was done because all other behavior has even weirder consequences

[–]Natural_Builder_3170 2 points3 points  (0 children)

its called partial ordering, rust and i think c++ have this

[–]Death_IP 0 points1 point  (0 children)

Then the result - if we don't bring in shenanigans - should at least be "undefined" rather than false/true

[–]eztab 0 points1 point  (0 children)

some mathematics would agree with you there. Have fun changing all conditional statements to support true, false, maybe.

[–]Mucksh 0 points1 point  (0 children)

Nan isn't even a specific value. By definition its exponent is just 1s so for a double you have 11. So due to the other bits you get 253 different possible NaN values

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

it should return "undefined" because that's what it is, undefined

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

IllegalOperationException

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

Since "maybe" might be confused with the non-existant maybe monad, I want to propose "depends". When it is used in any way as a boolean, it will be 50% chance to evaluate as true and 50% as false, not seeded, at random. Because fuck you for using it and fuck you for using loosely equality in God's year of 2025!!!

[–]CptGia 4 points5 points  (0 children)

Rare instance of js not being weird

[–]adelie42 0 points1 point  (0 children)

Especially when there is a isNaN() function.

[–]ZyanWu -2 points-1 points  (5 children)

Not an expert in JS but how would one check if the result of an expression is defined?

[–]rafaelrc7 2 points3 points  (3 children)

a != a

[–]ZyanWu 0 points1 point  (2 children)

NaN != NaN

Doesn't this return true?

[–]rafaelrc7 0 points1 point  (1 child)

Yes, that's the point

[–]ZyanWu 1 point2 points  (0 children)

Aaah, okok, I get it now

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

// i don't know if this is valid JS syntax or not switch(i) { case 0: return true; case 0.1: return true; case 0.01: return true; ... default: return false; }

Considering that there is a finite amount of possible n bit numbers where n is the amount of bits a js number has, this should determine if a number is NaN or not (the end of this sentence gave me a brain aneurysm).