Two Case Studies of NaN: How Python and Lua Break on Weird Numbers

I explore how IEEE-754 NaN values cause unexpected behavior in programming language design. In Python, list equality optimizations fail because NaN does not equal itself. In Lua, numerical for-loops produce unintuitive results when NaN is used as an initial value or step, revealing undocumented implementation quirks. These cases highlight how implicit assumptions in language semantics often leave NaN unaccounted for.

NaN is weird, and that means other things behave weirdly with it.
  1. aragilar

    You don't need NaN (or Inf) to have unexpected behaviour with floats and naive loops in lua, using 1e52 is enough. Conflating floating point and integers (independent of encoding) is always a bit of a trap.

  2. jjgreen

    hypot(Inf, NaN) == Inf

    made me laugh out loud ...

  3. ColinWright

    See also: https://news.ycombinator.com/item?id=48855045

More from this day

2026-07-14