For a quant, this can turn a positive probability or likelihood into a stored zero. Comparing two models then becomes unreliable if both products disappear before their relative sizes are examined. A numerical zero does not establish that an event is impossible.
A positive product that disappears
Consider a synthetic model with 400 independent stages. At each stage, a specified event has probability . The probability that every specified event occurs is
0.1 ** 400 and sequential multiplication of 400 copies of 0.1 give 0.0. Even the nearest binary64 representation of the exact decimal value is zero. Binary64's initial approximation to is a separate source of rounding error; it does not explain away the range failure.The last nonzero values are subnormal
That is not the smallest positive representable number. Subnormal numbers extend towards zero in fixed steps of
A subnormal result can still be exact: is representable. Merely producing a subnormal value does not necessarily raise an underflow signal. With round-to-nearest, ties-to-even, a positive exact result below rounds to zero; the halfway value also rounds to zero. Different rounding modes or settings that flush subnormals to zero change this behaviour.
Do not apply the familiar normal-range relative-error bound indiscriminately here. Rounding a positive result to zero gives 100% relative error, however tiny its absolute error is.
Take logs before multiplying
For positive factors, use the identity
In the example, the exact decimal inputs give
400 * math.log(0.1) produces the finite binary64 value -921.0340371976182. Keeping a log-likelihood lets a model-fitting routine compare products without constructing them. Logs preserve ordering for positive values; differences of logs represent log ratios.math.log after the product became 0.0 fails with ValueError in Python. The lost probability cannot be recovered from that zero. Converting the finite log result back with math.exp also gives 0.0 in this example: the log representation solves the storage problem for the log value, not for the original probability.A true zero factor needs its own branch. For a discrete probability model, a factor assigned exactly zero gives a zero product and an extended-real log value of when at least one factor is present. That differs from a positive product lost during computation. The empty product is 1 and its log is 0. These cases should be handled before evaluating logarithms.
Keep the numerical and modelling decisions separate
If a task only needs log probabilities, rankings or likelihood ratios, retain the log representation. If it needs the unscaled probability, use a representation with adequate range and stated precision, or report the range failure. Repeated rounded multiplication can also stagnate at a positive subnormal value, so a nonzero output alone is no guarantee of accuracy.
Sources
- NumPy documentation:
numpy.finfo, including normal and subnormal limits. - NumPy documentation:
numpy.seterr, including the distinction between underflow and other floating-point errors. - Python documentation:
sys.float_info, including the smallest positive normal float and how to obtain the smallest subnormal. - Python documentation:
math, including logarithms and exceptional-case behaviour. - Trefethen, L. N., and Bau, D. III. Numerical Linear Algebra. SIAM, 1997. Lecture 13, “Floating Point Arithmetic,” pp. 97–98.