chickadee » math » fpsum

fpsum xsprocedure
xs
(list-of flonum)

Like (apply + xs), but incurs rounding error only once.

Examples:

> (+ 1.0 1e-16)
1.0
> (+ (+ 1.0 1e-16) 1e-16)
1.0
> (fpsum '(1.0 1e-16 1e-16))
1.0000000000000002

The sum function does the same for heterogenous lists of reals.

Worst-case time complexity is O(n^2), though the pathological inputs needed to observe quadratic time are exponentially improbable and are hard to generate purposely. Expected time complexity is O(n log(n)).

See fpvector-sums for a variant that computes all the partial sums in xs.