v147 · JavaScript · demo

Sum REPL

Type comma-separated numbers and instantly see the naive sum, Math.sumPrecise result, and the exact difference between them.

Naive sum

Math.sumPrecise

Difference

const nums = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1];

// Naive: accumulated rounding error
let naive = 0;
for (const n of nums) naive += n;
console.log(naive); // 0.9999999999999999

// Precise: compensated algorithm (Chrome 147+)
const precise = Math.sumPrecise(nums);
console.log(precise); // 1

// Difference
console.log(Math.abs(naive - precise)); // 1.1102230246251565e-16

see also

implementation reference

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