Math.sumPrecise()
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.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 147 | 147 | 137 | 26.2 | 147 | 26.2 | |
1+Supported (version) Not supported ※Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Syntax
JAVASCRIPT
// Standard addition results in rounding errors
0.1 + 0.2 + 0.3; // 0.6000000000000001
// Precise sum
Math.sumPrecise([0.1, 0.2, 0.3]); // 0.6 Live demo
Compare normal addition and sumPrecise
Check whether Math.sumPrecise improves floating-point summation in the current runtime.
JavaScript
Output
Press the Run button
Sum many fractional values
Test repeated decimal values where floating-point error tends to accumulate.
JavaScript
Output
Press the Run button
Use a fallback helper
Build a small helper that prefers Math.sumPrecise when present.
JavaScript
Output
Press the Run button
Use cases
-
Financial-style totals
Reduce rounding drift when summing many decimal values for reporting or reconciliation.
-
Numeric aggregation
Use more stable summation in charting, measurement, or simulation results.
Cautions
- Improved summation helps, but it does not remove every precision concern in floating-point workflows.
- Keep the numeric model explicit if the domain requires exact decimal semantics.
Accessibility
- More accurate totals support trustworthy user-facing summaries and announcements.
- If numbers drive status messages or billing UI, precision improvements can reduce confusing discrepancies.