source: imaps-frontend/node_modules/core-js/modules/esnext.math.sum-precise.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 4.6 KB
Line 
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
4var $ = require('../internals/export');
5var uncurryThis = require('../internals/function-uncurry-this');
6var iterate = require('../internals/iterate');
7
8var $RangeError = RangeError;
9var $TypeError = TypeError;
10var $Infinity = Infinity;
11var $NaN = NaN;
12var abs = Math.abs;
13var pow = Math.pow;
14var push = uncurryThis([].push);
15
16var POW_2_1023 = pow(2, 1023);
17var MAX_SAFE_INTEGER = pow(2, 53) - 1; // 2 ** 53 - 1 === 9007199254740992
18var MAX_DOUBLE = Number.MAX_VALUE; // 2 ** 1024 - 2 ** (1023 - 52) === 1.79769313486231570815e+308
19var MAX_ULP = pow(2, 971); // 2 ** (1023 - 52) === 1.99584030953471981166e+292
20
21var NOT_A_NUMBER = {};
22var MINUS_INFINITY = {};
23var PLUS_INFINITY = {};
24var MINUS_ZERO = {};
25var FINITE = {};
26
27// prerequisite: abs(x) >= abs(y)
28var 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});
Note: See TracBrowser for help on using the repository browser.