Skip to content

Commit

Permalink
update to stage 2.7 (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkot committed Apr 9, 2024
1 parent 3a864c8 commit f4286d0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
26 changes: 8 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Math.sumExact
# Math.sumPrecise

A proposal to add a method to sum multiple values to JavaScript.

Expand All @@ -8,7 +8,7 @@ Authors: Kevin Gibbons

Champions: Kevin Gibbons

This proposal is at stage 2 of [the TC39 process](https://tc39.es/process-document/): the proposal has been accepted as a draft.
This proposal is at stage 2.7 of [the TC39 process](https://tc39.es/process-document/): the proposal is approved in principle and now requires tests.

## Motivation

Expand All @@ -18,14 +18,14 @@ Also, summing a list of floating point numbers can be done more precisely than t

## Proposal

Add an iterable-taking `Math.sumExact` method which returns the sum of the values in the iterable using a more precise algorithm than naive summation.
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.

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

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

Math.sumExact(values); // 0.1
Math.sumPrecise(values); // 0.1
````

## Questions
Expand All @@ -46,29 +46,19 @@ So this proposal includes only an iterable-taking form.

### Naming

`Math.sum` is the obvious name, but it's not obvious that this going to be a different (slower) algorithm than naive summation. This is tentatively called `Math.sumExact` to call attention to that difference.
Since it differs from `Math.max` in taking an iterable, we might want a name which calls attention to that as well, such as `sumExactFrom`. See [issue #3](https://github.com/tc39/proposal-math-sum/issues/3) for discussion.
`Math.sum` is the obvious name, but it's not obvious that this going to be a different (slower) algorithm than naive summation. This is called `Math.sumPrecise` to call attention to that difference.
### Should this coerce things to number, or throw if given something which is not a number?
I want to [stop coercing things](https://github.com/tc39/how-we-work/pull/136), but `Math.max` is pretty strong precedent that we do coercion.
As currently specified it will reject non-number values, breaking with precedent.
It will reject non-number values, breaking with precedent from `Math.max`.
### Is the sum of an empty list 0 or -0?
In some sense -0 is the right answer: that's the additive identity on floats.

But in another sense the point is stick as close to real-number arithmetic as possible, and in the reals there is no -0.

Python's `fsum` returns 0 when given an empty list.
As currently specified this will return -0. This question remains open. See [issue #5](https://github.com/tc39/proposal-math-sum/issues/5) for discussion.
It is -0. This is the floating point additive identity. This choice ensures that `Math.sumPrecise([]) + Math.sumPrecise(foo)` always gives the same answer as `Math.sumPrecise(foo)`.
### Should this work with BigInts?
[No](https://github.com/tc39/proposal-bigint-math/issues/23) - it's important that `Math.sumExact()` returns the Number `-0` (or `0`), which means that `5n + Math.sumExact(bigints)` would throw when `bigints` is empty, which would be bad.
[No](https://github.com/tc39/proposal-bigint-math/issues/23) - it's important that `Math.sumPrecise()` returns the Number `-0` (or `0`), which means that `5n + Math.sumPrecise(bigints)` would throw when `bigints` is empty, which would be bad.

We could have seperate methods for summing BigInts. I'd vote for `BigInt.sum` or `BigInt.sumFrom`, depending on the outcome of the naming discussion above. But such a method will not be part of this proposal.
Expand Down
8 changes: 4 additions & 4 deletions spec.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!DOCTYPE html>
<meta charset="utf-8">
<pre class="metadata">
title: Math.sumExact
title: Math.sumPrecise
status: proposal
stage: 2
stage: 2.7
copyright: false
contributors: Kevin Gibbons
</pre>
Expand All @@ -18,8 +18,8 @@ <h1>Introduction</h1>
<p>This proposal adds a method for summing multiple numbers.</p>
</emu-clause>

<emu-clause id="sec-math.sumexact">
<h1>Math.sumExact ( _items_ )</h1>
<emu-clause id="sec-math.sumprecise">
<h1>Math.sumPrecise ( _items_ )</h1>
<p>Given an iterable of Numbers, this function sums each value in the iterable and returns their sum. If any value is not a Number it throws a *TypeError* exception.</p>
<p>It performs the following steps when called:</p>
<emu-alg>
Expand Down

0 comments on commit f4286d0

Please sign in to comment.