Search code examples
floating-pointnanieee-754

Why does IEEE 754 reserve so many NaN values?


It seems that the IEEE 754 standard defines 16,777,214 32-bit floating point values as NaNs, or 0.4% of all possible values.

I wonder what is the rationale for reserving so many useful values, while only 2 ones essentially needed: one for signaling and one for quiet NaN.

Sorry if this question is trivial, I couldn't find any explanation on the internet.


Solution

  • According to this series of notes by William Kahan, one of the lead designers of the IEEE-754 format, the use of multiple NaNs was intended to let hardware fill in information about what triggered the NaN in the first place so that computations that would end up resulting in NaN could run to completion and then allow the programmer to write code to analyze what had gone wrong:

    IEEE 754's specification for NaN endows it with a field of bits into which software can record, say, how and/or where the NaN came into existence. That information would be extremely helpful for subsequent “Retrospective Diagnosis” of malfunctioning computations, but no software exists now to employ it. Customarily that field has been copied from an operand NaN to the result NaN of every arithmetic operation, or filled with binary 1000...000 when a new NaN was created by an untrapped INVALID operation. For lack of software to exploit it, that custom has been atrophying.

    So it seems like this was intentional and left unspecified so that different systems could handle things differently. In retrospect, it seems like this never really ended up happening, but it seems like a reasonable idea!