Understand floating-point range failures, distinguish infinity from an infinite mathematical answer, and compute log totals and normalised weights safely.
Overflow
Overflow occurs when a numerical result is too large in magnitude for the finite range of the chosen number format. The exact mathematical answer may exist and be finite; the computer cannot store it as a finite value in that format.
In quantitative work, overflow can interrupt an exponential calculation or turn intermediate values into infinity before a later division or logarithm brings the answer back to a manageable scale. A finite final answer does not guarantee that the route used to compute it will fit.
Range is different from precision
Floating-point arithmetic stores a significand and an exponent. The significand limits the available significant digits; the exponent range limits the scale. Rounding error concerns the gaps between representable values. Overflow concerns their outer boundary.
For IEEE binary64, the largest finite positive number is
M=(2−2−52)21023≈1.7976931348623157×10308.
The negative boundary has the same magnitude. For an exponential, logM≈709.7827128934 is a useful range diagnostic. It is not a license to replace a larger input by that threshold: doing so changes the calculation. The exact overflow boundary of a rounded operation also depends on its rounding behavior.
Overflow handling depends on the software. In the checked Python run, math.exp(1000) raises OverflowError. NumPy's binary64 exp, with overflow handling explicitly set to warn, returns positive infinity and issues a warning. NumPy can instead be configured to raise an error. Neither behavior means that the mathematical value e1000 is infinite. Python's documentation and NumPy's error-handling documentation describe these different interfaces.
A logarithm that fits when its ingredients do not
Suppose two synthetic, dimensionless log scores are 1000 and 999. We want
L=log(e1000+e999).
Both exponentials overflow in the checked Python math calculation. Taking a logarithm after constructing their sum cannot undo that failure.
Factor out the largest exponential algebraically, before evaluating it:
L=1000+log(1+e−1)≈1000.3132616875182.
The exponential that remains has a safe argument. Python's 1000 + math.log1p(math.exp(-1)) gives the displayed result. The function log1p(z) evaluates log(1+z) without unnecessarily losing a small correction near zero.
Two routes for log scores 1000 and 999. Direct exponentiation exceeds binary64 range. Subtracting 1000 first gives terms 1 and about 0.367879441, and a finite log total about 1000.3132616875182.
Original dimensionless calculation in binary64. The log total fits, while the unscaled sum is about 2.694819775×10434 and cannot be stored as a finite binary64 value. Runtime failure behavior is stated explicitly.
For a nonempty finite list of finite real scores, the same identity is the log-sum-exp reformulation:
log(i=1∑nexi)=m+log(i=1∑nexi−m),m=imaxxi.
Each shifted exponential is at most 1, and at least one is exactly 1 in exact arithmetic. This removes the original large positive exponentials. With an extremely wide spread, tiny shifted terms can still underflow; the method does not make every positive quantity representable. SciPy provides a dedicated logsumexp routine.
These weights are finite even though the original exponentials do not fit. In a direct NumPy evaluation, dividing the overflowed infinities produces NaN, meaning “not a number.” Invalid arithmetic such as infinity divided by infinity is distinct from the overflow that created those infinities.
Normalised exponential weights become probabilities only when the model gives them that interpretation. These scores are a numerical illustration, not market observations or automatically justified forecast probabilities. In likelihood calculations, retaining a log-likelihood can similarly avoid constructing large or tiny likelihood values merely to compare them.
What to do when a result exceeds the range
First decide which quantity the task actually requires: a log value, a ratio, an ordering, or an unscaled amount. Keep calculations in log form or use scaling when that preserves the required quantity. If the unscaled answer is essential and does not fit, report the range failure or use a suitable representation with a wider range and stated precision. Converting the finite log total above back to a binary64 exponential still overflows.
Clipping a result to the largest finite number fabricates a different answer. Silencing a warning also leaves the underlying infinity unchanged. Check inputs, intermediate values and outputs, and preserve the distinction between a range failure and a valid zero, infinity or missing observation in the model.
For the broader distinction between computational error and sensitive financial inputs, Numerical Conditioning and Stability connects numerical arithmetic to hedge ratios, regressions and covariance calculations. The full lesson requires membership; its public page provides a preview.