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.

InheritsBaseException›Exception›ArithmeticError›OverflowError
Arithmetic exceptionPython 3 (all)Live demo
OverflowError(*args)
Raised by
math.exp(1000), float(10**400), 10**400 / 3, int(inf)
Message
math range error · int too large to convert to float
Quick fix
stay in int (//), use Decimal, or work in log space
Watch out
1e308 * 10 gives inf silently — only some ops check

Demo

Live evaluation
math.pow(2, n) converts to float. The largest float is just under 2 ** 1024.
Try:
Inputs
nintexponent
Code
import math
math.pow(2, 10)
Result
1024.0

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

NameTypeRequiredDescription
*argsobjectnoUsually one message string; float ** can produce a two-item (errno, text) args tuple.

Attributes

AttributeTypeMeaning
argstupleUsually (message,). For float ** overflow CPython stores (errno, strerror), e.g. (34, 'Result too large') on Windows — the text is platform-dependent.
__context__BaseException | NoneThe exception being handled when this one was raised (implicit chaining).

Common patterns

Keep big numbers as ints
Int arithmetic is exact and unbounded; use // instead of / when the answer is an integer.
blocks = total_bytes // block_size
Log space for huge products
Add logarithms instead of multiplying big floats — standard for probabilities and likelihoods.
import math
log_p = sum(math.log(p) for p in probs)
Saturate instead of crashing
When infinity is an acceptable answer, catch the overflow and say so explicitly.
import math
try:
    growth = math.exp(rate * t)
except OverflowError:
    growth = math.inf
Decimal for big non-integers
Decimal has a far larger exponent range than float.
from decimal import Decimal
Decimal(10) ** 400

Examples

1. math.exp past the limit
import math math.exp(1000)
Returns
OverflowError: math range error
2. Huge int to float
float(10 ** 400)
Returns
OverflowError: int too large to convert to float
3. True division of huge ints
10 ** 400 / 3
Returns
OverflowError: integer division result too large for a float
4. Floor division stays exact
10 ** 400 // 7 % 10
Returns
8
5. Infinity to int
int(float('inf'))
Returns
OverflowError: cannot convert float infinity to integer
6. Plain multiply does not check
1e308 * 10
Returns
inf
7. Int outside a C-sized range
len(range(2 ** 64))
Returns
OverflowError: Python int too large to convert to C ssize_t
8. int.to_bytes too small
(256).to_bytes(1, 'big')
Returns
OverflowError: int too big to convert

Pitfalls

1. / on big ints converts to float
True division always produces a float, so it fails whenever the quotient is beyond float range — even though both operands are exact ints. Use // (and %) to stay exact.
/
total = 10 ** 400
half = total / 2
half
OverflowError: integer division result too large for a float
//
total = 10 ** 400
half = total // 2
half == 5 * 10 ** 399
True
2. Rounding an infinite float
An unchecked overflow (1e300 * 1e10) produces inf, and the error only shows up later when you convert it to int.
round(inf)
round(1e300 * 1e10)
OverflowError: cannot convert float infinity to integer
Check first
import math
x = 1e300 * 1e10
r = round(x) if math.isfinite(x) else None
r is None
True
3. math.sqrt of a huge int
math functions convert their argument to float first. math.isqrt works on ints of any size.
math.sqrt
import math
math.sqrt(10 ** 400)
OverflowError: int too large to convert to float
math.isqrt
import math
math.isqrt(10 ** 400) == 10 ** 200
True

When to use

Use it
  • 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
Reach for something else
  • Integer math → ints never overflow; use // and keep them ints
  • Huge products of floats → log space
  • Big non-integer values → decimal.Decimal

Notes

Ints are unbounded
The docs: overflow cannot occur for integers (they raise MemoryError instead); OverflowError on ints means a value is outside a range some API requires
Not checked
Float +, -, * and float() of a string return inf silently (1e308 * 10, float("1e999")); math functions and ** check
float ** text
2.0 ** 10000 raises OverflowError with args (errno, strerror) from the C library, so its message differs by platform — Windows shows (34, 'Result too large')
Catch via
except ArithmeticError also catches ZeroDivisionError

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.