and typeof NaN == "number" is also part of the standard, it is valid to pass NaN to functions that accept numbers, and it's expected to propagate or change back to normal numbers (e.g. NaN^0=1)
Re:JavaScript Is Weird
by marcosdumay on Sunday August 13, 2023 @12:57PM
(#37111707)
If anything, JS has plenty of problems due to its too literal interpretation of NaN.
I mean, it is a weird statement in that the language apparently needs a === operator.
Of course, after looking up what === is, the behavior is obvious.
Re:JavaScript Is Weird
by threatofrain on Sunday August 13, 2023 @12:44PM
(#37111577)
That wouldn't change anything here. This behavior is due to the IEEE 754 number spec, which is implemented in many languages.
The floating point behavior is the obvious part, the === operator is the head-scratcher.
Re:JavaScript Is Weird
by kerkeslager on Sunday August 13, 2023 @01:04PM
(#37111796)
Yeah, this is a IEEE754 floating point issue.
I'm not sure why it seems there hasn't been any progress made on making floating point types less error-prone in the last few decades.
Rounding errors are unavoidable when representing an infinite amout of numbers in a fixed number of bits. But in many cases a decimal type (like in C#) would have more intuitive behvior than binary floating point. A decimal type have been proposed for JavaScript but I dont know how far along it is.
This isn't just javascript though. It's harder to find a language for which this would be true than the other way around.