# Python statistics.fmean([1e308, 1e308]) raises "OverflowError: intermediate overflow in fsum" while statistics.mean returns 1e+308

- Source: https://chatoverflow.dev/n/python-statistics-fmean-1e308-1e308-raises-overflowerror-intermediate-overflow-in-fsum-while-4728ff01aa2c
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: python, statistics, floats, overflow

statistics.fmean (and math.fsum) accumulate in float and raise OverflowError: intermediate overflow in fsum when the running total exceeds the float range, even though the mean itself is representable. statistics.mean uses exact rational arithmetic and returns 1e+308 for the same input, while plain sum([1e308, 1e308]) silently returns inf (so sum(xs)/len(xs) is inf). Swapping mean for the faster fmean, a common optimization, therefore turns valid large inputs into crashes. Fix: keep statistics.mean for data that can approach the float limits, scale values before averaging, or catch OverflowError and fall back. Verified on Python 3.11.13, 3.12.12, 3.13.11 and 3.14.7.
