90 points · by shelfchair · 5y ago · 54 comments
From nanbox [0] README:
> IEEE 754 encodes 32-bit floating points with 1 bit for the sign, 8 bits for the exponent, and 23 bits for the number part (mantissa). For the specific case of NaN (e.g. the result of dividing 0 by 0, or the square root of a negative number), the spec encodes this as 11111111 in the exponent. The sign and the mantissa can be anything, and the spec suggests this can be used for "diagnostic information". One/some of these bits is/are commonly used to indicate a quiet NaN (qNaN) which could be expected vs. a signaling NaN (sNaN) which could be unexpected and should trigger an halting exception. This behavior is however sometimes different on different hardware.
> 23 bits isn't much space, but conveniently the max size of a Unicode codepoint is 0x10FFFF, making it a 22-bit charset. When you encode your strings in UTF-32, you're only going to be using 22 of those bits, so masking the top portion with the NaN signature makes all of your characters NaNs.
# [[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, (...) nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]]
After reading this snippet, you, too, might have for the day, or week(s) the soung stick in your head. NyaN. sorry, not sorry.
All values are stored as a double; true, false, null, undefined have different NaN values. NaN even has enough spare bits to store Object pointers. Short strings are also stored packed into NaN bits.
(x87 fp is obsolete and should never be used for anything.)
SGI/Irix machines in the 2000s, for example, had perfectly good floating point but had the 23rd being OFF for a quiet NaN, while other machines have it ON for quiet NaN. And this is independent of endien-ness: these SGIs were big endien just like other big-endian machines that made quiet NaN differently.
(I heard this from Joe Stoy, who worked with Strachey, so I suspect it's authentic.)
(!NaN) // true
(NaN == false) // false
Also JavaScript:
"10"+1 => "101"
"10"-1 => 9
String subtraction = ??? => Number subtraction
typeof(NaN) // 'number'Because x == x is falsified by NaN, it's not obvious to a human auditor or compiler optimization writer than you can't just blindly fold this expression without being very careful about data types.
I would sum it up by saying that x == x yielding false violates the principle of least surprise. It'll trip up every beginner who hasn't been explicitly been taught about NaN's behavior.
Maybe there are legitimate use cases for that kind of comparison, but you shouldn't use the conventional == operator for it. I think OCaml avoids the problem by having separate operators for floats, and Rust sort-of does by having a distinction between PartialEq and Eq.
> Every NaN shall compare unordered with everything, including itself.
(5.11 Details of comparison predicates)