oracle / graaljs

GraalJS – A high-performance, ECMAScript compliant, and embeddable JavaScript runtime for Java
https://www.graalvm.org/javascript/
Universal Permissive License v1.0
1.82k stars 191 forks source link

[GR-53531] Implement `Math.sumPrecise` #857

Open fniephaus opened 1 month ago

fniephaus commented 1 month ago

TL;DR

We plan to implement Math.sumPrecise in GraalJS.

Details

Add an iterable-taking Math.sumPrecise method which returns the sum of the values in the iterable using a more precise algorithm than naive summation.

Summing a list is a very common operation and is one of the few remaining use cases for Array.prototype.reduce. Better to let users express that operation directly.

Also, summing a list of floating point numbers can be done more precisely than the naive .reduce((a, b) => a + b, 0) approach using more clever algorithms, a fact which few JavaScript programmers are aware of (and even among those who are, most wouldn't bother doing it). We can make it easy to reach for the better option.