1 | 'use strict';
|
---|
2 | // based on Shewchuk's algorithm for exactly floating point addition
|
---|
3 | // adapted from https://github.com/tc39/proposal-math-sum/blob/3513d58323a1ae25560e8700aa5294500c6c9287/polyfill/polyfill.mjs
|
---|
4 | var $ = require('../internals/export');
|
---|
5 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
6 | var iterate = require('../internals/iterate');
|
---|
7 |
|
---|
8 | var $RangeError = RangeError;
|
---|
9 | var $TypeError = TypeError;
|
---|
10 | var $Infinity = Infinity;
|
---|
11 | var $NaN = NaN;
|
---|
12 | var abs = Math.abs;
|
---|
13 | var pow = Math.pow;
|
---|
14 | var push = uncurryThis([].push);
|
---|
15 |
|
---|
16 | var POW_2_1023 = pow(2, 1023);
|
---|
17 | var MAX_SAFE_INTEGER = pow(2, 53) - 1; // 2 ** 53 - 1 === 9007199254740992
|
---|
18 | var MAX_DOUBLE = Number.MAX_VALUE; // 2 ** 1024 - 2 ** (1023 - 52) === 1.79769313486231570815e+308
|
---|
19 | var MAX_ULP = pow(2, 971); // 2 ** (1023 - 52) === 1.99584030953471981166e+292
|
---|
20 |
|
---|
21 | var NOT_A_NUMBER = {};
|
---|
22 | var MINUS_INFINITY = {};
|
---|
23 | var PLUS_INFINITY = {};
|
---|
24 | var MINUS_ZERO = {};
|
---|
25 | var FINITE = {};
|
---|
26 |
|
---|
27 | // prerequisite: abs(x) >= abs(y)
|
---|
28 | var twosum = function (x, y) {
|
---|
29 | var hi = x + y;
|
---|
30 | var lo = y - (hi - x);
|
---|
31 | return { hi: hi, lo: lo };
|
---|
32 | };
|
---|
33 |
|
---|
34 | // `Math.sumPrecise` method
|
---|
35 | // https://github.com/tc39/proposal-math-sum
|
---|
36 | $({ target: 'Math', stat: true }, {
|
---|
37 | // eslint-disable-next-line max-statements -- ok
|
---|
38 | sumPrecise: function sumPrecise(items) {
|
---|
39 | var numbers = [];
|
---|
40 | var count = 0;
|
---|
41 | var state = MINUS_ZERO;
|
---|
42 |
|
---|
43 | iterate(items, function (n) {
|
---|
44 | if (++count >= MAX_SAFE_INTEGER) throw new $RangeError('Maximum allowed index exceeded');
|
---|
45 | if (typeof n != 'number') throw new $TypeError('Value is not a number');
|
---|
46 | if (state !== NOT_A_NUMBER) {
|
---|
47 | // eslint-disable-next-line no-self-compare -- NaN check
|
---|
48 | if (n !== n) state = NOT_A_NUMBER;
|
---|
49 | else if (n === $Infinity) state = state === MINUS_INFINITY ? NOT_A_NUMBER : PLUS_INFINITY;
|
---|
50 | else if (n === -$Infinity) state = state === PLUS_INFINITY ? NOT_A_NUMBER : MINUS_INFINITY;
|
---|
51 | else if ((n !== 0 || (1 / n) === $Infinity) && (state === MINUS_ZERO || state === FINITE)) {
|
---|
52 | state = FINITE;
|
---|
53 | push(numbers, n);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | });
|
---|
57 |
|
---|
58 | switch (state) {
|
---|
59 | case NOT_A_NUMBER: return $NaN;
|
---|
60 | case MINUS_INFINITY: return -$Infinity;
|
---|
61 | case PLUS_INFINITY: return $Infinity;
|
---|
62 | case MINUS_ZERO: return -0;
|
---|
63 | }
|
---|
64 |
|
---|
65 | var partials = [];
|
---|
66 | var overflow = 0; // conceptually 2 ** 1024 times this value; the final partial is biased by this amount
|
---|
67 | var x, y, sum, hi, lo, tmp;
|
---|
68 |
|
---|
69 | for (var i = 0; i < numbers.length; i++) {
|
---|
70 | x = numbers[i];
|
---|
71 | var actuallyUsedPartials = 0;
|
---|
72 | for (var j = 0; j < partials.length; j++) {
|
---|
73 | y = partials[j];
|
---|
74 | if (abs(x) < abs(y)) {
|
---|
75 | tmp = x;
|
---|
76 | x = y;
|
---|
77 | y = tmp;
|
---|
78 | }
|
---|
79 | sum = twosum(x, y);
|
---|
80 | hi = sum.hi;
|
---|
81 | lo = sum.lo;
|
---|
82 | if (abs(hi) === $Infinity) {
|
---|
83 | var sign = hi === $Infinity ? 1 : -1;
|
---|
84 | overflow += sign;
|
---|
85 |
|
---|
86 | x = (x - (sign * POW_2_1023)) - (sign * POW_2_1023);
|
---|
87 | if (abs(x) < abs(y)) {
|
---|
88 | tmp = x;
|
---|
89 | x = y;
|
---|
90 | y = tmp;
|
---|
91 | }
|
---|
92 | sum = twosum(x, y);
|
---|
93 | hi = sum.hi;
|
---|
94 | lo = sum.lo;
|
---|
95 | }
|
---|
96 | if (lo !== 0) partials[actuallyUsedPartials++] = lo;
|
---|
97 | x = hi;
|
---|
98 | }
|
---|
99 | partials.length = actuallyUsedPartials;
|
---|
100 | if (x !== 0) push(partials, x);
|
---|
101 | }
|
---|
102 |
|
---|
103 | // compute the exact sum of partials, stopping once we lose precision
|
---|
104 | var n = partials.length - 1;
|
---|
105 | hi = 0;
|
---|
106 | lo = 0;
|
---|
107 |
|
---|
108 | if (overflow !== 0) {
|
---|
109 | var next = n >= 0 ? partials[n] : 0;
|
---|
110 | n--;
|
---|
111 | if (abs(overflow) > 1 || (overflow > 0 && next > 0) || (overflow < 0 && next < 0)) {
|
---|
112 | return overflow > 0 ? $Infinity : -$Infinity;
|
---|
113 | }
|
---|
114 | // here we actually have to do the arithmetic
|
---|
115 | // drop a factor of 2 so we can do it without overflow
|
---|
116 | // assert(abs(overflow) === 1)
|
---|
117 | sum = twosum(overflow * POW_2_1023, next / 2);
|
---|
118 | hi = sum.hi;
|
---|
119 | lo = sum.lo;
|
---|
120 | lo *= 2;
|
---|
121 | if (abs(2 * hi) === $Infinity) {
|
---|
122 | // rounding to the maximum value
|
---|
123 | if (hi > 0) {
|
---|
124 | return (hi === POW_2_1023 && lo === -(MAX_ULP / 2) && n >= 0 && partials[n] < 0) ? MAX_DOUBLE : $Infinity;
|
---|
125 | } return (hi === -POW_2_1023 && lo === (MAX_ULP / 2) && n >= 0 && partials[n] > 0) ? -MAX_DOUBLE : -$Infinity;
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (lo !== 0) {
|
---|
129 | partials[++n] = lo;
|
---|
130 | lo = 0;
|
---|
131 | }
|
---|
132 |
|
---|
133 | hi *= 2;
|
---|
134 | }
|
---|
135 |
|
---|
136 | while (n >= 0) {
|
---|
137 | sum = twosum(hi, partials[n--]);
|
---|
138 | hi = sum.hi;
|
---|
139 | lo = sum.lo;
|
---|
140 | if (lo !== 0) break;
|
---|
141 | }
|
---|
142 |
|
---|
143 | if (n >= 0 && ((lo < 0 && partials[n] < 0) || (lo > 0 && partials[n] > 0))) {
|
---|
144 | y = lo * 2;
|
---|
145 | x = hi + y;
|
---|
146 | if (y === x - hi) hi = x;
|
---|
147 | }
|
---|
148 |
|
---|
149 | return hi;
|
---|
150 | }
|
---|
151 | });
|
---|