Data Representation: Bits, Bytes & Numbers

floating-point special values (zero, infinity, NaN, subnormals)

/ NaN: nan /

IEEE-754 does not just store ordinary numbers; it reserves a few special bit patterns for situations ordinary numbers cannot handle — the result of dividing by zero, a quantity too small to write normally, or a calculation with no meaningful answer at all. Knowing these special values turns baffling output like 'inf' or 'nan' into something you can reason about instead of fear.

There are four families worth knowing. First, ZERO comes in two forms: +0.0 and -0.0, because the sign is a separate bit; they compare as equal but can behave differently (1.0 / +0.0 is +infinity, 1.0 / -0.0 is -infinity). Second, INFINITY (+inf and -inf) is what you get from overflow or from dividing a nonzero number by zero; it behaves arithmetically as you would hope (inf + 1 is inf). Third, NaN, 'Not a Number', is the result of a genuinely undefined operation like 0.0 / 0.0, infinity minus infinity, or the square root of a negative — and NaN is contagious: any arithmetic involving a NaN yields NaN. Fourth, SUBNORMALS (also called denormals) are extra-tiny values just above zero; normally floats have that implicit leading 1, but for the smallest magnitudes the format relaxes that rule to let values decay gradually to zero instead of snapping abruptly, a feature called gradual underflow.

The single most important and surprising property is that NaN is not equal to anything — not even to itself. The expression 'x == x' is true for every ordinary number but FALSE when x is NaN, which is in fact the standard, portable way to test for NaN (or use isnan()). Infinity and NaN propagate through later arithmetic, so a single bad operation early on can quietly turn a whole array of results into nan. Subnormals, meanwhile, are correct but can be dramatically slower on some hardware, occasionally causing mysterious performance cliffs.

0.0 / 0.0 gives NaN; 1.0 / 0.0 gives +inf. Testing for NaN: a value n is NaN exactly when (n != n) is true, since NaN compares unequal to everything including itself.

NaN != NaN is the portable NaN test; inf comes from overflow or x/0.

Because NaN is unequal to itself, sorting or de-duplicating data that contains NaN can behave bizarrely, and 'if (x == NaN)' never matches — use isnan(x) or the (x != x) idiom.

Also called
infinitynot a numberdenormalssubnormal非數值