v147 · JavaScript

Math.sumPrecise

A new static method that sums all values in an iterable using a compensated algorithm — eliminating the floating-point drift that accumulates with naive + addition.

concepts

  1. Float Drift Lab

    Side-by-side comparison of naive loop summation vs Math.sumPrecise. Add large sets of IEEE 754 floats and watch the error accumulate in the naive column while the precise column stays exact.

  2. Sum REPL

    Enter any comma-separated numbers and instantly see the naive sum, Math.sumPrecise result, and the exact difference. Great for verifying where floating-point math breaks down.

  3. Kahan Summation Visualizer

    Step through Kahan compensated summation — the algorithm behind Math.sumPrecise() — side-by-side with naïve addition. See the compensation term accumulate at each step and compare error magnitude across five different test datasets.

  4. Financial Ledger

    Add transactions to a ledger and compare the running balance computed by naïve reduce() vs Math.sumPrecise(). Load the stress test — 40 micro-transactions plus the classic 0.1+0.2 entry — to see where real financial drift appears.

  5. Compatibility Lab

    Detects Math.sumPrecise availability and runs a battery of floating-point precision test cases — [0.1, 0.2], [1e15, 0.1, -1e15], [0.1 × 10] — comparing naive reduce() against Math.sumPrecise. Provides the Kahan compensated summation fallback for non-supporting environments.

why it shipped

IEEE 754 floating-point arithmetic means that adding many values in a loop accumulates rounding errors — 0.1 + 0.2 famously yields 0.30000000000000004, not 0.3. Financial calculations, statistical aggregations, and physics simulations all suffer from this. Math.sumPrecise implements the Neumaier compensated-summation algorithm, which tracks the accumulated error and corrects for it. The TC39 proposal standardises the method so every JavaScript engine can provide a reliable, cross-platform precise sum without the developer needing to write or import a library.

references

implementation reference

Need the exact API surface, compatibility boundaries, errors, lifecycle, and source links? Read the matching gendn reference ↗