Reference · Math
math.fsum
math.fsum(list | null) -> floatWhat it does
The sum of a list of numbers as a float, rounded once at the end, as Python's
math.fsum (Shewchuk's algorithm): every item's bits count, so the answer is the
exactly rounded sum where sum accumulates rounding errors.
math.fsum([0.1, 0.2, 0.3]) is 0.6; sum of the same list is
0.6000000000000001. A long list is summed on all the cores and still gives the one
exact answer.
Examples
print(math.fsum([0.1, 0.2, 0.3]), sum([0.1, 0.2, 0.3]))print(math.fsum([1e100, 1.0, -1e100]), sum([1e100, 1.0, -1e100]))print(math.fsum([1, 2, 3]), type_of(math.fsum([1, 2, 3])))0.6 0.60000000000000011.0 0.06.0 floatEdge cases
- An empty list, or
null, gives0.0. - Ints are summed as floats, so the result is a float even for ints, and ints past
2⁵³ lose precision; use
sumfor an exact int total. - Infinity sums to infinity. Infinity with minus infinity has no sum, and neither has a
sum past the largest float, so
math.fsum([1e308, 1e308])stops the code, as Python raisesOverflowError;sumgivesinf.
print(math.fsum([]), math.fsum(null), math.fsum([9007199254740993]))print(math.fsum([float("inf"), 1]), sum([1e308, 1e308]))0.0 0.0 9007199254740992.0inf infErrors
A list item that is not a number is a runtime error, not an error value, and the
message says which item; so is an argument that is not a list or null.
print(math.fsum([1, "x"]))line 1: `math.fsum` expects a list of numbers, but item 1 is strChaining
A list in a pipeline is the only argument. The total of a column of floats, then on to the next step:
print([0.1, 0.2, 0.3] | math.fsum)print([2.5, 3.5] | math.fsum | math.floor)let rows = [{ amount: 12.5 }, { amount: 7.25 }, { amount: 0.1 }]print(rows | map((r) => r.amount) | math.fsum)0.6619.85See also
sum, math.prod, math.sumprod, stats.mean, vec.cumsum
Thread 0 posts
On math.fsum(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo