Sub topic 11
Zero, Denormalized Numbers, Infinity and NaN
Assume you know binary place values, powers of two, and the IEEE 754 fields: sign, exponent, and fraction. This chapter uses binary32, which has 1 sign bit, 8 exponent bits, and 23 fraction bits.
The special exponent patterns
For a normal IEEE 754 number, the exponent field is neither all zeros nor all ones. Its value is
(-1)^sign × (1.fraction) × 2^(exponent − bias)
The leading 1 is implicit. Binary32 uses a bias of 127.
The two boundary exponent patterns have different meanings:
| Exponent field | Fraction field | Meaning |
|---|---|---|
| All zeros | All zeros | Positive or negative zero |
| All zeros | Nonzero | Positive or negative subnormal |
| All ones | All zeros | Positive or negative infinity |
| All ones | Nonzero | NaN |
| Other patterns | Any | Normal finite number |
The sign bit still distinguishes positive and negative zero, subnormal values, and infinity.
Zero and subnormal numbers
The bit patterns for positive and negative zero are:
0 00000000 00000000000000000000000=+01 00000000 00000000000000000000000=-0
These values compare equal in ordinary comparisons, but they can behave differently in some operations. For example, 1 / +0 is positive infinity, while 1 / -0 is negative infinity.
A subnormal number, also called a denormalized number, has an all-zero exponent and a nonzero fraction. It does not use the implicit leading 1. Its value is
(-1)^sign × (0.fraction) × 2^(1 − bias)
For binary32, the smallest positive subnormal is 2^-149. Subnormals fill the gap between zero and the smallest positive normal number, 2^-126. This behavior is called gradual underflow.
The trade-off is lower precision near zero. Normal values have a significand beginning with 1; subnormal values begin with 0, so fewer significant bits are available.
Infinity and NaN
Infinity represents a value beyond the finite range. It is produced by operations such as finite overflow or division by zero, depending on the operation and rounding rules.
NaN means “not a number.” It represents an undefined or invalid result, such as 0 / 0 or the square root of a negative number in real arithmetic. A NaN has an all-one exponent and a nonzero fraction.
NaN is unordered: comparisons such as NaN == NaN are false, and both NaN < x and NaN > x are false. Test it with an explicit NaN predicate. IEEE 754 distinguishes signaling and quiet NaNs, although most application code handles quiet NaNs.
import math
import struct
def bits(x):
word = struct.unpack(">I", struct.pack(">f", x))[0]
return f"{word:032b}"
values = [0.0, -0.0, float("inf"), float("-inf"), float("nan")]
for value in values:
print(value, bits(value), math.isnan(value), math.isinf(value))GATE problem pattern
When a question gives a floating-point bit pattern:
- Separate the sign, exponent, and fraction fields.
- Check the exponent for all zeros or all ones.
- For an all-zero exponent, check whether the fraction is zero.
- Apply the normal or subnormal formula only after classification.
Exercise
In binary32, what does the pattern 0 11111111 00000000000000000000000 represent, and what does 1 00000000 10000000000000000000000 represent?
Show answer
The first pattern is positive infinity because the exponent is all ones and the fraction is zero. The second is a negative subnormal because the sign is one, the exponent is all zeros, and the fraction is nonzero.