deno.land / x / simplestatistic@v7.7.1 / src / sum.js

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/** * Our default sum is the [Kahan-Babuska algorithm](https://pdfs.semanticscholar.org/1760/7d467cda1d0277ad272deb2113533131dc09.pdf). * This method is an improvement over the classical * [Kahan summation algorithm](https://en.wikipedia.org/wiki/Kahan_summation_algorithm). * It aims at computing the sum of a list of numbers while correcting for * floating-point errors. Traditionally, sums are calculated as many * successive additions, each one with its own floating-point roundoff. These * losses in precision add up as the number of numbers increases. This alternative * algorithm is more accurate than the simple way of calculating sums by simple * addition. * * This runs in `O(n)`, linear time, with respect to the length of the array. * * @param {Array<number>} x input * @return {number} sum of all input numbers * @example * sum([1, 2, 3]); // => 6 */function sum(x) { // If the array is empty, we needn't bother computing its sum if (x.length === 0) { return 0; }
// Initializing the sum as the first number in the array let sum = x[0];
// Keeping track of the floating-point error correction let correction = 0;
let transition;
for (let i = 1; i < x.length; i++) { transition = sum + x[i];
// Here we need to update the correction in a different fashion // if the new absolute value is greater than the absolute sum if (Math.abs(sum) >= Math.abs(x[i])) { correction += sum - transition + x[i]; } else { correction += x[i] - transition + sum; }
sum = transition; }
// Returning the corrected sum return sum + correction;}
export default sum;
simplestatistic

Version Info

Tagged at
2 years ago