JavaScript (ES2026) reference · Math Methods and Constants
JavaScript Math.sumPrecise() Method
Add up an iterable of numbers with full precision, avoiding the rounding errors of repeated + additions.
JavaScript (ES2026) Since ES2026
Syntax
Math.sumPrecise(items)
Parameters
| Parameter | Description |
|---|---|
items |
An iterable of numbers. |
Return value
A number.
Examples
const values = [0.1, 0.2, 0.3];
console.log(values.reduce((a, b) => a + b, 0), Math.sumPrecise(values));
Output
0.6000000000000001 0.6
console.log(Math.sumPrecise([1e20, 1, -1e20]), 1e20 + 1 - 1e20);
Output
1 0
Math.sumPrecise() is new in ES2026 and not yet available in every engine (Node.js 26, for example, doesn't
have it), so the outputs above are the results defined by the specification. Check support before relying on
it, or fall back to reduce():
const sum = (xs) => (Math.sumPrecise ? Math.sumPrecise(xs) : xs.reduce((a, b) => a + b, 0));
Version history
| Version | Change |
|---|---|
| JavaScript ES2026 | Added |
In other languages
sum()· Python
Related
Math.round()· Round a number to the nearest integer; halves round up towards +Infinity, so Math.round(-2.5) is -2.- JavaScript Math · tutorial
- JavaScript Numbers · tutorial