Special Floating-Point Values: Infinity and NaN
What you'll learn: How floating-point systems represent and use infinity and NaN (Not-a-Number) to handle exceptional mathematical conditions.
Why These Special Values Exist
In the IEEE 754 floating-point standard, not every computation produces a normal number. Division by zero, square roots of negative numbers, and overflows need representation. Rather than crashing your program, the standard defines special bit patterns for infinity (∞) and NaN (Not-a-Number).
Positive and Negative Infinity
Infinity represents values too large to fit in the available floating-point range. When all exponent bits are 1 and all mantissa (fraction) bits are 0, you have infinity.
- Positive infinity (
+∞): Results from operations like1.0 / 0.0or overflow from extremely large positive values - Negative infinity (
-∞): Results from operations like-1.0 / 0.0or overflow from extremely large negative values
Analogy: Think of infinity as a "number line escape hatch"—once you go beyond the representable range, you enter infinity territory.
Not-a-Number (NaN)
NaN represents undefined or unrepresentable results. When all exponent bits are 1 and the mantissa is non-zero, you have NaN. Examples:
0.0 / 0.0(indeterminate form)∞ - ∞(undefined)- Square root of a negative number
- Any operation involving an existing NaN
NaN has a unique property: NaN ≠ NaN. It's not equal to itself, making it easy to detect.
Their Role in Robust Computing
These special values prevent program crashes and allow computations to continue even when exceptional conditions occur, letting you detect and handle errors gracefully after the fact rather than during arithmetic.
Key Takeaway: Infinity and NaN are IEEE 754's standardized representations for overflow, division by zero, and undefined operations, enabling floating-point arithmetic to handle exceptional conditions without halting execution.