Math.sumPrecise()

Baseline 2026
Newly available

Since April 2026, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

The Math.sumPrecise() static method takes an iterable of numbers and returns the sum of them. It is more precise than summing them up in a loop, because it avoids floating point precision loss in intermediate results.

Try it

console.log(Math.sumPrecise([1, 2]));
// Expected output: 3

console.log(Math.sumPrecise([1e20, 0.1, -1e20]));
// Expected output: 0.1

Syntax

js
Math.sumPrecise(numbers)

Parameters

numbers

An iterable (such as an Array) of numbers.

Return value

A number that is the sum of the numbers in the numbers iterable. If the iterable is empty, the return value is -0 (not 0).

Exceptions

TypeError

If numbers is not an iterable, or if any of the numbers in the iterable is not of the number type.

Description

Because sumPrecise() is a static method of Math, you always use it as Math.sumPrecise(), rather than as a method of a Math object you created (Math is not a constructor).

The method is called Math.sumPrecise() because it is more precise than naïvely summing up numbers in a loop. Consider the following example:

js
let sum = 0;
const numbers = [1e20, 0.1, -1e20];
for (const number of numbers) {
  sum += number;
}
console.log(sum); // 0

The output is 0. This is because 1e20 + 0.1 cannot be represented precisely in 64-bit floats, so the intermediate result is rounded to 1e20. Then, the sum of 1e20 and -1e20 is 0, so the final result is 0.

Math.sumPrecise() avoids this issue by using some specialized summing algorithm. It works as if the floating point numbers are summed up using their precise mathematical values, and the final result is then converted to the nearest representable 64-bit float. This still cannot avoid the 0.1 + 0.2 precision problem:

js
console.log(Math.sumPrecise([0.1, 0.2])); // 0.30000000000000004

Because the floating point literals 0.1 and 0.2 already represent mathematical values greater than 0.1 and 0.2, and their sum's closest 64-bit float representation is actually 0.30000000000000004.

Examples

Using Math.sumPrecise()

js
console.log(Math.sumPrecise([1, 2, 3])); // 6
console.log(Math.sumPrecise([1e20, 0.1, -1e20])); // 0.1

Specifications

Specification
ECMAScript® 2027 Language Specification
# sec-math.sumprecise

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
sumPrecise
Chrome – Full support
Chrome 147 (Release date: 2026-04-07)
footnote Full support
Edge – Full support
Edge 147 (Release date: 2026-04-10)
footnote Full support
Firefox – Full support
Firefox 137 (Release date: 2025-04-01)
footnote Full support
Opera – Full support
Opera 131 (Release date: 2026-04-29)
footnote Full support
Safari – Full support
Safari 26.2 (Release date: 2025-12-12)
footnote Full support
Chrome Android – Full support
Chrome Android 147 (Release date: 2026-04-07)
footnote Full support
Firefox for Android – Full support
Firefox for Android 137 (Release date: 2025-04-01)
footnote Full support
Opera Android – No support
Opera Android
footnote No support
Safari on iOS – Full support
Safari on iOS 26.2 (Release date: 2025-12-12)
footnote Full support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – Full support
WebView Android 147 (Release date: 2026-04-07)
footnote Full support
WebView on iOS – Full support
WebView on iOS 26.2 (Release date: 2025-12-12)
footnote Full support
Bun – Full support
Bun 1.2.18 (Release date: 2025-07-03)
footnote Full support
Deno – No support
Deno
footnote No support
Node.js – No support
Node.js
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support

See also