The Secret Life of NaN
a day ago
- #IEEE 754
- #NaN-boxing
- #floating-point
- Not-a-Number (NaN) in IEEE 754-2008 has a payload of bits that can be used for custom purposes.
- NaN payloads are used in dynamically typed languages like JavaScript and Lua for NaN-boxing, storing type information and non-float values.
- JavaScriptCore uses NaN-boxing to encode pointers, integers, booleans, and other values in 64 bits, shifting ranges to favor pointers.
- Quiet NaNs propagate diagnostic information, while signaling NaNs can raise exceptions for uninitialized variables.
- NaN-boxing trade-offs include memory savings and speed vs. implementation complexity and potential security vulnerabilities.