Reference · Math

math.fsum

signature
math.fsum(list | null) -> float

What 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])))
output
0.6 0.60000000000000011.0 0.06.0 float

Edge cases

print(math.fsum([]), math.fsum(null), math.fsum([9007199254740993]))print(math.fsum([float("inf"), 1]), sum([1e308, 1e308]))
output
0.0 0.0 9007199254740992.0inf inf

Errors

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.

this one fails
print(math.fsum([1, "x"]))
output
line 1: `math.fsum` expects a list of numbers, but item 1 is str

Chaining

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)
output
0.6619.85

See also

sum, math.prod, math.sumprod, stats.mean, vec.cumsum

Thread
8Useful? Like it, or add to its thread below.

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