v147 · JavaScript · Precision
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 and watch error drift at each step.
Naïve sum (+ operator)
Math.sumPrecise() / Kahan
the algorithm
// Naïve — accumulates floating-point error
function naiveSum(values) {
let sum = 0;
for (const v of values) sum += v;
return sum;
}
// Kahan compensated sum — what Math.sumPrecise() implements
function kahanSum(values) {
let sum = 0, compensation = 0;
for (const v of values) {
const y = v - compensation; // compensate for lost low bits
const t = sum + y;
compensation = (t - sum) - y; // recover what was lost
sum = t;
}
return sum;
}
// Chrome 147+: built-in, handles edge cases (Infinity, NaN, -0)
Math.sumPrecise([0.1, 0.2, 0.3]) // → 0.6 (exact)
references
implementation reference
Need the exact API surface, compatibility boundaries, errors, lifecycle, and source links? Read the matching gendn reference ↗