Search code examples
floating-pointlanguage-agnosticnanieee-754numerical-computing

Is 0.0 / 0.0 a well-defined value?


Since 0.0 / 0.0 is mathematically undefined, the IEEE-754 floating-point standards reasonably define NaN as its result. Now, because unlike infinity, NaN is not a well-defined value, but a set of values, the question that whether 0.0 / 0.0 is a well defined constant or not is also reasonable.

It's worth it to mention that x / 0.0 would be infinity in IEEE-754 if x != 0.0.

Is 0.0 / 0.0 a well-defined constant NaN value in the IEEE-754 floating-point standards or not? In other words does it have a well-defined bit pattern or not?


Solution

  • IEEE 754-2008 describes four levels for specifying floating-point data.

    Level 1 is the extended real numbers—the real numbers plus −∞ and +∞. This is the level for regular mathematics; two divided by three is exactly two-thirds.

    Level 2 is floating-point data. It includes all the values representable in floating-point (finite and infinite) plus a NaN, and it distinguishes −0 and +0. This is the level for floating-point arithmetic; operations return a value that is the exact mathematical value rounded to a representable value. Two divided by three in the basic 64-bit binary format using round-to-nearest mode is exactly 0.66666666666666662965923251249478198587894439697265625. This level is algebraically closed; any arithmetic operation on floating-point data produces floating-point data (possibly NaN).

    Level 3 is representations of floating-point data. At this level, finite numbers are represented with a sign, an exponent, and a significand, and it includes −∞ and +∞ and two NaNs, a quiet (non-signaling) NaN and a signaling NaN. (A signaling NaN causes exceptions when used.) A significand is a fraction portion of the representation. If a binary floating-point number has an exponent of e and a significand of f, it represents a value of −f • 2e or +f • 2e, depending on its sign. At this level, that result of dividing two by three is a + sign, an exponent of -1, and a significand of 1.3333333333333332593184650249895639717578887939453125 or, in hexadecimal, 1.555555555555516.

    Level 4 is bit strings. At this level:

    • For finite numbers, the sign is 0 (for +) or 1 (for −). For normal numbers, the exponent is encoded as an unsigned value by adding a bias to the actual exponent. For the basic 64-bit binary format, the bias is 1023, so an actual exponent of −1 is encoded as −1+1023 = 1022. The significand is encoded with its first bit removed. So, for 53-bit significands, only 52 bits are stored in the significand field. For subnormal numbers, the exponent is encoded as 0.

    • Infinity is encoded with a sign bit, an exponent field that is all ones (2047 in the basic 64-bit binary format), and a significand field that is all zeros.

    • NaNs are encoded with a sign bit, an exponent field that is all ones, and a significand field that is not all zeros. IEEE 754-2008 recommends that quiet NaNs be encoded with a 1 in the first bit of the significand field and signaling NaNs be encoded with a 0 in the first bit, but this is not required.

    Since a “NaN” is not a number, IEEE 754-2008 avoids calling it a number or a value. A thing in level 2 is a datum.

    NaNs are well defined data. Operations that produce them and operations on them are specified.

    Technically, 0.0 / 0.0 is not a constant value; it is an expression. IEEE 754-2008 specifies that dividing zero by zero signals an invalid operation exception. When an exception is signaled, normal processing may be interrupted, so this division might not produce any result. In many environments, floating-point exceptions just raise a flag recording the exception but normal processing continues. In this case, IEEE 754-2008 specifies the operation produces a quiet NaN. IEEE 754-2008 does not specify the particular bits in the quiet NaN; a system is free to use those bits to encoded diagnostic information or to use them for other purposes.

    (In order to focus on NaNs, I have omitted some details, such as what subnormal numbers are, how significands are normalized for level four, and how the leading bit of a significand is known if it is not stored.)