math.fsum(iterable) computes an accurate floating-point sum of the numbers in iterable, returning a float.
It uses Shewchuk's expansion algorithm to track lost low-order bits across every addition, avoiding cumulative drift such as sum([0.1] * 10) returning 0.9999999999999999.
math.fsum() is a function in Python's standard math module: import it with import math and call math.fsum(...), or import the name directly with from math import fsum.
Syntax
Import math and pass any iterable of real numbers.
python
import math
total = math.fsum(numbers)
Call
What it does
math.fsum([0.1] * 10)
Returns 1.0 (exact rounded sum).
math.fsum([1e100, 1.0, -1e100])
Returns 1.0 (avoids cancellation loss).
math.fsum([1, 2, 3])
Returns 6.0 (always a float).
math.fsum([])
Returns 0.0.
Even though Python 3.12 improved built-in sum() for floats, math.fsum() guarantees exact final rounding across all Python 3 versions.
Function signatures
python
math.fsum(iterable, /)
# returns: float exact-rounded sum of iterable items
Parameters
Parameter
Description
iterable
Required positional iterable of real numbers (int or float).
Return Value
Return Value
Description
float
Accurately rounded floating-point sum, always as a float.
Python Examples
Example 1 — Compare Built-in sum() With math.fsum() on Repeated Tenths
Add ten copies of 0.1 and inspect exact equality with 1.0.