ShopperCove
Menu
All writingBlogTopicsCategoriesAboutRSS
Blog
Categories
Observability & SRE62All categories
About

Plate 85

  1. Blog

math.fsum vs sum Float Totals: Localhost Lab

Hands-on math.fsum vs builtin sum for float totals: real items/s plus cancellation accuracy, measured on Linux localhost in this hands-on lab for SREs.

Aditya Challa·1 October 2026·4 min read

Summary
On this page
  1. Intro — what this post promises
  2. Arms
  3. Lab topology
  4. Lead table — n=100 000 (p50 Mitems/s)
  5. Scale (uniform)
  6. Cancellation footgun
  7. Mixed-magnitude cost
  8. Reading it for SRE work
  9. Pitfalls
  10. Reproduce
  11. Limits
  12. Takeaway

Intro — what this post promises

Total floats with math.fsum vs builtin sum vs a naive += loop. This lab reports items/s on Linux localhost, plus a cancellation accuracy case that still bites on Python 3.13.

Related links:

  • fractions vs float localhost lab
  • statistics quantiles vs manual localhost lab
  • decimal vs float sum localhost lab
  • random choices vs sample localhost lab
  • graphlib topo vs manual localhost lab
  • functools cache vs lru localhost lab
  • tomllib vs json localhost lab
  • itertools batched vs chunk localhost lab

Lab honesty (1 Oct 2026 IST): Python 3.13.5. Affiliates: 0. Not Decimal (lab 85) and not Fraction (lab 122) — this is IEEE float summation only.

Verdict up front (n=100 000 uniform): sum ~205.04 Mitems/s; math.fsum ~188.64; naive loop ~84.48. Mixed magnitudes: sum ~206.94 vs fsum ~46.51. Accuracy: sum([1, 1e100, 1, -1e100]) → 0.0; math.fsum(...) → 2.0.


Arms

ArmPattern
sum(data)builtin float sum
math.fsum(data)precise float sum
naive s += xpure Python loop
mixed-magnitude variantsstress partials
statistics.fmean * nrelated mean path

Seven rounds, p50 items/s. Uniform draws keep magnitudes similar; mixed-exponent draws force larger intermediate cancellations.


Lab topology

n in {10k, 100k, 1M} · uniform + mixed · 7 rounds · p50
metric: items/s = n / p50_s

Script: lab-evidence/123-math-fsum-vs-sum/results/run_lab.py.


Lead table — n=100 000 (p50 Mitems/s)

ArmMitems/s
sum builtin205.04
math.fsum188.64
statistics.fmean×n187.1
naive loop84.48
sum mixed mag206.94
fsum mixed mag46.51

On uniform data, sum and fsum are close; the naive loop loses badly to interpreter overhead.


Scale (uniform)

nsumfsum
10 000209.33194.45
100 000205.04188.64
1 000 000149.79105.49

Throughput stays in the same ballpark as n grows — both paths are C-backed; the story is accuracy under cancellation, not asymptotic surprise.


Cancellation footgun

For [1, 1e100, 1, -1e100]: sum → 0.0, fsum → 2.0. Prefer math.fsum when large partials can cancel. Keep the classic mixed int/float literals in regression tests — all-float versions can take a different sum path on 3.13 and hide the bug.


Mixed-magnitude cost

On mixed exponents, fsum slowed to ~46.51 Mitems/s vs sum ~206.94 — precision work is not free. That is the trade: pay for shepherds of partials when correctness matters more than peak items/s.


Reading it for SRE work

  • Hot path, similar magnitudes → sum.
  • Large dynamic range / cancellation risk (log totals, compensated sensors) → math.fsum.
  • Never hand-roll += for speed — it lost to both C paths.
  • Exact decimals still want Decimal (lab 85) or Fraction (lab 122).

Document which reducer you pick in runbooks so on-call does not “optimize” away a deliberate fsum later.


Pitfalls

  • Assuming sum and fsum always agree.
  • Using fsum then wiping precision in display formats.
  • Confusing with lab 85 Decimal arithmetic.
  • Testing only all-float literals for cancellation demos on 3.13.
  • Replacing fsum with sum in a hot path without an accuracy fixture.

Reproduce

python3 lab-evidence/123-math-fsum-vs-sum/results/run_lab.py

Evidence: summary.json, summary.txt under lab-evidence/123-math-fsum-vs-sum/results/.


Limits

One Linux box. CPython float summation only. Not GPU reductions, not multiprecision.


Takeaway

At 100 k uniforms, sum ~205.04 Mitems/s edged fsum ~188.64. Use math.fsum when cancellation matters — classic case: sum 0.0 vs fsum 2.0.

pythonmath.fsumfloatsumperformanceprecisioncpython

Lab evidence

What I found running this

Ran the package lab on Linux localhost with CPython 3.13.5 on 1 Oct 2026 IST. Benchmarked seven p50 rounds for uniform and mixed-magnitude floats at 10k, 100k, and 1M items. Verified cancellation: sum returned 0.0 while math.fsum returned 2.0; affiliates 0.

Notes when a lab post goes up

Occasional email for new hands-on reviews. No sequence and no sponsors.

Related links

  • Plate 77

    fractions.Fraction vs float: Localhost Lab

    Hands-on fractions.Fraction vs float for exact ratios: real ops/s and exactness checks, measured on Linux localhost today in this hands-on lab for SREs.

    1 Oct 2026

  • Plate 57

    memoryview vs bytes Slice: Localhost Lab

    1 Oct 2026

  • Plate 82

    shlex.split vs str.split: Localhost Lab

    1 Oct 2026

On this page

  1. Intro — what this post promises
  2. Arms
  3. Lab topology
  4. Lead table — n=100 000 (p50 Mitems/s)
  5. Scale (uniform)
  6. Cancellation footgun
  7. Mixed-magnitude cost
  8. Reading it for SRE work
  9. Pitfalls
  10. Reproduce
  11. Limits
  12. Takeaway
All writingBlogCategoriesTopicsAboutPrivacyRSS

© 2026 ShopperCove