Loading...
const total = [0.1, 0.2, 0.3].reduce((a, b) => a + b, 0);
console.log(total); // 0.6000000000000001const total = Math.sumPrecise([0.1, 0.2, 0.3]);
console.log(total); // 0.6reduce() rounds after every addition, while Math.sumPrecise() rounds only the final exact sum.
Math.sumPrecise(a, b) instead of Math.sumPrecise([a, b]).Math.sumPrecise() receives an iterable like an array, not separate arguments.BigInt values are not passed, because they throw a TypeError.It returns -0, which equals 0 in comparisons.
No. Each input is still a binary float. It only avoids extra rounding between additions, so the result is the closest float to their exact sum.
It does more work per value than a plain loop, so use it where accuracy matters, not in tight hot paths.