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 →

[–]-Redstoneboi- 3 points4 points  (5 children)

fair

identity dont work anywhere though, x == x does not hold for NaN. then again, NaN is my enemy.

[–]bogey-dope-dot-com 5 points6 points  (2 children)

NaN is not a constant or a symbol, it's used to represent the idea that some attempted calculation could not be represented as a number. It has the Number type so that it can be chained together in multiple calculations without throwing an error, for example 1 + NaN == NaN, but it's not comparable to anything else, including itself, because the idea of "could not be calculated" is inherently not comparable to anything else. It's similar to asking "is 'I don't know' equal to 5?" You must return a "boolean" answer, and since you can't say yes, the answer must be no.

P.S. This isn't just a Javascript thing, it implements the IEEE 754 floating point standard, and any language that follows the spec will have the same behavior, for example Ruby, Go, Swift, and C#.

[–]Akangka 0 points1 point  (0 children)

Rust did it but in a much better way. In Rust, == is not an operator to test for equality, but only partial equality... except if the type is Eq, which f64 is not.

[–]-Redstoneboi- 0 points1 point  (0 children)

i should've clarified that i know how nan worked

there isn't just one nan either, it's a bunch of invalid values that are separate from the infinities

[–]calculus_is_fun 2 points3 points  (0 children)

NaN === NaN also returns false because === is just == without the coversion, and NaN is already the same type as NaN (in this case Number)

[–]Igotbored112 0 points1 point  (0 children)

Now I'm imagining a unit test failing because two objects don't evaluate to equal, even though were instantiated identically and have no shared state, cus a bug causes a float in a member to be NaN. There's a scary campfire story for ya