NaN Is Never Equal to Itself, Except When It Is

If It Floats

NaN Is Never Equal to Itself, Except When It Is

A tour of floating-point absurdities in C and JavaScript: NaN fails every equality check yet can equal itself as an array index, NaN ** 1e-350 returns 1, and ~NaN equals ~Infinity. JavaScript's float-backed arrays let foo[1e16 + 1] retrieve what was stored at foo[1e16], and a loop starting at 1e16 never terminates. The sign of NaN even reveals whether a CPU is x86 or ARM.

In C, because NaN is not a number, it can be positive or negative. This is a common way to detect knock-off CPUs.

More from this day

2026-12-01