OverflowError
Python ints never overflow — floats do. You get OverflowError when a float result passes about 1.8e308, when a huge int is converted to float, or when an int does not fit a C-sized slot.
Demo
import math math.pow(2, 10)
In Trigger, 1023 gives 8.98846567431158e+307 and 1024 overflows with math range error. Going the other way, -1075 does not raise at all — it quietly underflows to 0.0. That asymmetry is the rule: overflow is checked, underflow is not. The int 2 ** 1024 itself is fine; only the conversion to float fails.
Constructor
| Name | Type | Required | Description |
|---|---|---|---|
| *args | object | no | Usually one message string; float ** can produce a two-item (errno, text) args tuple. |
Attributes
| Attribute | Type | Meaning |
|---|---|---|
| args | tuple | Usually (message,). For float ** overflow CPython stores (errno, strerror), e.g. (34, 'Result too large') on Windows — the text is platform-dependent. |
| __context__ | BaseException | None | The exception being handled when this one was raised (implicit chaining). |
Common patterns
blocks = total_bytes // block_size
import math log_p = sum(math.log(p) for p in probs)
import math try: growth = math.exp(rate * t) except OverflowError: growth = math.inf
from decimal import Decimal Decimal(10) ** 400
Examples
Pitfalls
total = 10 ** 400 half = total / 2 half
total = 10 ** 400 half = total // 2 half == 5 * 10 ** 399
round(1e300 * 1e10)
import math x = 1e300 * 1e10 r = round(x) if math.isfinite(x) else None r is None
import math math.sqrt(10 ** 400)
import math math.isqrt(10 ** 400) == 10 ** 200
When to use
- Raising it when a value does not fit a fixed-size field (int32, a byte)
- Catching it around math.exp / math.pow on unbounded input
- Mapping overflow to math.inf when saturation is the right answer
- Integer math → ints never overflow; use // and keep them ints
- Huge products of floats → log space
- Big non-integer values → decimal.Decimal
Notes
FAQ
A math module function (exp, pow, ldexp, cosh …) produced a result larger than the largest float, about 1.8e308. math.exp overflows just above 709.78. Reduce the input, work with logarithms, catch it and use math.inf, or use decimal.Decimal for a larger range.