Skip to content
Latchkey

Python "OverflowError" in CI

Python integers are unbounded, but floats and C-backed math functions are not. OverflowError is raised when a float result exceeds the largest representable double, commonly from math.exp, **, or float(huge_int).

What this error means

A numeric test fails with "OverflowError: math range error" or "OverflowError: (34, 'Result too large')" on an exponential or power expression.

python
    p = math.exp(x)
OverflowError: math range error

Common causes

An exponential or power on a large input

math.exp(x) or base ** big exceeds the float range when x or the exponent is large.

Converting a very large int to float

float(huge_int) overflows when the integer is larger than the maximum double.

How to fix it

Bound the input or work in log space

  1. Clamp or validate inputs so they stay within a representable range.
  2. For probability math, compute in log space (logsumexp) instead of raw exp.
  3. Catch OverflowError and substitute a saturating value where appropriate.
Python
import math

def safe_exp(x: float) -> float:
    if x > 709:  # exp(710) overflows float64
        return math.inf
    return math.exp(x)

How to prevent it

  • Validate the numeric range of inputs before exp/pow.
  • Use log-space math for products of many small probabilities.
  • Test boundary inputs explicitly.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →