Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

math.fsum

Accumulates floating-point values while tracking multiple partial sums to limit loss of precision.

Version: Python 3.14; repository image, production runtime not attested

Syntax

math.fsum(iterable, /)

Accumulates floating-point values while tracking multiple partial sums to limit loss of precision.

Large opposite-signed terms can hide a small term in ordinary sequential addition. An empty input sums to zero.

At 1e16 the increment 1.0 is too small to survive rounding in a single floating-point accumulator. By retaining several partial sums, fsum recovers that increment when the large terms cancel. Empty input returns floating-point zero.

import math
assert (1e16 + 1.0) - 1e16 == 0.0
assert math.fsum([1e16, 1.0, -1e16]) == 1.0 and math.fsum([]) == 0.0

Examples

import math
print(math.fsum([1e16, 1.0, -1e16]))
print(math.fsum([]))

Result

1.0
0.0

At 1e16 the increment 1.0 is too small to survive rounding in a single floating-point accumulator. By retaining several partial sums, fsum recovers that increment when the large terms cancel. Empty input returns floating-point zero.

Sources