tc39 / proposal-math-sum

TC39 proposal to add a summation method to JavaScript
https://tc39.github.io/proposal-math-sum
72 stars 1 forks source link

Needs an example of what the "precise" means #17

Open shgysk8zer0 opened 2 months ago

shgysk8zer0 commented 2 months ago

I've looked at the polyfill, but I'm not exactly clear on why or how the algorithm is any more precise. 0.1 + 0.2 !== 0.3 still, and while I kinda get that maybe this isn't meant to fix floating point issues like that, I still don't get how this is meant to be any more "precise" than any basic approach. And just a single example of how this would be any more accurate than a staright-forward nums.reduce() highlighting where and how the simple approach is less accurate/precise than this would really do a lot to clarify why the more expensive operations are warranted.

Also worth mentioning that, due to the lack of such examples, many others are confused by this as well. It's not uncommon for devs to assume that this proposal is meant to resolve floating point arithmetic problems, and I don't think it actually resolves the issues many think it does. Again, just giving a quick example or two of where this is and isn't better than the simple approach would go a very long way.

bakkot commented 2 months ago

There's an example early in the readme:

let values = [1e20, 0.1, -1e20];

values.reduce((a, b) => a + b, 0); // 0

Math.sumPrecise(values); // 0.1

Is that not what you were looking for?

shgysk8zer0 commented 2 months ago

@bakkot My bad. I see that now.

So let's take my issue here as that difference needing more highlighting, including calling out what it does and does not resolve. I'm not sure if I just glossed over the proposal section because that's typically just where I look for the introduction of the new method rather than such details, or because I had previously seen some prior version where I think this was just Math.sum(...nums) .

Shall we just say that the actual problem this proposal is meant to address should be more explicitly detailed, especially with the emphasis implied by using sumPrecise rather than sum ?

Regardless, the core concept of this issue remains in that it should be more clear and detailed on what it is vs what it is not. This may be considered a duplicate of #14, in fact, but that only shows that the confusion isn't uncommon, and I think that giving a few clear examples of what this is and is not is still worth requesting.