Floating-Point Overflow and Underflow
Understand what happens when floating-point calculations produce values too large (overflow) or too small (underflow) to represent, and how IEEE 754 handles these cases.
Decimal Value
3.4028234663852886e+38
Float32 Hex
0x7F7FFFFF
Float64 Hex
0x47EFFFFFE0000000
Detailed Explanation
Overflow and underflow occur when the result of a floating-point operation falls outside the representable range. IEEE 754 defines specific behaviors for both cases.
Overflow:
Overflow occurs when the result's magnitude exceeds the largest representable finite value.
| Precision | Max Finite Value | Hex |
|---|---|---|
| Float32 | 3.4028235e+38 |
0x7F7FFFFF |
| Float64 | 1.7976931e+308 |
0x7FEFFFFFFFFFFFFF |
When overflow occurs, the result becomes +Infinity or -Infinity (depending on sign) in the default rounding mode. In round-toward-zero mode, it may return the largest finite value instead.
Common overflow scenarios:
- Exponential growth:
Math.exp(1000)→Infinity - Large multiplications:
1e200 * 1e200→Infinity - Squaring:
(1e200) ** 2→Infinity
Underflow:
Underflow occurs when the result is too small (too close to zero) to represent as a normalized number. IEEE 754 handles this with gradual underflow through denormalized numbers.
| Precision | Smallest Normalized | Smallest Denormalized |
|---|---|---|
| Float32 | 1.175e-38 |
1.401e-45 |
| Float64 | 2.225e-308 |
5e-324 |
Below the smallest denormalized value, the result flushes to zero.
The underflow flag:
IEEE 754 defines an underflow exception flag that is set when:
- The result is denormalized, AND
- There is a loss of accuracy (the rounded result differs from the exact result)
Most high-level languages do not expose this flag directly.
Detecting overflow and underflow:
// Overflow detection
if (!isFinite(result)) {
console.log("Overflow occurred");
}
// Underflow detection (approximate)
if (result !== 0 && Math.abs(result) < Number.MIN_VALUE) {
console.log("Result is denormalized (underflow)");
}
// Complete flush to zero
if (result === 0 && exactResult !== 0) {
console.log("Underflow: flushed to zero");
}
Avoiding overflow in practice:
A common technique is to work in log space. Instead of computing a * b, compute log(a) + log(b). This prevents overflow at the cost of some precision. Machine learning frameworks use this extensively for probability calculations.
Another technique is scaling: divide large numbers by a known constant before multiplication, then multiply the result back.
Use Case
Managing overflow and underflow is essential in scientific computing (particle physics simulations, cosmological calculations), machine learning (loss function computation, softmax), financial risk models, and any application dealing with very large or very small numbers.