Stop Using reduce for Sums: JavaScript's Math.sumPrecise() Explained
This article explains how JavaScript's reduce method loses precision when summing large or many numbers due to floating-point rounding, introduces the new ES2026 Math.sumPrecise() method that avoids intermediate rounding errors, demonstrates its accuracy with examples like summing 1e20, 0.1, -1e20 and 1000 times 0.1, and notes current browser support.
Floating-Point Precision Issues with reduce
The most common way to sum an array in JavaScript uses reduce:
const total = prices.reduce((sum, price) => sum + price, 0);While concise, this approach silently loses precision when numbers vary greatly in magnitude or when summing many values.
Classic Example: Large Numbers Swallow Small Ones
const values = [1e20, 0.1, -1e20];
values.reduce((a, b) => a + b, 0);
// => 0
Math.sumPrecise(values);
// => 0.1 reducereturns 0 because 1e20 + 0.1 rounds back to 1e20 in IEEE 754 double-precision, the 0.1 is lost, and adding -1e20 yields 0. Mathematically absurd: the middle value is completely erased. Math.sumPrecise returns the correct 0.1 because it treats all numbers as exact values and rounds only once at the end.
The Root Cause: IEEE 754 Double-Precision
JavaScript's Number type is based on the IEEE 754 double-precision standard, using 64-bit binary representation. It offers a huge range but only about 15–17 significant decimal digits. Many decimal fractions are inherently approximate, e.g.:
0.1 + 0.2;
// => 0.30000000000000004This is not a JavaScript bug; any language using IEEE 754 double-precision (C, Java, Python, etc.) exhibits the same behavior.
New Feature: Math.sumPrecise()
Math.sumPrecise()is a static method added in ES2026 for more accurate summation of an iterable (arrays, Sets, Map values, generators, etc.):
Math.sumPrecise([0.1, 0.2, 0.3]);
// => 0.6000000000000001The result still shows a tiny error because 0.1, 0.2, and 0.3 themselves are approximate literals. Math.sumPrecise solves rounding errors during accumulation, not the fundamental representation limit of decimal fractions.
Intuitive Comparison: Summing 1000 Times 0.1
const values = Array.from({ length: 1000 }, () => 0.1);
values.reduce((a, b) => a + b, 0);
// => 99.99999999999859
Math.sumPrecise(values);
// => 100With reduce, error appears by the 12th decimal place; Math.sumPrecise delivers exactly 100 by avoiding repeated intermediate rounding.
Why It's More Accurate
reduceperforms a chain of pairwise additions: (((a + b) + c) + d) + ... Each addition incurs a floating-point rounding, and errors accumulate. Worse, different input orders can produce different results. Math.sumPrecise is specified to sum all numbers as exact values first, then round once to a 64-bit float. Consequently:
Result is independent of input order.
Large numbers cannot “swallow” smaller ones.
Precision remains high when accumulating large datasets.
Browser Support
As of writing, Math.sumPrecise() is supported in the latest versions of Chrome, Firefox, and Safari. Node.js does not yet support it (absent in Node 24.x).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JavaScript
Provides JavaScript enthusiasts with tutorials and experience sharing on web front‑end technologies, including JavaScript, Node.js, Deno, Vue.js, React, Angular, HTML5, CSS3, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
