Python math.fsum() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math module

Definition

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)
CallWhat 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

ParameterDescription
iterableRequired positional iterable of real numbers (int or float).

Return Value

Return ValueDescription
floatAccurately 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.

python
import math

tenths = [0.1] * 10
naive = sum(tenths)
exact = math.fsum(tenths)

print("fsum:", exact, exact == 1.0)
print("matches 1.0 exactly?", exact == 1.0, naive <= 1.0)

How It Works

  1. Exact rounded total. Tracking rounding errors across all ten additions lets math.fsum() land cleanly on 1.0.
  2. Cross-version safety. While older Python sum() leaves 0.9999999999999999, fsum() gives exact 1.0 everywhere.
  3. Useful for financial floats. Summing long invoices of float cents stays immune to sequential rounding drift.

Example 2 — Preserve Small Values Between Giant Opposing Numbers

Compute the sum of [1e100, 1.0, -1e100] where sequential left-to-right addition drops 1.0.

python
import math

vals = [1e100, 1.0, -1e100]
seq = (vals[0] + vals[1]) + vals[2]
comp = math.fsum(vals)

print("left-to-right:", seq)
print("math.fsum    :", comp)

How It Works

  1. Swallowed middle term. Adding 1.0 directly to 1e100 rounds straight back to 1e100, so subtracting 1e100 leaves 0.0.
  2. Non-overlapping partials. math.fsum() holds 1.0 in a separate residual bucket until 1e100 cancels out.
  3. Exact recovery. Final summation yields exact 1.0.

Example 3 — Check Float Return Types, Empty Lists, and Inf + (-Inf)

Verify integer lists return floats, empty iterables return 0.0, and mixed infinities produce NaN.

python
import math

print(math.fsum([1, 2, 3]), type(math.fsum([1, 2, 3])).__name__)
print(math.fsum([]))
mixed_inf = math.fsum([float("inf"), float("-inf")])
print(math.isnan(mixed_inf))

How It Works

  1. Integer list becomes float. Unlike built-in sum(), math.fsum([1, 2, 3]) returns 6.0.
  2. Empty iterable gives 0.0. With no elements, it returns floating-point zero.
  3. Opposing infinities yield NaN. Summing positive and negative infinity produces nan, verified via math.isnan().

Frequently Asked Questions

It uses Shewchuk's full-precision partial-sum expansion algorithm, storing exact intermediate rounding errors until the final sum.
Always a float (6.0). Use built-in sum() when you want exact arbitrary-precision integer sums.
Because 0.1 in binary64 is slightly above 1/10 (0.10000000000000000555...), ten copies sum to just above 1.0, which rounds to exact 1.0.
No, unlike built-in sum(). Include any starting value in the iterable instead.
It returns float("nan") rather than raising an error.

Next: math.gamma()

Return the Gamma function at x as a float.

gamma() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful