Sub topic 11
Zero, Denormalized Numbers, Infinity and NaN
This chapter covers the reserved exponent patterns in IEEE 754: all zeros and all ones. It assumes you can encode a normal number as sign, biased exponent and fraction. It also assumes you know the biases (127 for single precision, 1023 for double) and the implicit leading 1 of a normalized mantissa.
Why reserved patterns exist
For a normal number, the value is (-1)^s × 1.f × 2^(e − bias). The implicit leading 1 causes two problems:
- No pattern represents zero, because 1.f is never 0.
- There is no way to represent the result of an invalid operation such as 0/0, or a result too large to store.
IEEE 754 solves both by reserving the two extreme exponent fields. The field with all zeros marks zero and denormals. The field with all ones marks infinity and NaN. Normal numbers use only the exponent fields in between.
The classification table
For single precision the exponent field is 8 bits, so the all-ones value is 255. For double precision it is 11 bits, so the all-ones value is 2047.
| Exponent field | Fraction | Meaning | Value |
|---|---|---|---|
| all 0s | 0 | Zero | (-1)^s × 0 |
| all 0s | not 0 | Denormalized | (-1)^s × 0.f × 2^(1 − bias) |
| 1 to max − 1 | any | Normal | (-1)^s × 1.f × 2^(e − bias) |
| all 1s | 0 | Infinity | (-1)^s × ∞ |
| all 1s | not 0 | NaN | not a number |
Zero and signed zero
Zero is the pattern with exponent 0 and fraction 0. The sign bit is still free, so there are two zeros: +0 (0x00000000) and −0 (0x80000000). They compare equal, so +0 == -0 is true. They behave differently in a few places:
1 / +0gives +∞ and1 / -0gives −∞.- A tiny negative result that underflows to zero keeps its sign, giving −0.
copysign(1, -0.0)returns −1.
Because of this, GATE questions that ask how many distinct bit patterns represent zero expect the answer 2.
Denormalized numbers
A denormalized (or subnormal) number has exponent field 0 and a nonzero fraction. Two rules change:
- The leading bit is 0, not 1. The mantissa is 0.f.
- The exponent is fixed at 1 − bias, not 0 − bias.
The second rule is a common source of mistakes. With exponent field 0, the true exponent is −126 in single precision, not −127. Using 1 − bias makes the largest denormal join smoothly to the smallest normal number.
For single precision:
- Smallest positive normal: 1.0 × 2^−126 ≈ 1.18 × 10^−38.
- Largest denormal: (1 − 2^−23) × 2^−126, just below the smallest normal.
- Smallest positive denormal: 2^−23 × 2^−126 = 2^−149 ≈ 1.4 × 10^−45.
For double precision, the smallest normal is 2^−1022 ≈ 2.2 × 10^−308. The smallest denormal is 2^−1074 ≈ 4.9 × 10^−324.
What denormals buy you
Without denormals, the gap between 0 and the smallest normal number (2^−126) would be much larger than the gap between the smallest normal number and the next one (2^−149). Any result in that gap would flush to zero. With denormals, the numbers below 2^−126 are evenly spaced 2^−149 apart. This is called gradual underflow. It guarantees that if x ≠ y, then x − y ≠ 0, so code like if x != y: z = 1/(x-y) is safe.
The cost is precision. A denormal has fewer significant bits because its leading zeros use up fraction bits. The smallest denormal has only 1 significant bit. Hardware also often handles denormals more slowly than normal numbers.
Infinity
Exponent field all 1s with fraction 0 is ±∞. Infinity is produced by:
- Overflow. A finite result whose magnitude is too big for the format becomes ±∞ under round-to-nearest.
- Division of a nonzero number by zero. The sign follows the usual sign rules, including the sign of zero.
Infinity behaves as the limit of large values: ∞ + 1 = ∞, 1/∞ = 0, and −∞ < every finite number. The largest finite single-precision value is (2 − 2^−23) × 2^127 ≈ 3.4 × 10^38. That is the pattern 0x7F7FFFFF, one step below 0x7F800000 (+∞).
NaN
Exponent field all 1s with a nonzero fraction is NaN (Not a Number). Operations with no meaningful numeric answer return NaN:
- 0/0 and ∞/∞
- ∞ − ∞ and 0 × ∞
- square root of a negative number
NaN has three properties:
- It propagates. Almost any operation with a NaN input returns NaN.
- It is unordered. Every comparison with NaN is false, except
!=. Sonan == nanis false, and testing for NaN requires a dedicated function such asisnan. - It has many encodings. The sign bit and the fraction bits are free (except that the fraction cannot be 0). Single precision therefore has 2 × (2^23 − 1) = 16,777,214 NaN patterns.
The top fraction bit separates the two kinds:
- Quiet NaN. The top fraction bit is 1, for example
0x7FC00000. It propagates silently. - Signaling NaN. The top fraction bit is 0 and the remaining bits are nonzero, for example
0x7F800001. Using it as an operand raises an invalid-operation exception.
The remaining fraction bits can carry a diagnostic payload.
Decoding by program
This function classifies and decodes a 32-bit pattern. The denormal branch uses 2^−126, the 1 − bias rule.
import math
def classify32(bits):
s = bits >> 31
e = (bits >> 23) & 0xFF
f = bits & 0x7FFFFF
if e == 0:
kind = "zero" if f == 0 else "denormal"
elif e == 0xFF:
kind = "infinity" if f == 0 else "NaN"
else:
kind = "normal"
return s, e, f, kind
def decode32(bits):
s, e, f, kind = classify32(bits)
sign = -1.0 if s else 1.0
if kind in ("zero", "denormal"):
return sign * (f / 2**23) * 2.0**-126
if kind == "infinity":
return sign * math.inf
if kind == "NaN":
return math.nan
return sign * (1 + f / 2**23) * 2.0**(e - 127)
patterns = [0x00000000, 0x80000000, 0x00000001, 0x007FFFFF,
0x00800000, 0x7F7FFFFF, 0x7F800000, 0xFF800000, 0x7FC00000]
for bits in patterns:
print(f"{bits:08x}", classify32(bits)[3], decode32(bits))Pattern 0x00000001 decodes to 2^−149 ≈ 1.4 × 10^−45, and 0x00800000 decodes to 2^−126. Python's float is a double, but these single-precision values are exactly representable in it.
Observing the behaviors
The next program shows overflow, invalid operations, signed zero and gradual underflow in double precision.
import math
print(1e308 * 10) # inf (overflow)
print(math.inf - math.inf) # nan (invalid operation)
print(math.nan == math.nan) # False
print(math.isnan(math.nan)) # True
print(0.0 == -0.0) # True
print(math.copysign(1, -0.0)) # -1.0
print(1 / math.inf) # 0.0
print(5e-324, 5e-324 / 2) # 5e-324 0.0
print(2.2250738585072014e-308 / 2 > 0) # True (denormal, not zero)Python raises ZeroDivisionError for 1 / 0.0 instead of returning ∞. That is a language choice. Hardware following IEEE 754 returns ±∞ by default.
Custom formats in GATE questions
GATE often defines a small format, such as 1 sign bit, 4 exponent bits (bias 7) and 3 fraction bits. Apply the same reserved-pattern rules:
- Reserved exponent fields. 0000 is for zero and denormals, and 1111 is for infinity and NaN.
- Largest finite. Exponent field 1110 (true exponent 14 − 7 = 7), mantissa 1.111₂ = 1.875. The value is 1.875 × 2^7 = 240.
- Smallest normal. Exponent field 0001, so 1.0 × 2^(1−7) = 2^−6.
- Largest denormal. (7/8) × 2^−6.
- Smallest positive denormal. (1/8) × 2^−6 = 2^−9.
- NaN patterns. Exponent 1111 with a nonzero 3-bit fraction and either sign gives 2 × 7 = 14.
A quick check is that the largest denormal and the smallest normal are adjacent patterns, and the largest finite number is adjacent to ∞.
Common errors
- Using exponent −127 (single) instead of −126 for denormals.
- Adding the implicit 1 to a denormal's mantissa.
- Counting one zero instead of two.
- Treating exponent field 255 as a normal number with exponent 128.
- Assuming
nan == nanis true.
Exercise
In single precision, what value does the bit pattern 0x00400000 represent?
Show answer
The exponent field is 0 and the fraction is 0x400000 = 2^22, so the number is denormalized. Its mantissa is 0.1b = 0.5, and the exponent is 1 − 127 = −126. The value is 0.5 × 2^−126 = 2^−127.
Exercise
A format has 1 sign bit, 4 exponent bits with bias 7, and 3 fraction bits, and follows IEEE 754 conventions. How many bit patterns are NaN, and what is the largest finite value?
Show answer
NaN needs exponent 1111 and a nonzero fraction, so 7 fractions × 2 signs = 14 patterns. The largest finite value has exponent field 1110 (true exponent 7) and mantissa 1.111b = 1.875, so it is 1.875 × 128 = 240.
Exercise
Is the single-precision pattern 0x7F800001 infinity, a quiet NaN or a signaling NaN? Which values do 1/(+0) and 0/0 give?
Show answer
The exponent field is all ones and the fraction is 1, which is nonzero, so it is a NaN. The top fraction bit is 0, so it is a signaling NaN. 1/(+0) gives +∞ (divide by zero of a nonzero number), and 0/0 gives NaN (invalid operation).