Comments (14)
If Math.sum
makes it, Math.product
might be useful, too.
from proposal-math-extensions.
@petamoriken Python has Math.fsum()
for accurate floating point sum.
Maybe add both? ๐ Math.sum()
- fast implementation and Math.fsum()
- slower implementation, but more accurate
from proposal-math-extensions.
Since we're "pivoting" this proposal to be a broader Math update, I think this addition is solid.
from proposal-math-extensions.
Why don't you adopt Kahan summation algorithm?
https://en.wikipedia.org/wiki/Kahan_summation_algorithm
from proposal-math-extensions.
Actually I think all arguments should be coerced ToNumber, like almost all builtin Math methods. In other words, it should definitely work on strings.
from proposal-math-extensions.
Here's an attempt at being more specific:
- If any argument is
NaN
the result should beNaN
. This can be used to bail out early. - Otherwise:
- If any argument is
Infinity
and no other arguments are-Infinity
the result isInfinity
- If any argument is
-Infinity
and no other arguments areInfinity
the result is-Infinity
- If any argument is
Infinity
and any other argument is-Infinity
the result isNaN
- If
typeof arg !== 'number'
for any argument the result isNaN
. OverloadingMath
functions to work on strings should not be allow. - Otherwise the result is the sum of the arguments computed by adding them in order.
- If no arguments are provided, the result is
0
.
- If no arguments are provided, the result is
- If any argument is
Here's a possible implementation:
Math.sum = (...args) => {
let result = 0;
for (let i = 0; i < args.length; i++) {
if (typeof args[i] !== 'number') {
return NaN;
}
result += args[i];
if (isNaN(result)) {
return result;
}
}
return result;
};
from proposal-math-extensions.
I vote for single accurate Math.sum()
which probably could use iterative pairwise summation like in Julia or Shewchuk's algorithm like in Python, but not Kahan summation which not always guarantee accurate results.
from proposal-math-extensions.
To be clear; the spec will need to include some algorithm. Itโs just that it should be possible for implementations to choose another one thatโs observably the same.
from proposal-math-extensions.
Effectively, Math.sum = function sum(...args) { return args.reduce((a, b) => Number(a) + Number(b), 0); }'
- I would be quite surprised if that polyfill did not match the spec.
from proposal-math-extensions.
@ljharb Kahan summation algorithm is a compensated summation (observable).
// Naive summation [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7].reduce(function(a, b) { return a + b }) // -> 15.299999999999999 // Kahan summation require('kahan').sum([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7]) // -> 15.3
Stream API (Collection) in Java use this algorithm.
http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/stream/Collectors.java#l538
from proposal-math-extensions.
TIL that Math
methods coerce strings to numbers.
from proposal-math-extensions.
That sounds like a specific implementation, which the spec usually does not dictate. That's the sort of thing that if it is not observable, an implementation is free to use.
from proposal-math-extensions.
That's going to be a different proposal
https://github.com/bakkot/proposal-math-sum
from proposal-math-extensions.
I agree with @ljharb, the algorithm should be implementation-defined. The only requirement the spec should impose, is to make the sum as accurate/precise as possible (by minimizing rounding-errors). This can be accomplished with summation-algorithms designed for F.P.-arithmetic, or by internally using BigFloat
s
from proposal-math-extensions.
Related Issues (20)
- [Suggestion] Methods to be added HOT 8
- [Suggestion] Math.mean and Math.roundBy HOT 2
- [Suggestion] Constants to be added HOT 1
- Is it still active? HOT 7
- [Suggestion] Math.lerp and Math.normalize HOT 2
- alternatives to Math.radians and Math.degrees HOT 5
- Math.exp2
- modulo function HOT 8
- Math.pow should accept Bigints, modulo and pow=-1 HOT 12
- Proposal Math.clamp separately HOT 1
- In the spec draft missed conversion of arguments to numbers
- Repository should be transferred to tc39 organization HOT 2
- Should `factorial` be defined in all or some domains? HOT 2
- Dot product?
- New rounding modes HOT 1
- Bitwise methods HOT 4
- Tuple methods
- `Number.prototype.toByteSize` / `Intl.SizeFormat` HOT 2
- `Math.copysign`? HOT 1
- `Math.MAX_RANDOM_BITS`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from proposal-math-extensions.