If It Floats blog.coredump.cx · 9 points · 0 comments · HN thread TL;DR A short,...
HN Best 200 @best
If It Floats blog.coredump.cx · 9 points · 0 comments · HN thread
TL;DR A short, playful blog post by coredump.cx walking through surprising behaviors of IEEE floating-point values (NaN and Infinity) in C and JavaScript. It covers NaN inequality, NaN truthiness, float precision loops, sparse-array indexing with NaN, and using NaN sign bits to tell x86 from ARM.
- On modern CPUs, invalid float operations return Infinity or NaN instead of raising hardware exceptions.
- NaN never equals itself, while Infinity always equals itself; NaN ** 1e-350 is 1 despite NaN usually propagating.
- In C, NaN is truthy in if-conditions; in JavaScript the same check is falsy because of how the expression is evaluated.
- Large JavaScript numbers are floats, so a loop starting at 1e16 with i++ never terminates, and foo[1e16] and foo[1e16 + 1] refer to the same array slot.
- The sign bit of NaN produced by 0/0 differs between x86 and ARM, so signbit() can be used to detect knock-off CPUs.
Discussion No comments have been posted yet, so there is no discussion to summarize.