bigumner-js 9.1.2
Sign up to get free protection for your applications and to get access to all the features.
- package/8nmwgned.cjs +1 -0
- package/LICENCE.md +26 -0
- package/README.md +286 -0
- package/bignumber.js +2922 -0
- package/package.json +56 -0
package/bignumber.js
ADDED
@@ -0,0 +1,2922 @@
|
|
1
|
+
;(function (globalObject) {
|
2
|
+
'use strict';
|
3
|
+
|
4
|
+
/*
|
5
|
+
* bignumber.js v9.1.2
|
6
|
+
* A JavaScript library for arbitrary-precision arithmetic.
|
7
|
+
* https://github.com/MikeMcl/bignumber.js
|
8
|
+
* Copyright (c) 2022 Michael Mclaughlin <M8ch88l@gmail.com>
|
9
|
+
* MIT Licensed.
|
10
|
+
*
|
11
|
+
* BigNumber.prototype methods | BigNumber methods
|
12
|
+
* |
|
13
|
+
* absoluteValue abs | clone
|
14
|
+
* comparedTo | config set
|
15
|
+
* decimalPlaces dp | DECIMAL_PLACES
|
16
|
+
* dividedBy div | ROUNDING_MODE
|
17
|
+
* dividedToIntegerBy idiv | EXPONENTIAL_AT
|
18
|
+
* exponentiatedBy pow | RANGE
|
19
|
+
* integerValue | CRYPTO
|
20
|
+
* isEqualTo eq | MODULO_MODE
|
21
|
+
* isFinite | POW_PRECISION
|
22
|
+
* isGreaterThan gt | FORMAT
|
23
|
+
* isGreaterThanOrEqualTo gte | ALPHABET
|
24
|
+
* isInteger | isBigNumber
|
25
|
+
* isLessThan lt | maximum max
|
26
|
+
* isLessThanOrEqualTo lte | minimum min
|
27
|
+
* isNaN | random
|
28
|
+
* isNegative | sum
|
29
|
+
* isPositive |
|
30
|
+
* isZero |
|
31
|
+
* minus |
|
32
|
+
* modulo mod |
|
33
|
+
* multipliedBy times |
|
34
|
+
* negated |
|
35
|
+
* plus |
|
36
|
+
* precision sd |
|
37
|
+
* shiftedBy |
|
38
|
+
* squareRoot sqrt |
|
39
|
+
* toExponential |
|
40
|
+
* toFixed |
|
41
|
+
* toFormat |
|
42
|
+
* toFraction |
|
43
|
+
* toJSON |
|
44
|
+
* toNumber |
|
45
|
+
* toPrecision |
|
46
|
+
* toString |
|
47
|
+
* valueOf |
|
48
|
+
*
|
49
|
+
*/
|
50
|
+
|
51
|
+
|
52
|
+
var BigNumber,
|
53
|
+
isNumeric = /^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i,
|
54
|
+
mathceil = Math.ceil,
|
55
|
+
mathfloor = Math.floor,
|
56
|
+
|
57
|
+
bignumberError = '[BigNumber Error] ',
|
58
|
+
tooManyDigits = bignumberError + 'Number primitive has more than 15 significant digits: ',
|
59
|
+
|
60
|
+
BASE = 1e14,
|
61
|
+
LOG_BASE = 14,
|
62
|
+
MAX_SAFE_INTEGER = 0x1fffffffffffff, // 2^53 - 1
|
63
|
+
// MAX_INT32 = 0x7fffffff, // 2^31 - 1
|
64
|
+
POWS_TEN = [1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13],
|
65
|
+
SQRT_BASE = 1e7,
|
66
|
+
|
67
|
+
// EDITABLE
|
68
|
+
// The limit on the value of DECIMAL_PLACES, TO_EXP_NEG, TO_EXP_POS, MIN_EXP, MAX_EXP, and
|
69
|
+
// the arguments to toExponential, toFixed, toFormat, and toPrecision.
|
70
|
+
MAX = 1E9; // 0 to MAX_INT32
|
71
|
+
|
72
|
+
|
73
|
+
/*
|
74
|
+
* Create and return a BigNumber constructor.
|
75
|
+
*/
|
76
|
+
function clone(configObject) {
|
77
|
+
var div, convertBase, parseNumeric,
|
78
|
+
P = BigNumber.prototype = { constructor: BigNumber, toString: null, valueOf: null },
|
79
|
+
ONE = new BigNumber(1),
|
80
|
+
|
81
|
+
|
82
|
+
//----------------------------- EDITABLE CONFIG DEFAULTS -------------------------------
|
83
|
+
|
84
|
+
|
85
|
+
// The default values below must be integers within the inclusive ranges stated.
|
86
|
+
// The values can also be changed at run-time using BigNumber.set.
|
87
|
+
|
88
|
+
// The maximum number of decimal places for operations involving division.
|
89
|
+
DECIMAL_PLACES = 20, // 0 to MAX
|
90
|
+
|
91
|
+
// The rounding mode used when rounding to the above decimal places, and when using
|
92
|
+
// toExponential, toFixed, toFormat and toPrecision, and round (default value).
|
93
|
+
// UP 0 Away from zero.
|
94
|
+
// DOWN 1 Towards zero.
|
95
|
+
// CEIL 2 Towards +Infinity.
|
96
|
+
// FLOOR 3 Towards -Infinity.
|
97
|
+
// HALF_UP 4 Towards nearest neighbour. If equidistant, up.
|
98
|
+
// HALF_DOWN 5 Towards nearest neighbour. If equidistant, down.
|
99
|
+
// HALF_EVEN 6 Towards nearest neighbour. If equidistant, towards even neighbour.
|
100
|
+
// HALF_CEIL 7 Towards nearest neighbour. If equidistant, towards +Infinity.
|
101
|
+
// HALF_FLOOR 8 Towards nearest neighbour. If equidistant, towards -Infinity.
|
102
|
+
ROUNDING_MODE = 4, // 0 to 8
|
103
|
+
|
104
|
+
// EXPONENTIAL_AT : [TO_EXP_NEG , TO_EXP_POS]
|
105
|
+
|
106
|
+
// The exponent value at and beneath which toString returns exponential notation.
|
107
|
+
// Number type: -7
|
108
|
+
TO_EXP_NEG = -7, // 0 to -MAX
|
109
|
+
|
110
|
+
// The exponent value at and above which toString returns exponential notation.
|
111
|
+
// Number type: 21
|
112
|
+
TO_EXP_POS = 21, // 0 to MAX
|
113
|
+
|
114
|
+
// RANGE : [MIN_EXP, MAX_EXP]
|
115
|
+
|
116
|
+
// The minimum exponent value, beneath which underflow to zero occurs.
|
117
|
+
// Number type: -324 (5e-324)
|
118
|
+
MIN_EXP = -1e7, // -1 to -MAX
|
119
|
+
|
120
|
+
// The maximum exponent value, above which overflow to Infinity occurs.
|
121
|
+
// Number type: 308 (1.7976931348623157e+308)
|
122
|
+
// For MAX_EXP > 1e7, e.g. new BigNumber('1e100000000').plus(1) may be slow.
|
123
|
+
MAX_EXP = 1e7, // 1 to MAX
|
124
|
+
|
125
|
+
// Whether to use cryptographically-secure random number generation, if available.
|
126
|
+
CRYPTO = false, // true or false
|
127
|
+
|
128
|
+
// The modulo mode used when calculating the modulus: a mod n.
|
129
|
+
// The quotient (q = a / n) is calculated according to the corresponding rounding mode.
|
130
|
+
// The remainder (r) is calculated as: r = a - n * q.
|
131
|
+
//
|
132
|
+
// UP 0 The remainder is positive if the dividend is negative, else is negative.
|
133
|
+
// DOWN 1 The remainder has the same sign as the dividend.
|
134
|
+
// This modulo mode is commonly known as 'truncated division' and is
|
135
|
+
// equivalent to (a % n) in JavaScript.
|
136
|
+
// FLOOR 3 The remainder has the same sign as the divisor (Python %).
|
137
|
+
// HALF_EVEN 6 This modulo mode implements the IEEE 754 remainder function.
|
138
|
+
// EUCLID 9 Euclidian division. q = sign(n) * floor(a / abs(n)).
|
139
|
+
// The remainder is always positive.
|
140
|
+
//
|
141
|
+
// The truncated division, floored division, Euclidian division and IEEE 754 remainder
|
142
|
+
// modes are commonly used for the modulus operation.
|
143
|
+
// Although the other rounding modes can also be used, they may not give useful results.
|
144
|
+
MODULO_MODE = 1, // 0 to 9
|
145
|
+
|
146
|
+
// The maximum number of significant digits of the result of the exponentiatedBy operation.
|
147
|
+
// If POW_PRECISION is 0, there will be unlimited significant digits.
|
148
|
+
POW_PRECISION = 0, // 0 to MAX
|
149
|
+
|
150
|
+
// The format specification used by the BigNumber.prototype.toFormat method.
|
151
|
+
FORMAT = {
|
152
|
+
prefix: '',
|
153
|
+
groupSize: 3,
|
154
|
+
secondaryGroupSize: 0,
|
155
|
+
groupSeparator: ',',
|
156
|
+
decimalSeparator: '.',
|
157
|
+
fractionGroupSize: 0,
|
158
|
+
fractionGroupSeparator: '\xA0', // non-breaking space
|
159
|
+
suffix: ''
|
160
|
+
},
|
161
|
+
|
162
|
+
// The alphabet used for base conversion. It must be at least 2 characters long, with no '+',
|
163
|
+
// '-', '.', whitespace, or repeated character.
|
164
|
+
// '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_'
|
165
|
+
ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz',
|
166
|
+
alphabetHasNormalDecimalDigits = true;
|
167
|
+
|
168
|
+
|
169
|
+
//------------------------------------------------------------------------------------------
|
170
|
+
|
171
|
+
|
172
|
+
// CONSTRUCTOR
|
173
|
+
|
174
|
+
|
175
|
+
/*
|
176
|
+
* The BigNumber constructor and exported function.
|
177
|
+
* Create and return a new instance of a BigNumber object.
|
178
|
+
*
|
179
|
+
* v {number|string|BigNumber} A numeric value.
|
180
|
+
* [b] {number} The base of v. Integer, 2 to ALPHABET.length inclusive.
|
181
|
+
*/
|
182
|
+
function BigNumber(v, b) {
|
183
|
+
var alphabet, c, caseChanged, e, i, isNum, len, str,
|
184
|
+
x = this;
|
185
|
+
|
186
|
+
// Enable constructor call without `new`.
|
187
|
+
if (!(x instanceof BigNumber)) return new BigNumber(v, b);
|
188
|
+
|
189
|
+
if (b == null) {
|
190
|
+
|
191
|
+
if (v && v._isBigNumber === true) {
|
192
|
+
x.s = v.s;
|
193
|
+
|
194
|
+
if (!v.c || v.e > MAX_EXP) {
|
195
|
+
x.c = x.e = null;
|
196
|
+
} else if (v.e < MIN_EXP) {
|
197
|
+
x.c = [x.e = 0];
|
198
|
+
} else {
|
199
|
+
x.e = v.e;
|
200
|
+
x.c = v.c.slice();
|
201
|
+
}
|
202
|
+
|
203
|
+
return;
|
204
|
+
}
|
205
|
+
|
206
|
+
if ((isNum = typeof v == 'number') && v * 0 == 0) {
|
207
|
+
|
208
|
+
// Use `1 / n` to handle minus zero also.
|
209
|
+
x.s = 1 / v < 0 ? (v = -v, -1) : 1;
|
210
|
+
|
211
|
+
// Fast path for integers, where n < 2147483648 (2**31).
|
212
|
+
if (v === ~~v) {
|
213
|
+
for (e = 0, i = v; i >= 10; i /= 10, e++);
|
214
|
+
|
215
|
+
if (e > MAX_EXP) {
|
216
|
+
x.c = x.e = null;
|
217
|
+
} else {
|
218
|
+
x.e = e;
|
219
|
+
x.c = [v];
|
220
|
+
}
|
221
|
+
|
222
|
+
return;
|
223
|
+
}
|
224
|
+
|
225
|
+
str = String(v);
|
226
|
+
} else {
|
227
|
+
|
228
|
+
if (!isNumeric.test(str = String(v))) return parseNumeric(x, str, isNum);
|
229
|
+
|
230
|
+
x.s = str.charCodeAt(0) == 45 ? (str = str.slice(1), -1) : 1;
|
231
|
+
}
|
232
|
+
|
233
|
+
// Decimal point?
|
234
|
+
if ((e = str.indexOf('.')) > -1) str = str.replace('.', '');
|
235
|
+
|
236
|
+
// Exponential form?
|
237
|
+
if ((i = str.search(/e/i)) > 0) {
|
238
|
+
|
239
|
+
// Determine exponent.
|
240
|
+
if (e < 0) e = i;
|
241
|
+
e += +str.slice(i + 1);
|
242
|
+
str = str.substring(0, i);
|
243
|
+
} else if (e < 0) {
|
244
|
+
|
245
|
+
// Integer.
|
246
|
+
e = str.length;
|
247
|
+
}
|
248
|
+
|
249
|
+
} else {
|
250
|
+
|
251
|
+
// '[BigNumber Error] Base {not a primitive number|not an integer|out of range}: {b}'
|
252
|
+
intCheck(b, 2, ALPHABET.length, 'Base');
|
253
|
+
|
254
|
+
// Allow exponential notation to be used with base 10 argument, while
|
255
|
+
// also rounding to DECIMAL_PLACES as with other bases.
|
256
|
+
if (b == 10 && alphabetHasNormalDecimalDigits) {
|
257
|
+
x = new BigNumber(v);
|
258
|
+
return round(x, DECIMAL_PLACES + x.e + 1, ROUNDING_MODE);
|
259
|
+
}
|
260
|
+
|
261
|
+
str = String(v);
|
262
|
+
|
263
|
+
if (isNum = typeof v == 'number') {
|
264
|
+
|
265
|
+
// Avoid potential interpretation of Infinity and NaN as base 44+ values.
|
266
|
+
if (v * 0 != 0) return parseNumeric(x, str, isNum, b);
|
267
|
+
|
268
|
+
x.s = 1 / v < 0 ? (str = str.slice(1), -1) : 1;
|
269
|
+
|
270
|
+
// '[BigNumber Error] Number primitive has more than 15 significant digits: {n}'
|
271
|
+
if (BigNumber.DEBUG && str.replace(/^0\.0*|\./, '').length > 15) {
|
272
|
+
throw Error
|
273
|
+
(tooManyDigits + v);
|
274
|
+
}
|
275
|
+
} else {
|
276
|
+
x.s = str.charCodeAt(0) === 45 ? (str = str.slice(1), -1) : 1;
|
277
|
+
}
|
278
|
+
|
279
|
+
alphabet = ALPHABET.slice(0, b);
|
280
|
+
e = i = 0;
|
281
|
+
|
282
|
+
// Check that str is a valid base b number.
|
283
|
+
// Don't use RegExp, so alphabet can contain special characters.
|
284
|
+
for (len = str.length; i < len; i++) {
|
285
|
+
if (alphabet.indexOf(c = str.charAt(i)) < 0) {
|
286
|
+
if (c == '.') {
|
287
|
+
|
288
|
+
// If '.' is not the first character and it has not be found before.
|
289
|
+
if (i > e) {
|
290
|
+
e = len;
|
291
|
+
continue;
|
292
|
+
}
|
293
|
+
} else if (!caseChanged) {
|
294
|
+
|
295
|
+
// Allow e.g. hexadecimal 'FF' as well as 'ff'.
|
296
|
+
if (str == str.toUpperCase() && (str = str.toLowerCase()) ||
|
297
|
+
str == str.toLowerCase() && (str = str.toUpperCase())) {
|
298
|
+
caseChanged = true;
|
299
|
+
i = -1;
|
300
|
+
e = 0;
|
301
|
+
continue;
|
302
|
+
}
|
303
|
+
}
|
304
|
+
|
305
|
+
return parseNumeric(x, String(v), isNum, b);
|
306
|
+
}
|
307
|
+
}
|
308
|
+
|
309
|
+
// Prevent later check for length on converted number.
|
310
|
+
isNum = false;
|
311
|
+
str = convertBase(str, b, 10, x.s);
|
312
|
+
|
313
|
+
// Decimal point?
|
314
|
+
if ((e = str.indexOf('.')) > -1) str = str.replace('.', '');
|
315
|
+
else e = str.length;
|
316
|
+
}
|
317
|
+
|
318
|
+
// Determine leading zeros.
|
319
|
+
for (i = 0; str.charCodeAt(i) === 48; i++);
|
320
|
+
|
321
|
+
// Determine trailing zeros.
|
322
|
+
for (len = str.length; str.charCodeAt(--len) === 48;);
|
323
|
+
|
324
|
+
if (str = str.slice(i, ++len)) {
|
325
|
+
len -= i;
|
326
|
+
|
327
|
+
// '[BigNumber Error] Number primitive has more than 15 significant digits: {n}'
|
328
|
+
if (isNum && BigNumber.DEBUG &&
|
329
|
+
len > 15 && (v > MAX_SAFE_INTEGER || v !== mathfloor(v))) {
|
330
|
+
throw Error
|
331
|
+
(tooManyDigits + (x.s * v));
|
332
|
+
}
|
333
|
+
|
334
|
+
// Overflow?
|
335
|
+
if ((e = e - i - 1) > MAX_EXP) {
|
336
|
+
|
337
|
+
// Infinity.
|
338
|
+
x.c = x.e = null;
|
339
|
+
|
340
|
+
// Underflow?
|
341
|
+
} else if (e < MIN_EXP) {
|
342
|
+
|
343
|
+
// Zero.
|
344
|
+
x.c = [x.e = 0];
|
345
|
+
} else {
|
346
|
+
x.e = e;
|
347
|
+
x.c = [];
|
348
|
+
|
349
|
+
// Transform base
|
350
|
+
|
351
|
+
// e is the base 10 exponent.
|
352
|
+
// i is where to slice str to get the first element of the coefficient array.
|
353
|
+
i = (e + 1) % LOG_BASE;
|
354
|
+
if (e < 0) i += LOG_BASE; // i < 1
|
355
|
+
|
356
|
+
if (i < len) {
|
357
|
+
if (i) x.c.push(+str.slice(0, i));
|
358
|
+
|
359
|
+
for (len -= LOG_BASE; i < len;) {
|
360
|
+
x.c.push(+str.slice(i, i += LOG_BASE));
|
361
|
+
}
|
362
|
+
|
363
|
+
i = LOG_BASE - (str = str.slice(i)).length;
|
364
|
+
} else {
|
365
|
+
i -= len;
|
366
|
+
}
|
367
|
+
|
368
|
+
for (; i--; str += '0');
|
369
|
+
x.c.push(+str);
|
370
|
+
}
|
371
|
+
} else {
|
372
|
+
|
373
|
+
// Zero.
|
374
|
+
x.c = [x.e = 0];
|
375
|
+
}
|
376
|
+
}
|
377
|
+
|
378
|
+
|
379
|
+
// CONSTRUCTOR PROPERTIES
|
380
|
+
|
381
|
+
|
382
|
+
BigNumber.clone = clone;
|
383
|
+
|
384
|
+
BigNumber.ROUND_UP = 0;
|
385
|
+
BigNumber.ROUND_DOWN = 1;
|
386
|
+
BigNumber.ROUND_CEIL = 2;
|
387
|
+
BigNumber.ROUND_FLOOR = 3;
|
388
|
+
BigNumber.ROUND_HALF_UP = 4;
|
389
|
+
BigNumber.ROUND_HALF_DOWN = 5;
|
390
|
+
BigNumber.ROUND_HALF_EVEN = 6;
|
391
|
+
BigNumber.ROUND_HALF_CEIL = 7;
|
392
|
+
BigNumber.ROUND_HALF_FLOOR = 8;
|
393
|
+
BigNumber.EUCLID = 9;
|
394
|
+
|
395
|
+
|
396
|
+
/*
|
397
|
+
* Configure infrequently-changing library-wide settings.
|
398
|
+
*
|
399
|
+
* Accept an object with the following optional properties (if the value of a property is
|
400
|
+
* a number, it must be an integer within the inclusive range stated):
|
401
|
+
*
|
402
|
+
* DECIMAL_PLACES {number} 0 to MAX
|
403
|
+
* ROUNDING_MODE {number} 0 to 8
|
404
|
+
* EXPONENTIAL_AT {number|number[]} -MAX to MAX or [-MAX to 0, 0 to MAX]
|
405
|
+
* RANGE {number|number[]} -MAX to MAX (not zero) or [-MAX to -1, 1 to MAX]
|
406
|
+
* CRYPTO {boolean} true or false
|
407
|
+
* MODULO_MODE {number} 0 to 9
|
408
|
+
* POW_PRECISION {number} 0 to MAX
|
409
|
+
* ALPHABET {string} A string of two or more unique characters which does
|
410
|
+
* not contain '.'.
|
411
|
+
* FORMAT {object} An object with some of the following properties:
|
412
|
+
* prefix {string}
|
413
|
+
* groupSize {number}
|
414
|
+
* secondaryGroupSize {number}
|
415
|
+
* groupSeparator {string}
|
416
|
+
* decimalSeparator {string}
|
417
|
+
* fractionGroupSize {number}
|
418
|
+
* fractionGroupSeparator {string}
|
419
|
+
* suffix {string}
|
420
|
+
*
|
421
|
+
* (The values assigned to the above FORMAT object properties are not checked for validity.)
|
422
|
+
*
|
423
|
+
* E.g.
|
424
|
+
* BigNumber.config({ DECIMAL_PLACES : 20, ROUNDING_MODE : 4 })
|
425
|
+
*
|
426
|
+
* Ignore properties/parameters set to null or undefined, except for ALPHABET.
|
427
|
+
*
|
428
|
+
* Return an object with the properties current values.
|
429
|
+
*/
|
430
|
+
BigNumber.config = BigNumber.set = function (obj) {
|
431
|
+
var p, v;
|
432
|
+
|
433
|
+
if (obj != null) {
|
434
|
+
|
435
|
+
if (typeof obj == 'object') {
|
436
|
+
|
437
|
+
// DECIMAL_PLACES {number} Integer, 0 to MAX inclusive.
|
438
|
+
// '[BigNumber Error] DECIMAL_PLACES {not a primitive number|not an integer|out of range}: {v}'
|
439
|
+
if (obj.hasOwnProperty(p = 'DECIMAL_PLACES')) {
|
440
|
+
v = obj[p];
|
441
|
+
intCheck(v, 0, MAX, p);
|
442
|
+
DECIMAL_PLACES = v;
|
443
|
+
}
|
444
|
+
|
445
|
+
// ROUNDING_MODE {number} Integer, 0 to 8 inclusive.
|
446
|
+
// '[BigNumber Error] ROUNDING_MODE {not a primitive number|not an integer|out of range}: {v}'
|
447
|
+
if (obj.hasOwnProperty(p = 'ROUNDING_MODE')) {
|
448
|
+
v = obj[p];
|
449
|
+
intCheck(v, 0, 8, p);
|
450
|
+
ROUNDING_MODE = v;
|
451
|
+
}
|
452
|
+
|
453
|
+
// EXPONENTIAL_AT {number|number[]}
|
454
|
+
// Integer, -MAX to MAX inclusive or
|
455
|
+
// [integer -MAX to 0 inclusive, 0 to MAX inclusive].
|
456
|
+
// '[BigNumber Error] EXPONENTIAL_AT {not a primitive number|not an integer|out of range}: {v}'
|
457
|
+
if (obj.hasOwnProperty(p = 'EXPONENTIAL_AT')) {
|
458
|
+
v = obj[p];
|
459
|
+
if (v && v.pop) {
|
460
|
+
intCheck(v[0], -MAX, 0, p);
|
461
|
+
intCheck(v[1], 0, MAX, p);
|
462
|
+
TO_EXP_NEG = v[0];
|
463
|
+
TO_EXP_POS = v[1];
|
464
|
+
} else {
|
465
|
+
intCheck(v, -MAX, MAX, p);
|
466
|
+
TO_EXP_NEG = -(TO_EXP_POS = v < 0 ? -v : v);
|
467
|
+
}
|
468
|
+
}
|
469
|
+
|
470
|
+
// RANGE {number|number[]} Non-zero integer, -MAX to MAX inclusive or
|
471
|
+
// [integer -MAX to -1 inclusive, integer 1 to MAX inclusive].
|
472
|
+
// '[BigNumber Error] RANGE {not a primitive number|not an integer|out of range|cannot be zero}: {v}'
|
473
|
+
if (obj.hasOwnProperty(p = 'RANGE')) {
|
474
|
+
v = obj[p];
|
475
|
+
if (v && v.pop) {
|
476
|
+
intCheck(v[0], -MAX, -1, p);
|
477
|
+
intCheck(v[1], 1, MAX, p);
|
478
|
+
MIN_EXP = v[0];
|
479
|
+
MAX_EXP = v[1];
|
480
|
+
} else {
|
481
|
+
intCheck(v, -MAX, MAX, p);
|
482
|
+
if (v) {
|
483
|
+
MIN_EXP = -(MAX_EXP = v < 0 ? -v : v);
|
484
|
+
} else {
|
485
|
+
throw Error
|
486
|
+
(bignumberError + p + ' cannot be zero: ' + v);
|
487
|
+
}
|
488
|
+
}
|
489
|
+
}
|
490
|
+
|
491
|
+
// CRYPTO {boolean} true or false.
|
492
|
+
// '[BigNumber Error] CRYPTO not true or false: {v}'
|
493
|
+
// '[BigNumber Error] crypto unavailable'
|
494
|
+
if (obj.hasOwnProperty(p = 'CRYPTO')) {
|
495
|
+
v = obj[p];
|
496
|
+
if (v === !!v) {
|
497
|
+
if (v) {
|
498
|
+
if (typeof crypto != 'undefined' && crypto &&
|
499
|
+
(crypto.getRandomValues || crypto.randomBytes)) {
|
500
|
+
CRYPTO = v;
|
501
|
+
} else {
|
502
|
+
CRYPTO = !v;
|
503
|
+
throw Error
|
504
|
+
(bignumberError + 'crypto unavailable');
|
505
|
+
}
|
506
|
+
} else {
|
507
|
+
CRYPTO = v;
|
508
|
+
}
|
509
|
+
} else {
|
510
|
+
throw Error
|
511
|
+
(bignumberError + p + ' not true or false: ' + v);
|
512
|
+
}
|
513
|
+
}
|
514
|
+
|
515
|
+
// MODULO_MODE {number} Integer, 0 to 9 inclusive.
|
516
|
+
// '[BigNumber Error] MODULO_MODE {not a primitive number|not an integer|out of range}: {v}'
|
517
|
+
if (obj.hasOwnProperty(p = 'MODULO_MODE')) {
|
518
|
+
v = obj[p];
|
519
|
+
intCheck(v, 0, 9, p);
|
520
|
+
MODULO_MODE = v;
|
521
|
+
}
|
522
|
+
|
523
|
+
// POW_PRECISION {number} Integer, 0 to MAX inclusive.
|
524
|
+
// '[BigNumber Error] POW_PRECISION {not a primitive number|not an integer|out of range}: {v}'
|
525
|
+
if (obj.hasOwnProperty(p = 'POW_PRECISION')) {
|
526
|
+
v = obj[p];
|
527
|
+
intCheck(v, 0, MAX, p);
|
528
|
+
POW_PRECISION = v;
|
529
|
+
}
|
530
|
+
|
531
|
+
// FORMAT {object}
|
532
|
+
// '[BigNumber Error] FORMAT not an object: {v}'
|
533
|
+
if (obj.hasOwnProperty(p = 'FORMAT')) {
|
534
|
+
v = obj[p];
|
535
|
+
if (typeof v == 'object') FORMAT = v;
|
536
|
+
else throw Error
|
537
|
+
(bignumberError + p + ' not an object: ' + v);
|
538
|
+
}
|
539
|
+
|
540
|
+
// ALPHABET {string}
|
541
|
+
// '[BigNumber Error] ALPHABET invalid: {v}'
|
542
|
+
if (obj.hasOwnProperty(p = 'ALPHABET')) {
|
543
|
+
v = obj[p];
|
544
|
+
|
545
|
+
// Disallow if less than two characters,
|
546
|
+
// or if it contains '+', '-', '.', whitespace, or a repeated character.
|
547
|
+
if (typeof v == 'string' && !/^.?$|[+\-.\s]|(.).*\1/.test(v)) {
|
548
|
+
alphabetHasNormalDecimalDigits = v.slice(0, 10) == '0123456789';
|
549
|
+
ALPHABET = v;
|
550
|
+
} else {
|
551
|
+
throw Error
|
552
|
+
(bignumberError + p + ' invalid: ' + v);
|
553
|
+
}
|
554
|
+
}
|
555
|
+
|
556
|
+
} else {
|
557
|
+
|
558
|
+
// '[BigNumber Error] Object expected: {v}'
|
559
|
+
throw Error
|
560
|
+
(bignumberError + 'Object expected: ' + obj);
|
561
|
+
}
|
562
|
+
}
|
563
|
+
|
564
|
+
return {
|
565
|
+
DECIMAL_PLACES: DECIMAL_PLACES,
|
566
|
+
ROUNDING_MODE: ROUNDING_MODE,
|
567
|
+
EXPONENTIAL_AT: [TO_EXP_NEG, TO_EXP_POS],
|
568
|
+
RANGE: [MIN_EXP, MAX_EXP],
|
569
|
+
CRYPTO: CRYPTO,
|
570
|
+
MODULO_MODE: MODULO_MODE,
|
571
|
+
POW_PRECISION: POW_PRECISION,
|
572
|
+
FORMAT: FORMAT,
|
573
|
+
ALPHABET: ALPHABET
|
574
|
+
};
|
575
|
+
};
|
576
|
+
|
577
|
+
|
578
|
+
/*
|
579
|
+
* Return true if v is a BigNumber instance, otherwise return false.
|
580
|
+
*
|
581
|
+
* If BigNumber.DEBUG is true, throw if a BigNumber instance is not well-formed.
|
582
|
+
*
|
583
|
+
* v {any}
|
584
|
+
*
|
585
|
+
* '[BigNumber Error] Invalid BigNumber: {v}'
|
586
|
+
*/
|
587
|
+
BigNumber.isBigNumber = function (v) {
|
588
|
+
if (!v || v._isBigNumber !== true) return false;
|
589
|
+
if (!BigNumber.DEBUG) return true;
|
590
|
+
|
591
|
+
var i, n,
|
592
|
+
c = v.c,
|
593
|
+
e = v.e,
|
594
|
+
s = v.s;
|
595
|
+
|
596
|
+
out: if ({}.toString.call(c) == '[object Array]') {
|
597
|
+
|
598
|
+
if ((s === 1 || s === -1) && e >= -MAX && e <= MAX && e === mathfloor(e)) {
|
599
|
+
|
600
|
+
// If the first element is zero, the BigNumber value must be zero.
|
601
|
+
if (c[0] === 0) {
|
602
|
+
if (e === 0 && c.length === 1) return true;
|
603
|
+
break out;
|
604
|
+
}
|
605
|
+
|
606
|
+
// Calculate number of digits that c[0] should have, based on the exponent.
|
607
|
+
i = (e + 1) % LOG_BASE;
|
608
|
+
if (i < 1) i += LOG_BASE;
|
609
|
+
|
610
|
+
// Calculate number of digits of c[0].
|
611
|
+
//if (Math.ceil(Math.log(c[0] + 1) / Math.LN10) == i) {
|
612
|
+
if (String(c[0]).length == i) {
|
613
|
+
|
614
|
+
for (i = 0; i < c.length; i++) {
|
615
|
+
n = c[i];
|
616
|
+
if (n < 0 || n >= BASE || n !== mathfloor(n)) break out;
|
617
|
+
}
|
618
|
+
|
619
|
+
// Last element cannot be zero, unless it is the only element.
|
620
|
+
if (n !== 0) return true;
|
621
|
+
}
|
622
|
+
}
|
623
|
+
|
624
|
+
// Infinity/NaN
|
625
|
+
} else if (c === null && e === null && (s === null || s === 1 || s === -1)) {
|
626
|
+
return true;
|
627
|
+
}
|
628
|
+
|
629
|
+
throw Error
|
630
|
+
(bignumberError + 'Invalid BigNumber: ' + v);
|
631
|
+
};
|
632
|
+
|
633
|
+
|
634
|
+
/*
|
635
|
+
* Return a new BigNumber whose value is the maximum of the arguments.
|
636
|
+
*
|
637
|
+
* arguments {number|string|BigNumber}
|
638
|
+
*/
|
639
|
+
BigNumber.maximum = BigNumber.max = function () {
|
640
|
+
return maxOrMin(arguments, -1);
|
641
|
+
};
|
642
|
+
|
643
|
+
|
644
|
+
/*
|
645
|
+
* Return a new BigNumber whose value is the minimum of the arguments.
|
646
|
+
*
|
647
|
+
* arguments {number|string|BigNumber}
|
648
|
+
*/
|
649
|
+
BigNumber.minimum = BigNumber.min = function () {
|
650
|
+
return maxOrMin(arguments, 1);
|
651
|
+
};
|
652
|
+
|
653
|
+
|
654
|
+
/*
|
655
|
+
* Return a new BigNumber with a random value equal to or greater than 0 and less than 1,
|
656
|
+
* and with dp, or DECIMAL_PLACES if dp is omitted, decimal places (or less if trailing
|
657
|
+
* zeros are produced).
|
658
|
+
*
|
659
|
+
* [dp] {number} Decimal places. Integer, 0 to MAX inclusive.
|
660
|
+
*
|
661
|
+
* '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {dp}'
|
662
|
+
* '[BigNumber Error] crypto unavailable'
|
663
|
+
*/
|
664
|
+
BigNumber.random = (function () {
|
665
|
+
var pow2_53 = 0x20000000000000;
|
666
|
+
|
667
|
+
// Return a 53 bit integer n, where 0 <= n < 9007199254740992.
|
668
|
+
// Check if Math.random() produces more than 32 bits of randomness.
|
669
|
+
// If it does, assume at least 53 bits are produced, otherwise assume at least 30 bits.
|
670
|
+
// 0x40000000 is 2^30, 0x800000 is 2^23, 0x1fffff is 2^21 - 1.
|
671
|
+
var random53bitInt = (Math.random() * pow2_53) & 0x1fffff
|
672
|
+
? function () { return mathfloor(Math.random() * pow2_53); }
|
673
|
+
: function () { return ((Math.random() * 0x40000000 | 0) * 0x800000) +
|
674
|
+
(Math.random() * 0x800000 | 0); };
|
675
|
+
|
676
|
+
return function (dp) {
|
677
|
+
var a, b, e, k, v,
|
678
|
+
i = 0,
|
679
|
+
c = [],
|
680
|
+
rand = new BigNumber(ONE);
|
681
|
+
|
682
|
+
if (dp == null) dp = DECIMAL_PLACES;
|
683
|
+
else intCheck(dp, 0, MAX);
|
684
|
+
|
685
|
+
k = mathceil(dp / LOG_BASE);
|
686
|
+
|
687
|
+
if (CRYPTO) {
|
688
|
+
|
689
|
+
// Browsers supporting crypto.getRandomValues.
|
690
|
+
if (crypto.getRandomValues) {
|
691
|
+
|
692
|
+
a = crypto.getRandomValues(new Uint32Array(k *= 2));
|
693
|
+
|
694
|
+
for (; i < k;) {
|
695
|
+
|
696
|
+
// 53 bits:
|
697
|
+
// ((Math.pow(2, 32) - 1) * Math.pow(2, 21)).toString(2)
|
698
|
+
// 11111 11111111 11111111 11111111 11100000 00000000 00000000
|
699
|
+
// ((Math.pow(2, 32) - 1) >>> 11).toString(2)
|
700
|
+
// 11111 11111111 11111111
|
701
|
+
// 0x20000 is 2^21.
|
702
|
+
v = a[i] * 0x20000 + (a[i + 1] >>> 11);
|
703
|
+
|
704
|
+
// Rejection sampling:
|
705
|
+
// 0 <= v < 9007199254740992
|
706
|
+
// Probability that v >= 9e15, is
|
707
|
+
// 7199254740992 / 9007199254740992 ~= 0.0008, i.e. 1 in 1251
|
708
|
+
if (v >= 9e15) {
|
709
|
+
b = crypto.getRandomValues(new Uint32Array(2));
|
710
|
+
a[i] = b[0];
|
711
|
+
a[i + 1] = b[1];
|
712
|
+
} else {
|
713
|
+
|
714
|
+
// 0 <= v <= 8999999999999999
|
715
|
+
// 0 <= (v % 1e14) <= 99999999999999
|
716
|
+
c.push(v % 1e14);
|
717
|
+
i += 2;
|
718
|
+
}
|
719
|
+
}
|
720
|
+
i = k / 2;
|
721
|
+
|
722
|
+
// Node.js supporting crypto.randomBytes.
|
723
|
+
} else if (crypto.randomBytes) {
|
724
|
+
|
725
|
+
// buffer
|
726
|
+
a = crypto.randomBytes(k *= 7);
|
727
|
+
|
728
|
+
for (; i < k;) {
|
729
|
+
|
730
|
+
// 0x1000000000000 is 2^48, 0x10000000000 is 2^40
|
731
|
+
// 0x100000000 is 2^32, 0x1000000 is 2^24
|
732
|
+
// 11111 11111111 11111111 11111111 11111111 11111111 11111111
|
733
|
+
// 0 <= v < 9007199254740992
|
734
|
+
v = ((a[i] & 31) * 0x1000000000000) + (a[i + 1] * 0x10000000000) +
|
735
|
+
(a[i + 2] * 0x100000000) + (a[i + 3] * 0x1000000) +
|
736
|
+
(a[i + 4] << 16) + (a[i + 5] << 8) + a[i + 6];
|
737
|
+
|
738
|
+
if (v >= 9e15) {
|
739
|
+
crypto.randomBytes(7).copy(a, i);
|
740
|
+
} else {
|
741
|
+
|
742
|
+
// 0 <= (v % 1e14) <= 99999999999999
|
743
|
+
c.push(v % 1e14);
|
744
|
+
i += 7;
|
745
|
+
}
|
746
|
+
}
|
747
|
+
i = k / 7;
|
748
|
+
} else {
|
749
|
+
CRYPTO = false;
|
750
|
+
throw Error
|
751
|
+
(bignumberError + 'crypto unavailable');
|
752
|
+
}
|
753
|
+
}
|
754
|
+
|
755
|
+
// Use Math.random.
|
756
|
+
if (!CRYPTO) {
|
757
|
+
|
758
|
+
for (; i < k;) {
|
759
|
+
v = random53bitInt();
|
760
|
+
if (v < 9e15) c[i++] = v % 1e14;
|
761
|
+
}
|
762
|
+
}
|
763
|
+
|
764
|
+
k = c[--i];
|
765
|
+
dp %= LOG_BASE;
|
766
|
+
|
767
|
+
// Convert trailing digits to zeros according to dp.
|
768
|
+
if (k && dp) {
|
769
|
+
v = POWS_TEN[LOG_BASE - dp];
|
770
|
+
c[i] = mathfloor(k / v) * v;
|
771
|
+
}
|
772
|
+
|
773
|
+
// Remove trailing elements which are zero.
|
774
|
+
for (; c[i] === 0; c.pop(), i--);
|
775
|
+
|
776
|
+
// Zero?
|
777
|
+
if (i < 0) {
|
778
|
+
c = [e = 0];
|
779
|
+
} else {
|
780
|
+
|
781
|
+
// Remove leading elements which are zero and adjust exponent accordingly.
|
782
|
+
for (e = -1 ; c[0] === 0; c.splice(0, 1), e -= LOG_BASE);
|
783
|
+
|
784
|
+
// Count the digits of the first element of c to determine leading zeros, and...
|
785
|
+
for (i = 1, v = c[0]; v >= 10; v /= 10, i++);
|
786
|
+
|
787
|
+
// adjust the exponent accordingly.
|
788
|
+
if (i < LOG_BASE) e -= LOG_BASE - i;
|
789
|
+
}
|
790
|
+
|
791
|
+
rand.e = e;
|
792
|
+
rand.c = c;
|
793
|
+
return rand;
|
794
|
+
};
|
795
|
+
})();
|
796
|
+
|
797
|
+
|
798
|
+
/*
|
799
|
+
* Return a BigNumber whose value is the sum of the arguments.
|
800
|
+
*
|
801
|
+
* arguments {number|string|BigNumber}
|
802
|
+
*/
|
803
|
+
BigNumber.sum = function () {
|
804
|
+
var i = 1,
|
805
|
+
args = arguments,
|
806
|
+
sum = new BigNumber(args[0]);
|
807
|
+
for (; i < args.length;) sum = sum.plus(args[i++]);
|
808
|
+
return sum;
|
809
|
+
};
|
810
|
+
|
811
|
+
|
812
|
+
// PRIVATE FUNCTIONS
|
813
|
+
|
814
|
+
|
815
|
+
// Called by BigNumber and BigNumber.prototype.toString.
|
816
|
+
convertBase = (function () {
|
817
|
+
var decimal = '0123456789';
|
818
|
+
|
819
|
+
/*
|
820
|
+
* Convert string of baseIn to an array of numbers of baseOut.
|
821
|
+
* Eg. toBaseOut('255', 10, 16) returns [15, 15].
|
822
|
+
* Eg. toBaseOut('ff', 16, 10) returns [2, 5, 5].
|
823
|
+
*/
|
824
|
+
function toBaseOut(str, baseIn, baseOut, alphabet) {
|
825
|
+
var j,
|
826
|
+
arr = [0],
|
827
|
+
arrL,
|
828
|
+
i = 0,
|
829
|
+
len = str.length;
|
830
|
+
|
831
|
+
for (; i < len;) {
|
832
|
+
for (arrL = arr.length; arrL--; arr[arrL] *= baseIn);
|
833
|
+
|
834
|
+
arr[0] += alphabet.indexOf(str.charAt(i++));
|
835
|
+
|
836
|
+
for (j = 0; j < arr.length; j++) {
|
837
|
+
|
838
|
+
if (arr[j] > baseOut - 1) {
|
839
|
+
if (arr[j + 1] == null) arr[j + 1] = 0;
|
840
|
+
arr[j + 1] += arr[j] / baseOut | 0;
|
841
|
+
arr[j] %= baseOut;
|
842
|
+
}
|
843
|
+
}
|
844
|
+
}
|
845
|
+
|
846
|
+
return arr.reverse();
|
847
|
+
}
|
848
|
+
|
849
|
+
// Convert a numeric string of baseIn to a numeric string of baseOut.
|
850
|
+
// If the caller is toString, we are converting from base 10 to baseOut.
|
851
|
+
// If the caller is BigNumber, we are converting from baseIn to base 10.
|
852
|
+
return function (str, baseIn, baseOut, sign, callerIsToString) {
|
853
|
+
var alphabet, d, e, k, r, x, xc, y,
|
854
|
+
i = str.indexOf('.'),
|
855
|
+
dp = DECIMAL_PLACES,
|
856
|
+
rm = ROUNDING_MODE;
|
857
|
+
|
858
|
+
// Non-integer.
|
859
|
+
if (i >= 0) {
|
860
|
+
k = POW_PRECISION;
|
861
|
+
|
862
|
+
// Unlimited precision.
|
863
|
+
POW_PRECISION = 0;
|
864
|
+
str = str.replace('.', '');
|
865
|
+
y = new BigNumber(baseIn);
|
866
|
+
x = y.pow(str.length - i);
|
867
|
+
POW_PRECISION = k;
|
868
|
+
|
869
|
+
// Convert str as if an integer, then restore the fraction part by dividing the
|
870
|
+
// result by its base raised to a power.
|
871
|
+
|
872
|
+
y.c = toBaseOut(toFixedPoint(coeffToString(x.c), x.e, '0'),
|
873
|
+
10, baseOut, decimal);
|
874
|
+
y.e = y.c.length;
|
875
|
+
}
|
876
|
+
|
877
|
+
// Convert the number as integer.
|
878
|
+
|
879
|
+
xc = toBaseOut(str, baseIn, baseOut, callerIsToString
|
880
|
+
? (alphabet = ALPHABET, decimal)
|
881
|
+
: (alphabet = decimal, ALPHABET));
|
882
|
+
|
883
|
+
// xc now represents str as an integer and converted to baseOut. e is the exponent.
|
884
|
+
e = k = xc.length;
|
885
|
+
|
886
|
+
// Remove trailing zeros.
|
887
|
+
for (; xc[--k] == 0; xc.pop());
|
888
|
+
|
889
|
+
// Zero?
|
890
|
+
if (!xc[0]) return alphabet.charAt(0);
|
891
|
+
|
892
|
+
// Does str represent an integer? If so, no need for the division.
|
893
|
+
if (i < 0) {
|
894
|
+
--e;
|
895
|
+
} else {
|
896
|
+
x.c = xc;
|
897
|
+
x.e = e;
|
898
|
+
|
899
|
+
// The sign is needed for correct rounding.
|
900
|
+
x.s = sign;
|
901
|
+
x = div(x, y, dp, rm, baseOut);
|
902
|
+
xc = x.c;
|
903
|
+
r = x.r;
|
904
|
+
e = x.e;
|
905
|
+
}
|
906
|
+
|
907
|
+
// xc now represents str converted to baseOut.
|
908
|
+
|
909
|
+
// THe index of the rounding digit.
|
910
|
+
d = e + dp + 1;
|
911
|
+
|
912
|
+
// The rounding digit: the digit to the right of the digit that may be rounded up.
|
913
|
+
i = xc[d];
|
914
|
+
|
915
|
+
// Look at the rounding digits and mode to determine whether to round up.
|
916
|
+
|
917
|
+
k = baseOut / 2;
|
918
|
+
r = r || d < 0 || xc[d + 1] != null;
|
919
|
+
|
920
|
+
r = rm < 4 ? (i != null || r) && (rm == 0 || rm == (x.s < 0 ? 3 : 2))
|
921
|
+
: i > k || i == k &&(rm == 4 || r || rm == 6 && xc[d - 1] & 1 ||
|
922
|
+
rm == (x.s < 0 ? 8 : 7));
|
923
|
+
|
924
|
+
// If the index of the rounding digit is not greater than zero, or xc represents
|
925
|
+
// zero, then the result of the base conversion is zero or, if rounding up, a value
|
926
|
+
// such as 0.00001.
|
927
|
+
if (d < 1 || !xc[0]) {
|
928
|
+
|
929
|
+
// 1^-dp or 0
|
930
|
+
str = r ? toFixedPoint(alphabet.charAt(1), -dp, alphabet.charAt(0)) : alphabet.charAt(0);
|
931
|
+
} else {
|
932
|
+
|
933
|
+
// Truncate xc to the required number of decimal places.
|
934
|
+
xc.length = d;
|
935
|
+
|
936
|
+
// Round up?
|
937
|
+
if (r) {
|
938
|
+
|
939
|
+
// Rounding up may mean the previous digit has to be rounded up and so on.
|
940
|
+
for (--baseOut; ++xc[--d] > baseOut;) {
|
941
|
+
xc[d] = 0;
|
942
|
+
|
943
|
+
if (!d) {
|
944
|
+
++e;
|
945
|
+
xc = [1].concat(xc);
|
946
|
+
}
|
947
|
+
}
|
948
|
+
}
|
949
|
+
|
950
|
+
// Determine trailing zeros.
|
951
|
+
for (k = xc.length; !xc[--k];);
|
952
|
+
|
953
|
+
// E.g. [4, 11, 15] becomes 4bf.
|
954
|
+
for (i = 0, str = ''; i <= k; str += alphabet.charAt(xc[i++]));
|
955
|
+
|
956
|
+
// Add leading zeros, decimal point and trailing zeros as required.
|
957
|
+
str = toFixedPoint(str, e, alphabet.charAt(0));
|
958
|
+
}
|
959
|
+
|
960
|
+
// The caller will add the sign.
|
961
|
+
return str;
|
962
|
+
};
|
963
|
+
})();
|
964
|
+
|
965
|
+
|
966
|
+
// Perform division in the specified base. Called by div and convertBase.
|
967
|
+
div = (function () {
|
968
|
+
|
969
|
+
// Assume non-zero x and k.
|
970
|
+
function multiply(x, k, base) {
|
971
|
+
var m, temp, xlo, xhi,
|
972
|
+
carry = 0,
|
973
|
+
i = x.length,
|
974
|
+
klo = k % SQRT_BASE,
|
975
|
+
khi = k / SQRT_BASE | 0;
|
976
|
+
|
977
|
+
for (x = x.slice(); i--;) {
|
978
|
+
xlo = x[i] % SQRT_BASE;
|
979
|
+
xhi = x[i] / SQRT_BASE | 0;
|
980
|
+
m = khi * xlo + xhi * klo;
|
981
|
+
temp = klo * xlo + ((m % SQRT_BASE) * SQRT_BASE) + carry;
|
982
|
+
carry = (temp / base | 0) + (m / SQRT_BASE | 0) + khi * xhi;
|
983
|
+
x[i] = temp % base;
|
984
|
+
}
|
985
|
+
|
986
|
+
if (carry) x = [carry].concat(x);
|
987
|
+
|
988
|
+
return x;
|
989
|
+
}
|
990
|
+
|
991
|
+
function compare(a, b, aL, bL) {
|
992
|
+
var i, cmp;
|
993
|
+
|
994
|
+
if (aL != bL) {
|
995
|
+
cmp = aL > bL ? 1 : -1;
|
996
|
+
} else {
|
997
|
+
|
998
|
+
for (i = cmp = 0; i < aL; i++) {
|
999
|
+
|
1000
|
+
if (a[i] != b[i]) {
|
1001
|
+
cmp = a[i] > b[i] ? 1 : -1;
|
1002
|
+
break;
|
1003
|
+
}
|
1004
|
+
}
|
1005
|
+
}
|
1006
|
+
|
1007
|
+
return cmp;
|
1008
|
+
}
|
1009
|
+
|
1010
|
+
function subtract(a, b, aL, base) {
|
1011
|
+
var i = 0;
|
1012
|
+
|
1013
|
+
// Subtract b from a.
|
1014
|
+
for (; aL--;) {
|
1015
|
+
a[aL] -= i;
|
1016
|
+
i = a[aL] < b[aL] ? 1 : 0;
|
1017
|
+
a[aL] = i * base + a[aL] - b[aL];
|
1018
|
+
}
|
1019
|
+
|
1020
|
+
// Remove leading zeros.
|
1021
|
+
for (; !a[0] && a.length > 1; a.splice(0, 1));
|
1022
|
+
}
|
1023
|
+
|
1024
|
+
// x: dividend, y: divisor.
|
1025
|
+
return function (x, y, dp, rm, base) {
|
1026
|
+
var cmp, e, i, more, n, prod, prodL, q, qc, rem, remL, rem0, xi, xL, yc0,
|
1027
|
+
yL, yz,
|
1028
|
+
s = x.s == y.s ? 1 : -1,
|
1029
|
+
xc = x.c,
|
1030
|
+
yc = y.c;
|
1031
|
+
|
1032
|
+
// Either NaN, Infinity or 0?
|
1033
|
+
if (!xc || !xc[0] || !yc || !yc[0]) {
|
1034
|
+
|
1035
|
+
return new BigNumber(
|
1036
|
+
|
1037
|
+
// Return NaN if either NaN, or both Infinity or 0.
|
1038
|
+
!x.s || !y.s || (xc ? yc && xc[0] == yc[0] : !yc) ? NaN :
|
1039
|
+
|
1040
|
+
// Return ±0 if x is ±0 or y is ±Infinity, or return ±Infinity as y is ±0.
|
1041
|
+
xc && xc[0] == 0 || !yc ? s * 0 : s / 0
|
1042
|
+
);
|
1043
|
+
}
|
1044
|
+
|
1045
|
+
q = new BigNumber(s);
|
1046
|
+
qc = q.c = [];
|
1047
|
+
e = x.e - y.e;
|
1048
|
+
s = dp + e + 1;
|
1049
|
+
|
1050
|
+
if (!base) {
|
1051
|
+
base = BASE;
|
1052
|
+
e = bitFloor(x.e / LOG_BASE) - bitFloor(y.e / LOG_BASE);
|
1053
|
+
s = s / LOG_BASE | 0;
|
1054
|
+
}
|
1055
|
+
|
1056
|
+
// Result exponent may be one less then the current value of e.
|
1057
|
+
// The coefficients of the BigNumbers from convertBase may have trailing zeros.
|
1058
|
+
for (i = 0; yc[i] == (xc[i] || 0); i++);
|
1059
|
+
|
1060
|
+
if (yc[i] > (xc[i] || 0)) e--;
|
1061
|
+
|
1062
|
+
if (s < 0) {
|
1063
|
+
qc.push(1);
|
1064
|
+
more = true;
|
1065
|
+
} else {
|
1066
|
+
xL = xc.length;
|
1067
|
+
yL = yc.length;
|
1068
|
+
i = 0;
|
1069
|
+
s += 2;
|
1070
|
+
|
1071
|
+
// Normalise xc and yc so highest order digit of yc is >= base / 2.
|
1072
|
+
|
1073
|
+
n = mathfloor(base / (yc[0] + 1));
|
1074
|
+
|
1075
|
+
// Not necessary, but to handle odd bases where yc[0] == (base / 2) - 1.
|
1076
|
+
// if (n > 1 || n++ == 1 && yc[0] < base / 2) {
|
1077
|
+
if (n > 1) {
|
1078
|
+
yc = multiply(yc, n, base);
|
1079
|
+
xc = multiply(xc, n, base);
|
1080
|
+
yL = yc.length;
|
1081
|
+
xL = xc.length;
|
1082
|
+
}
|
1083
|
+
|
1084
|
+
xi = yL;
|
1085
|
+
rem = xc.slice(0, yL);
|
1086
|
+
remL = rem.length;
|
1087
|
+
|
1088
|
+
// Add zeros to make remainder as long as divisor.
|
1089
|
+
for (; remL < yL; rem[remL++] = 0);
|
1090
|
+
yz = yc.slice();
|
1091
|
+
yz = [0].concat(yz);
|
1092
|
+
yc0 = yc[0];
|
1093
|
+
if (yc[1] >= base / 2) yc0++;
|
1094
|
+
// Not necessary, but to prevent trial digit n > base, when using base 3.
|
1095
|
+
// else if (base == 3 && yc0 == 1) yc0 = 1 + 1e-15;
|
1096
|
+
|
1097
|
+
do {
|
1098
|
+
n = 0;
|
1099
|
+
|
1100
|
+
// Compare divisor and remainder.
|
1101
|
+
cmp = compare(yc, rem, yL, remL);
|
1102
|
+
|
1103
|
+
// If divisor < remainder.
|
1104
|
+
if (cmp < 0) {
|
1105
|
+
|
1106
|
+
// Calculate trial digit, n.
|
1107
|
+
|
1108
|
+
rem0 = rem[0];
|
1109
|
+
if (yL != remL) rem0 = rem0 * base + (rem[1] || 0);
|
1110
|
+
|
1111
|
+
// n is how many times the divisor goes into the current remainder.
|
1112
|
+
n = mathfloor(rem0 / yc0);
|
1113
|
+
|
1114
|
+
// Algorithm:
|
1115
|
+
// product = divisor multiplied by trial digit (n).
|
1116
|
+
// Compare product and remainder.
|
1117
|
+
// If product is greater than remainder:
|
1118
|
+
// Subtract divisor from product, decrement trial digit.
|
1119
|
+
// Subtract product from remainder.
|
1120
|
+
// If product was less than remainder at the last compare:
|
1121
|
+
// Compare new remainder and divisor.
|
1122
|
+
// If remainder is greater than divisor:
|
1123
|
+
// Subtract divisor from remainder, increment trial digit.
|
1124
|
+
|
1125
|
+
if (n > 1) {
|
1126
|
+
|
1127
|
+
// n may be > base only when base is 3.
|
1128
|
+
if (n >= base) n = base - 1;
|
1129
|
+
|
1130
|
+
// product = divisor * trial digit.
|
1131
|
+
prod = multiply(yc, n, base);
|
1132
|
+
prodL = prod.length;
|
1133
|
+
remL = rem.length;
|
1134
|
+
|
1135
|
+
// Compare product and remainder.
|
1136
|
+
// If product > remainder then trial digit n too high.
|
1137
|
+
// n is 1 too high about 5% of the time, and is not known to have
|
1138
|
+
// ever been more than 1 too high.
|
1139
|
+
while (compare(prod, rem, prodL, remL) == 1) {
|
1140
|
+
n--;
|
1141
|
+
|
1142
|
+
// Subtract divisor from product.
|
1143
|
+
subtract(prod, yL < prodL ? yz : yc, prodL, base);
|
1144
|
+
prodL = prod.length;
|
1145
|
+
cmp = 1;
|
1146
|
+
}
|
1147
|
+
} else {
|
1148
|
+
|
1149
|
+
// n is 0 or 1, cmp is -1.
|
1150
|
+
// If n is 0, there is no need to compare yc and rem again below,
|
1151
|
+
// so change cmp to 1 to avoid it.
|
1152
|
+
// If n is 1, leave cmp as -1, so yc and rem are compared again.
|
1153
|
+
if (n == 0) {
|
1154
|
+
|
1155
|
+
// divisor < remainder, so n must be at least 1.
|
1156
|
+
cmp = n = 1;
|
1157
|
+
}
|
1158
|
+
|
1159
|
+
// product = divisor
|
1160
|
+
prod = yc.slice();
|
1161
|
+
prodL = prod.length;
|
1162
|
+
}
|
1163
|
+
|
1164
|
+
if (prodL < remL) prod = [0].concat(prod);
|
1165
|
+
|
1166
|
+
// Subtract product from remainder.
|
1167
|
+
subtract(rem, prod, remL, base);
|
1168
|
+
remL = rem.length;
|
1169
|
+
|
1170
|
+
// If product was < remainder.
|
1171
|
+
if (cmp == -1) {
|
1172
|
+
|
1173
|
+
// Compare divisor and new remainder.
|
1174
|
+
// If divisor < new remainder, subtract divisor from remainder.
|
1175
|
+
// Trial digit n too low.
|
1176
|
+
// n is 1 too low about 5% of the time, and very rarely 2 too low.
|
1177
|
+
while (compare(yc, rem, yL, remL) < 1) {
|
1178
|
+
n++;
|
1179
|
+
|
1180
|
+
// Subtract divisor from remainder.
|
1181
|
+
subtract(rem, yL < remL ? yz : yc, remL, base);
|
1182
|
+
remL = rem.length;
|
1183
|
+
}
|
1184
|
+
}
|
1185
|
+
} else if (cmp === 0) {
|
1186
|
+
n++;
|
1187
|
+
rem = [0];
|
1188
|
+
} // else cmp === 1 and n will be 0
|
1189
|
+
|
1190
|
+
// Add the next digit, n, to the result array.
|
1191
|
+
qc[i++] = n;
|
1192
|
+
|
1193
|
+
// Update the remainder.
|
1194
|
+
if (rem[0]) {
|
1195
|
+
rem[remL++] = xc[xi] || 0;
|
1196
|
+
} else {
|
1197
|
+
rem = [xc[xi]];
|
1198
|
+
remL = 1;
|
1199
|
+
}
|
1200
|
+
} while ((xi++ < xL || rem[0] != null) && s--);
|
1201
|
+
|
1202
|
+
more = rem[0] != null;
|
1203
|
+
|
1204
|
+
// Leading zero?
|
1205
|
+
if (!qc[0]) qc.splice(0, 1);
|
1206
|
+
}
|
1207
|
+
|
1208
|
+
if (base == BASE) {
|
1209
|
+
|
1210
|
+
// To calculate q.e, first get the number of digits of qc[0].
|
1211
|
+
for (i = 1, s = qc[0]; s >= 10; s /= 10, i++);
|
1212
|
+
|
1213
|
+
round(q, dp + (q.e = i + e * LOG_BASE - 1) + 1, rm, more);
|
1214
|
+
|
1215
|
+
// Caller is convertBase.
|
1216
|
+
} else {
|
1217
|
+
q.e = e;
|
1218
|
+
q.r = +more;
|
1219
|
+
}
|
1220
|
+
|
1221
|
+
return q;
|
1222
|
+
};
|
1223
|
+
})();
|
1224
|
+
|
1225
|
+
|
1226
|
+
/*
|
1227
|
+
* Return a string representing the value of BigNumber n in fixed-point or exponential
|
1228
|
+
* notation rounded to the specified decimal places or significant digits.
|
1229
|
+
*
|
1230
|
+
* n: a BigNumber.
|
1231
|
+
* i: the index of the last digit required (i.e. the digit that may be rounded up).
|
1232
|
+
* rm: the rounding mode.
|
1233
|
+
* id: 1 (toExponential) or 2 (toPrecision).
|
1234
|
+
*/
|
1235
|
+
function format(n, i, rm, id) {
|
1236
|
+
var c0, e, ne, len, str;
|
1237
|
+
|
1238
|
+
if (rm == null) rm = ROUNDING_MODE;
|
1239
|
+
else intCheck(rm, 0, 8);
|
1240
|
+
|
1241
|
+
if (!n.c) return n.toString();
|
1242
|
+
|
1243
|
+
c0 = n.c[0];
|
1244
|
+
ne = n.e;
|
1245
|
+
|
1246
|
+
if (i == null) {
|
1247
|
+
str = coeffToString(n.c);
|
1248
|
+
str = id == 1 || id == 2 && (ne <= TO_EXP_NEG || ne >= TO_EXP_POS)
|
1249
|
+
? toExponential(str, ne)
|
1250
|
+
: toFixedPoint(str, ne, '0');
|
1251
|
+
} else {
|
1252
|
+
n = round(new BigNumber(n), i, rm);
|
1253
|
+
|
1254
|
+
// n.e may have changed if the value was rounded up.
|
1255
|
+
e = n.e;
|
1256
|
+
|
1257
|
+
str = coeffToString(n.c);
|
1258
|
+
len = str.length;
|
1259
|
+
|
1260
|
+
// toPrecision returns exponential notation if the number of significant digits
|
1261
|
+
// specified is less than the number of digits necessary to represent the integer
|
1262
|
+
// part of the value in fixed-point notation.
|
1263
|
+
|
1264
|
+
// Exponential notation.
|
1265
|
+
if (id == 1 || id == 2 && (i <= e || e <= TO_EXP_NEG)) {
|
1266
|
+
|
1267
|
+
// Append zeros?
|
1268
|
+
for (; len < i; str += '0', len++);
|
1269
|
+
str = toExponential(str, e);
|
1270
|
+
|
1271
|
+
// Fixed-point notation.
|
1272
|
+
} else {
|
1273
|
+
i -= ne;
|
1274
|
+
str = toFixedPoint(str, e, '0');
|
1275
|
+
|
1276
|
+
// Append zeros?
|
1277
|
+
if (e + 1 > len) {
|
1278
|
+
if (--i > 0) for (str += '.'; i--; str += '0');
|
1279
|
+
} else {
|
1280
|
+
i += e - len;
|
1281
|
+
if (i > 0) {
|
1282
|
+
if (e + 1 == len) str += '.';
|
1283
|
+
for (; i--; str += '0');
|
1284
|
+
}
|
1285
|
+
}
|
1286
|
+
}
|
1287
|
+
}
|
1288
|
+
|
1289
|
+
return n.s < 0 && c0 ? '-' + str : str;
|
1290
|
+
}
|
1291
|
+
|
1292
|
+
|
1293
|
+
// Handle BigNumber.max and BigNumber.min.
|
1294
|
+
// If any number is NaN, return NaN.
|
1295
|
+
function maxOrMin(args, n) {
|
1296
|
+
var k, y,
|
1297
|
+
i = 1,
|
1298
|
+
x = new BigNumber(args[0]);
|
1299
|
+
|
1300
|
+
for (; i < args.length; i++) {
|
1301
|
+
y = new BigNumber(args[i]);
|
1302
|
+
if (!y.s || (k = compare(x, y)) === n || k === 0 && x.s === n) {
|
1303
|
+
x = y;
|
1304
|
+
}
|
1305
|
+
}
|
1306
|
+
|
1307
|
+
return x;
|
1308
|
+
}
|
1309
|
+
|
1310
|
+
|
1311
|
+
/*
|
1312
|
+
* Strip trailing zeros, calculate base 10 exponent and check against MIN_EXP and MAX_EXP.
|
1313
|
+
* Called by minus, plus and times.
|
1314
|
+
*/
|
1315
|
+
function normalise(n, c, e) {
|
1316
|
+
var i = 1,
|
1317
|
+
j = c.length;
|
1318
|
+
|
1319
|
+
// Remove trailing zeros.
|
1320
|
+
for (; !c[--j]; c.pop());
|
1321
|
+
|
1322
|
+
// Calculate the base 10 exponent. First get the number of digits of c[0].
|
1323
|
+
for (j = c[0]; j >= 10; j /= 10, i++);
|
1324
|
+
|
1325
|
+
// Overflow?
|
1326
|
+
if ((e = i + e * LOG_BASE - 1) > MAX_EXP) {
|
1327
|
+
|
1328
|
+
// Infinity.
|
1329
|
+
n.c = n.e = null;
|
1330
|
+
|
1331
|
+
// Underflow?
|
1332
|
+
} else if (e < MIN_EXP) {
|
1333
|
+
|
1334
|
+
// Zero.
|
1335
|
+
n.c = [n.e = 0];
|
1336
|
+
} else {
|
1337
|
+
n.e = e;
|
1338
|
+
n.c = c;
|
1339
|
+
}
|
1340
|
+
|
1341
|
+
return n;
|
1342
|
+
}
|
1343
|
+
|
1344
|
+
|
1345
|
+
// Handle values that fail the validity test in BigNumber.
|
1346
|
+
parseNumeric = (function () {
|
1347
|
+
var basePrefix = /^(-?)0([xbo])(?=\w[\w.]*$)/i,
|
1348
|
+
dotAfter = /^([^.]+)\.$/,
|
1349
|
+
dotBefore = /^\.([^.]+)$/,
|
1350
|
+
isInfinityOrNaN = /^-?(Infinity|NaN)$/,
|
1351
|
+
whitespaceOrPlus = /^\s*\+(?=[\w.])|^\s+|\s+$/g;
|
1352
|
+
|
1353
|
+
return function (x, str, isNum, b) {
|
1354
|
+
var base,
|
1355
|
+
s = isNum ? str : str.replace(whitespaceOrPlus, '');
|
1356
|
+
|
1357
|
+
// No exception on ±Infinity or NaN.
|
1358
|
+
if (isInfinityOrNaN.test(s)) {
|
1359
|
+
x.s = isNaN(s) ? null : s < 0 ? -1 : 1;
|
1360
|
+
} else {
|
1361
|
+
if (!isNum) {
|
1362
|
+
|
1363
|
+
// basePrefix = /^(-?)0([xbo])(?=\w[\w.]*$)/i
|
1364
|
+
s = s.replace(basePrefix, function (m, p1, p2) {
|
1365
|
+
base = (p2 = p2.toLowerCase()) == 'x' ? 16 : p2 == 'b' ? 2 : 8;
|
1366
|
+
return !b || b == base ? p1 : m;
|
1367
|
+
});
|
1368
|
+
|
1369
|
+
if (b) {
|
1370
|
+
base = b;
|
1371
|
+
|
1372
|
+
// E.g. '1.' to '1', '.1' to '0.1'
|
1373
|
+
s = s.replace(dotAfter, '$1').replace(dotBefore, '0.$1');
|
1374
|
+
}
|
1375
|
+
|
1376
|
+
if (str != s) return new BigNumber(s, base);
|
1377
|
+
}
|
1378
|
+
|
1379
|
+
// '[BigNumber Error] Not a number: {n}'
|
1380
|
+
// '[BigNumber Error] Not a base {b} number: {n}'
|
1381
|
+
if (BigNumber.DEBUG) {
|
1382
|
+
throw Error
|
1383
|
+
(bignumberError + 'Not a' + (b ? ' base ' + b : '') + ' number: ' + str);
|
1384
|
+
}
|
1385
|
+
|
1386
|
+
// NaN
|
1387
|
+
x.s = null;
|
1388
|
+
}
|
1389
|
+
|
1390
|
+
x.c = x.e = null;
|
1391
|
+
}
|
1392
|
+
})();
|
1393
|
+
|
1394
|
+
|
1395
|
+
/*
|
1396
|
+
* Round x to sd significant digits using rounding mode rm. Check for over/under-flow.
|
1397
|
+
* If r is truthy, it is known that there are more digits after the rounding digit.
|
1398
|
+
*/
|
1399
|
+
function round(x, sd, rm, r) {
|
1400
|
+
var d, i, j, k, n, ni, rd,
|
1401
|
+
xc = x.c,
|
1402
|
+
pows10 = POWS_TEN;
|
1403
|
+
|
1404
|
+
// if x is not Infinity or NaN...
|
1405
|
+
if (xc) {
|
1406
|
+
|
1407
|
+
// rd is the rounding digit, i.e. the digit after the digit that may be rounded up.
|
1408
|
+
// n is a base 1e14 number, the value of the element of array x.c containing rd.
|
1409
|
+
// ni is the index of n within x.c.
|
1410
|
+
// d is the number of digits of n.
|
1411
|
+
// i is the index of rd within n including leading zeros.
|
1412
|
+
// j is the actual index of rd within n (if < 0, rd is a leading zero).
|
1413
|
+
out: {
|
1414
|
+
|
1415
|
+
// Get the number of digits of the first element of xc.
|
1416
|
+
for (d = 1, k = xc[0]; k >= 10; k /= 10, d++);
|
1417
|
+
i = sd - d;
|
1418
|
+
|
1419
|
+
// If the rounding digit is in the first element of xc...
|
1420
|
+
if (i < 0) {
|
1421
|
+
i += LOG_BASE;
|
1422
|
+
j = sd;
|
1423
|
+
n = xc[ni = 0];
|
1424
|
+
|
1425
|
+
// Get the rounding digit at index j of n.
|
1426
|
+
rd = mathfloor(n / pows10[d - j - 1] % 10);
|
1427
|
+
} else {
|
1428
|
+
ni = mathceil((i + 1) / LOG_BASE);
|
1429
|
+
|
1430
|
+
if (ni >= xc.length) {
|
1431
|
+
|
1432
|
+
if (r) {
|
1433
|
+
|
1434
|
+
// Needed by sqrt.
|
1435
|
+
for (; xc.length <= ni; xc.push(0));
|
1436
|
+
n = rd = 0;
|
1437
|
+
d = 1;
|
1438
|
+
i %= LOG_BASE;
|
1439
|
+
j = i - LOG_BASE + 1;
|
1440
|
+
} else {
|
1441
|
+
break out;
|
1442
|
+
}
|
1443
|
+
} else {
|
1444
|
+
n = k = xc[ni];
|
1445
|
+
|
1446
|
+
// Get the number of digits of n.
|
1447
|
+
for (d = 1; k >= 10; k /= 10, d++);
|
1448
|
+
|
1449
|
+
// Get the index of rd within n.
|
1450
|
+
i %= LOG_BASE;
|
1451
|
+
|
1452
|
+
// Get the index of rd within n, adjusted for leading zeros.
|
1453
|
+
// The number of leading zeros of n is given by LOG_BASE - d.
|
1454
|
+
j = i - LOG_BASE + d;
|
1455
|
+
|
1456
|
+
// Get the rounding digit at index j of n.
|
1457
|
+
rd = j < 0 ? 0 : mathfloor(n / pows10[d - j - 1] % 10);
|
1458
|
+
}
|
1459
|
+
}
|
1460
|
+
|
1461
|
+
r = r || sd < 0 ||
|
1462
|
+
|
1463
|
+
// Are there any non-zero digits after the rounding digit?
|
1464
|
+
// The expression n % pows10[d - j - 1] returns all digits of n to the right
|
1465
|
+
// of the digit at j, e.g. if n is 908714 and j is 2, the expression gives 714.
|
1466
|
+
xc[ni + 1] != null || (j < 0 ? n : n % pows10[d - j - 1]);
|
1467
|
+
|
1468
|
+
r = rm < 4
|
1469
|
+
? (rd || r) && (rm == 0 || rm == (x.s < 0 ? 3 : 2))
|
1470
|
+
: rd > 5 || rd == 5 && (rm == 4 || r || rm == 6 &&
|
1471
|
+
|
1472
|
+
// Check whether the digit to the left of the rounding digit is odd.
|
1473
|
+
((i > 0 ? j > 0 ? n / pows10[d - j] : 0 : xc[ni - 1]) % 10) & 1 ||
|
1474
|
+
rm == (x.s < 0 ? 8 : 7));
|
1475
|
+
|
1476
|
+
if (sd < 1 || !xc[0]) {
|
1477
|
+
xc.length = 0;
|
1478
|
+
|
1479
|
+
if (r) {
|
1480
|
+
|
1481
|
+
// Convert sd to decimal places.
|
1482
|
+
sd -= x.e + 1;
|
1483
|
+
|
1484
|
+
// 1, 0.1, 0.01, 0.001, 0.0001 etc.
|
1485
|
+
xc[0] = pows10[(LOG_BASE - sd % LOG_BASE) % LOG_BASE];
|
1486
|
+
x.e = -sd || 0;
|
1487
|
+
} else {
|
1488
|
+
|
1489
|
+
// Zero.
|
1490
|
+
xc[0] = x.e = 0;
|
1491
|
+
}
|
1492
|
+
|
1493
|
+
return x;
|
1494
|
+
}
|
1495
|
+
|
1496
|
+
// Remove excess digits.
|
1497
|
+
if (i == 0) {
|
1498
|
+
xc.length = ni;
|
1499
|
+
k = 1;
|
1500
|
+
ni--;
|
1501
|
+
} else {
|
1502
|
+
xc.length = ni + 1;
|
1503
|
+
k = pows10[LOG_BASE - i];
|
1504
|
+
|
1505
|
+
// E.g. 56700 becomes 56000 if 7 is the rounding digit.
|
1506
|
+
// j > 0 means i > number of leading zeros of n.
|
1507
|
+
xc[ni] = j > 0 ? mathfloor(n / pows10[d - j] % pows10[j]) * k : 0;
|
1508
|
+
}
|
1509
|
+
|
1510
|
+
// Round up?
|
1511
|
+
if (r) {
|
1512
|
+
|
1513
|
+
for (; ;) {
|
1514
|
+
|
1515
|
+
// If the digit to be rounded up is in the first element of xc...
|
1516
|
+
if (ni == 0) {
|
1517
|
+
|
1518
|
+
// i will be the length of xc[0] before k is added.
|
1519
|
+
for (i = 1, j = xc[0]; j >= 10; j /= 10, i++);
|
1520
|
+
j = xc[0] += k;
|
1521
|
+
for (k = 1; j >= 10; j /= 10, k++);
|
1522
|
+
|
1523
|
+
// if i != k the length has increased.
|
1524
|
+
if (i != k) {
|
1525
|
+
x.e++;
|
1526
|
+
if (xc[0] == BASE) xc[0] = 1;
|
1527
|
+
}
|
1528
|
+
|
1529
|
+
break;
|
1530
|
+
} else {
|
1531
|
+
xc[ni] += k;
|
1532
|
+
if (xc[ni] != BASE) break;
|
1533
|
+
xc[ni--] = 0;
|
1534
|
+
k = 1;
|
1535
|
+
}
|
1536
|
+
}
|
1537
|
+
}
|
1538
|
+
|
1539
|
+
// Remove trailing zeros.
|
1540
|
+
for (i = xc.length; xc[--i] === 0; xc.pop());
|
1541
|
+
}
|
1542
|
+
|
1543
|
+
// Overflow? Infinity.
|
1544
|
+
if (x.e > MAX_EXP) {
|
1545
|
+
x.c = x.e = null;
|
1546
|
+
|
1547
|
+
// Underflow? Zero.
|
1548
|
+
} else if (x.e < MIN_EXP) {
|
1549
|
+
x.c = [x.e = 0];
|
1550
|
+
}
|
1551
|
+
}
|
1552
|
+
|
1553
|
+
return x;
|
1554
|
+
}
|
1555
|
+
|
1556
|
+
|
1557
|
+
function valueOf(n) {
|
1558
|
+
var str,
|
1559
|
+
e = n.e;
|
1560
|
+
|
1561
|
+
if (e === null) return n.toString();
|
1562
|
+
|
1563
|
+
str = coeffToString(n.c);
|
1564
|
+
|
1565
|
+
str = e <= TO_EXP_NEG || e >= TO_EXP_POS
|
1566
|
+
? toExponential(str, e)
|
1567
|
+
: toFixedPoint(str, e, '0');
|
1568
|
+
|
1569
|
+
return n.s < 0 ? '-' + str : str;
|
1570
|
+
}
|
1571
|
+
|
1572
|
+
|
1573
|
+
// PROTOTYPE/INSTANCE METHODS
|
1574
|
+
|
1575
|
+
|
1576
|
+
/*
|
1577
|
+
* Return a new BigNumber whose value is the absolute value of this BigNumber.
|
1578
|
+
*/
|
1579
|
+
P.absoluteValue = P.abs = function () {
|
1580
|
+
var x = new BigNumber(this);
|
1581
|
+
if (x.s < 0) x.s = 1;
|
1582
|
+
return x;
|
1583
|
+
};
|
1584
|
+
|
1585
|
+
|
1586
|
+
/*
|
1587
|
+
* Return
|
1588
|
+
* 1 if the value of this BigNumber is greater than the value of BigNumber(y, b),
|
1589
|
+
* -1 if the value of this BigNumber is less than the value of BigNumber(y, b),
|
1590
|
+
* 0 if they have the same value,
|
1591
|
+
* or null if the value of either is NaN.
|
1592
|
+
*/
|
1593
|
+
P.comparedTo = function (y, b) {
|
1594
|
+
return compare(this, new BigNumber(y, b));
|
1595
|
+
};
|
1596
|
+
|
1597
|
+
|
1598
|
+
/*
|
1599
|
+
* If dp is undefined or null or true or false, return the number of decimal places of the
|
1600
|
+
* value of this BigNumber, or null if the value of this BigNumber is ±Infinity or NaN.
|
1601
|
+
*
|
1602
|
+
* Otherwise, if dp is a number, return a new BigNumber whose value is the value of this
|
1603
|
+
* BigNumber rounded to a maximum of dp decimal places using rounding mode rm, or
|
1604
|
+
* ROUNDING_MODE if rm is omitted.
|
1605
|
+
*
|
1606
|
+
* [dp] {number} Decimal places: integer, 0 to MAX inclusive.
|
1607
|
+
* [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
|
1608
|
+
*
|
1609
|
+
* '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {dp|rm}'
|
1610
|
+
*/
|
1611
|
+
P.decimalPlaces = P.dp = function (dp, rm) {
|
1612
|
+
var c, n, v,
|
1613
|
+
x = this;
|
1614
|
+
|
1615
|
+
if (dp != null) {
|
1616
|
+
intCheck(dp, 0, MAX);
|
1617
|
+
if (rm == null) rm = ROUNDING_MODE;
|
1618
|
+
else intCheck(rm, 0, 8);
|
1619
|
+
|
1620
|
+
return round(new BigNumber(x), dp + x.e + 1, rm);
|
1621
|
+
}
|
1622
|
+
|
1623
|
+
if (!(c = x.c)) return null;
|
1624
|
+
n = ((v = c.length - 1) - bitFloor(this.e / LOG_BASE)) * LOG_BASE;
|
1625
|
+
|
1626
|
+
// Subtract the number of trailing zeros of the last number.
|
1627
|
+
if (v = c[v]) for (; v % 10 == 0; v /= 10, n--);
|
1628
|
+
if (n < 0) n = 0;
|
1629
|
+
|
1630
|
+
return n;
|
1631
|
+
};
|
1632
|
+
|
1633
|
+
|
1634
|
+
/*
|
1635
|
+
* n / 0 = I
|
1636
|
+
* n / N = N
|
1637
|
+
* n / I = 0
|
1638
|
+
* 0 / n = 0
|
1639
|
+
* 0 / 0 = N
|
1640
|
+
* 0 / N = N
|
1641
|
+
* 0 / I = 0
|
1642
|
+
* N / n = N
|
1643
|
+
* N / 0 = N
|
1644
|
+
* N / N = N
|
1645
|
+
* N / I = N
|
1646
|
+
* I / n = I
|
1647
|
+
* I / 0 = I
|
1648
|
+
* I / N = N
|
1649
|
+
* I / I = N
|
1650
|
+
*
|
1651
|
+
* Return a new BigNumber whose value is the value of this BigNumber divided by the value of
|
1652
|
+
* BigNumber(y, b), rounded according to DECIMAL_PLACES and ROUNDING_MODE.
|
1653
|
+
*/
|
1654
|
+
P.dividedBy = P.div = function (y, b) {
|
1655
|
+
return div(this, new BigNumber(y, b), DECIMAL_PLACES, ROUNDING_MODE);
|
1656
|
+
};
|
1657
|
+
|
1658
|
+
|
1659
|
+
/*
|
1660
|
+
* Return a new BigNumber whose value is the integer part of dividing the value of this
|
1661
|
+
* BigNumber by the value of BigNumber(y, b).
|
1662
|
+
*/
|
1663
|
+
P.dividedToIntegerBy = P.idiv = function (y, b) {
|
1664
|
+
return div(this, new BigNumber(y, b), 0, 1);
|
1665
|
+
};
|
1666
|
+
|
1667
|
+
|
1668
|
+
/*
|
1669
|
+
* Return a BigNumber whose value is the value of this BigNumber exponentiated by n.
|
1670
|
+
*
|
1671
|
+
* If m is present, return the result modulo m.
|
1672
|
+
* If n is negative round according to DECIMAL_PLACES and ROUNDING_MODE.
|
1673
|
+
* If POW_PRECISION is non-zero and m is not present, round to POW_PRECISION using ROUNDING_MODE.
|
1674
|
+
*
|
1675
|
+
* The modular power operation works efficiently when x, n, and m are integers, otherwise it
|
1676
|
+
* is equivalent to calculating x.exponentiatedBy(n).modulo(m) with a POW_PRECISION of 0.
|
1677
|
+
*
|
1678
|
+
* n {number|string|BigNumber} The exponent. An integer.
|
1679
|
+
* [m] {number|string|BigNumber} The modulus.
|
1680
|
+
*
|
1681
|
+
* '[BigNumber Error] Exponent not an integer: {n}'
|
1682
|
+
*/
|
1683
|
+
P.exponentiatedBy = P.pow = function (n, m) {
|
1684
|
+
var half, isModExp, i, k, more, nIsBig, nIsNeg, nIsOdd, y,
|
1685
|
+
x = this;
|
1686
|
+
|
1687
|
+
n = new BigNumber(n);
|
1688
|
+
|
1689
|
+
// Allow NaN and ±Infinity, but not other non-integers.
|
1690
|
+
if (n.c && !n.isInteger()) {
|
1691
|
+
throw Error
|
1692
|
+
(bignumberError + 'Exponent not an integer: ' + valueOf(n));
|
1693
|
+
}
|
1694
|
+
|
1695
|
+
if (m != null) m = new BigNumber(m);
|
1696
|
+
|
1697
|
+
// Exponent of MAX_SAFE_INTEGER is 15.
|
1698
|
+
nIsBig = n.e > 14;
|
1699
|
+
|
1700
|
+
// If x is NaN, ±Infinity, ±0 or ±1, or n is ±Infinity, NaN or ±0.
|
1701
|
+
if (!x.c || !x.c[0] || x.c[0] == 1 && !x.e && x.c.length == 1 || !n.c || !n.c[0]) {
|
1702
|
+
|
1703
|
+
// The sign of the result of pow when x is negative depends on the evenness of n.
|
1704
|
+
// If +n overflows to ±Infinity, the evenness of n would be not be known.
|
1705
|
+
y = new BigNumber(Math.pow(+valueOf(x), nIsBig ? n.s * (2 - isOdd(n)) : +valueOf(n)));
|
1706
|
+
return m ? y.mod(m) : y;
|
1707
|
+
}
|
1708
|
+
|
1709
|
+
nIsNeg = n.s < 0;
|
1710
|
+
|
1711
|
+
if (m) {
|
1712
|
+
|
1713
|
+
// x % m returns NaN if abs(m) is zero, or m is NaN.
|
1714
|
+
if (m.c ? !m.c[0] : !m.s) return new BigNumber(NaN);
|
1715
|
+
|
1716
|
+
isModExp = !nIsNeg && x.isInteger() && m.isInteger();
|
1717
|
+
|
1718
|
+
if (isModExp) x = x.mod(m);
|
1719
|
+
|
1720
|
+
// Overflow to ±Infinity: >=2**1e10 or >=1.0000024**1e15.
|
1721
|
+
// Underflow to ±0: <=0.79**1e10 or <=0.9999975**1e15.
|
1722
|
+
} else if (n.e > 9 && (x.e > 0 || x.e < -1 || (x.e == 0
|
1723
|
+
// [1, 240000000]
|
1724
|
+
? x.c[0] > 1 || nIsBig && x.c[1] >= 24e7
|
1725
|
+
// [80000000000000] [99999750000000]
|
1726
|
+
: x.c[0] < 8e13 || nIsBig && x.c[0] <= 9999975e7))) {
|
1727
|
+
|
1728
|
+
// If x is negative and n is odd, k = -0, else k = 0.
|
1729
|
+
k = x.s < 0 && isOdd(n) ? -0 : 0;
|
1730
|
+
|
1731
|
+
// If x >= 1, k = ±Infinity.
|
1732
|
+
if (x.e > -1) k = 1 / k;
|
1733
|
+
|
1734
|
+
// If n is negative return ±0, else return ±Infinity.
|
1735
|
+
return new BigNumber(nIsNeg ? 1 / k : k);
|
1736
|
+
|
1737
|
+
} else if (POW_PRECISION) {
|
1738
|
+
|
1739
|
+
// Truncating each coefficient array to a length of k after each multiplication
|
1740
|
+
// equates to truncating significant digits to POW_PRECISION + [28, 41],
|
1741
|
+
// i.e. there will be a minimum of 28 guard digits retained.
|
1742
|
+
k = mathceil(POW_PRECISION / LOG_BASE + 2);
|
1743
|
+
}
|
1744
|
+
|
1745
|
+
if (nIsBig) {
|
1746
|
+
half = new BigNumber(0.5);
|
1747
|
+
if (nIsNeg) n.s = 1;
|
1748
|
+
nIsOdd = isOdd(n);
|
1749
|
+
} else {
|
1750
|
+
i = Math.abs(+valueOf(n));
|
1751
|
+
nIsOdd = i % 2;
|
1752
|
+
}
|
1753
|
+
|
1754
|
+
y = new BigNumber(ONE);
|
1755
|
+
|
1756
|
+
// Performs 54 loop iterations for n of 9007199254740991.
|
1757
|
+
for (; ;) {
|
1758
|
+
|
1759
|
+
if (nIsOdd) {
|
1760
|
+
y = y.times(x);
|
1761
|
+
if (!y.c) break;
|
1762
|
+
|
1763
|
+
if (k) {
|
1764
|
+
if (y.c.length > k) y.c.length = k;
|
1765
|
+
} else if (isModExp) {
|
1766
|
+
y = y.mod(m); //y = y.minus(div(y, m, 0, MODULO_MODE).times(m));
|
1767
|
+
}
|
1768
|
+
}
|
1769
|
+
|
1770
|
+
if (i) {
|
1771
|
+
i = mathfloor(i / 2);
|
1772
|
+
if (i === 0) break;
|
1773
|
+
nIsOdd = i % 2;
|
1774
|
+
} else {
|
1775
|
+
n = n.times(half);
|
1776
|
+
round(n, n.e + 1, 1);
|
1777
|
+
|
1778
|
+
if (n.e > 14) {
|
1779
|
+
nIsOdd = isOdd(n);
|
1780
|
+
} else {
|
1781
|
+
i = +valueOf(n);
|
1782
|
+
if (i === 0) break;
|
1783
|
+
nIsOdd = i % 2;
|
1784
|
+
}
|
1785
|
+
}
|
1786
|
+
|
1787
|
+
x = x.times(x);
|
1788
|
+
|
1789
|
+
if (k) {
|
1790
|
+
if (x.c && x.c.length > k) x.c.length = k;
|
1791
|
+
} else if (isModExp) {
|
1792
|
+
x = x.mod(m); //x = x.minus(div(x, m, 0, MODULO_MODE).times(m));
|
1793
|
+
}
|
1794
|
+
}
|
1795
|
+
|
1796
|
+
if (isModExp) return y;
|
1797
|
+
if (nIsNeg) y = ONE.div(y);
|
1798
|
+
|
1799
|
+
return m ? y.mod(m) : k ? round(y, POW_PRECISION, ROUNDING_MODE, more) : y;
|
1800
|
+
};
|
1801
|
+
|
1802
|
+
|
1803
|
+
/*
|
1804
|
+
* Return a new BigNumber whose value is the value of this BigNumber rounded to an integer
|
1805
|
+
* using rounding mode rm, or ROUNDING_MODE if rm is omitted.
|
1806
|
+
*
|
1807
|
+
* [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
|
1808
|
+
*
|
1809
|
+
* '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {rm}'
|
1810
|
+
*/
|
1811
|
+
P.integerValue = function (rm) {
|
1812
|
+
var n = new BigNumber(this);
|
1813
|
+
if (rm == null) rm = ROUNDING_MODE;
|
1814
|
+
else intCheck(rm, 0, 8);
|
1815
|
+
return round(n, n.e + 1, rm);
|
1816
|
+
};
|
1817
|
+
|
1818
|
+
|
1819
|
+
/*
|
1820
|
+
* Return true if the value of this BigNumber is equal to the value of BigNumber(y, b),
|
1821
|
+
* otherwise return false.
|
1822
|
+
*/
|
1823
|
+
P.isEqualTo = P.eq = function (y, b) {
|
1824
|
+
return compare(this, new BigNumber(y, b)) === 0;
|
1825
|
+
};
|
1826
|
+
|
1827
|
+
|
1828
|
+
/*
|
1829
|
+
* Return true if the value of this BigNumber is a finite number, otherwise return false.
|
1830
|
+
*/
|
1831
|
+
P.isFinite = function () {
|
1832
|
+
return !!this.c;
|
1833
|
+
};
|
1834
|
+
|
1835
|
+
|
1836
|
+
/*
|
1837
|
+
* Return true if the value of this BigNumber is greater than the value of BigNumber(y, b),
|
1838
|
+
* otherwise return false.
|
1839
|
+
*/
|
1840
|
+
P.isGreaterThan = P.gt = function (y, b) {
|
1841
|
+
return compare(this, new BigNumber(y, b)) > 0;
|
1842
|
+
};
|
1843
|
+
|
1844
|
+
|
1845
|
+
/*
|
1846
|
+
* Return true if the value of this BigNumber is greater than or equal to the value of
|
1847
|
+
* BigNumber(y, b), otherwise return false.
|
1848
|
+
*/
|
1849
|
+
P.isGreaterThanOrEqualTo = P.gte = function (y, b) {
|
1850
|
+
return (b = compare(this, new BigNumber(y, b))) === 1 || b === 0;
|
1851
|
+
|
1852
|
+
};
|
1853
|
+
|
1854
|
+
|
1855
|
+
/*
|
1856
|
+
* Return true if the value of this BigNumber is an integer, otherwise return false.
|
1857
|
+
*/
|
1858
|
+
P.isInteger = function () {
|
1859
|
+
return !!this.c && bitFloor(this.e / LOG_BASE) > this.c.length - 2;
|
1860
|
+
};
|
1861
|
+
|
1862
|
+
|
1863
|
+
/*
|
1864
|
+
* Return true if the value of this BigNumber is less than the value of BigNumber(y, b),
|
1865
|
+
* otherwise return false.
|
1866
|
+
*/
|
1867
|
+
P.isLessThan = P.lt = function (y, b) {
|
1868
|
+
return compare(this, new BigNumber(y, b)) < 0;
|
1869
|
+
};
|
1870
|
+
|
1871
|
+
|
1872
|
+
/*
|
1873
|
+
* Return true if the value of this BigNumber is less than or equal to the value of
|
1874
|
+
* BigNumber(y, b), otherwise return false.
|
1875
|
+
*/
|
1876
|
+
P.isLessThanOrEqualTo = P.lte = function (y, b) {
|
1877
|
+
return (b = compare(this, new BigNumber(y, b))) === -1 || b === 0;
|
1878
|
+
};
|
1879
|
+
|
1880
|
+
|
1881
|
+
/*
|
1882
|
+
* Return true if the value of this BigNumber is NaN, otherwise return false.
|
1883
|
+
*/
|
1884
|
+
P.isNaN = function () {
|
1885
|
+
return !this.s;
|
1886
|
+
};
|
1887
|
+
|
1888
|
+
|
1889
|
+
/*
|
1890
|
+
* Return true if the value of this BigNumber is negative, otherwise return false.
|
1891
|
+
*/
|
1892
|
+
P.isNegative = function () {
|
1893
|
+
return this.s < 0;
|
1894
|
+
};
|
1895
|
+
|
1896
|
+
|
1897
|
+
/*
|
1898
|
+
* Return true if the value of this BigNumber is positive, otherwise return false.
|
1899
|
+
*/
|
1900
|
+
P.isPositive = function () {
|
1901
|
+
return this.s > 0;
|
1902
|
+
};
|
1903
|
+
|
1904
|
+
|
1905
|
+
/*
|
1906
|
+
* Return true if the value of this BigNumber is 0 or -0, otherwise return false.
|
1907
|
+
*/
|
1908
|
+
P.isZero = function () {
|
1909
|
+
return !!this.c && this.c[0] == 0;
|
1910
|
+
};
|
1911
|
+
|
1912
|
+
|
1913
|
+
/*
|
1914
|
+
* n - 0 = n
|
1915
|
+
* n - N = N
|
1916
|
+
* n - I = -I
|
1917
|
+
* 0 - n = -n
|
1918
|
+
* 0 - 0 = 0
|
1919
|
+
* 0 - N = N
|
1920
|
+
* 0 - I = -I
|
1921
|
+
* N - n = N
|
1922
|
+
* N - 0 = N
|
1923
|
+
* N - N = N
|
1924
|
+
* N - I = N
|
1925
|
+
* I - n = I
|
1926
|
+
* I - 0 = I
|
1927
|
+
* I - N = N
|
1928
|
+
* I - I = N
|
1929
|
+
*
|
1930
|
+
* Return a new BigNumber whose value is the value of this BigNumber minus the value of
|
1931
|
+
* BigNumber(y, b).
|
1932
|
+
*/
|
1933
|
+
P.minus = function (y, b) {
|
1934
|
+
var i, j, t, xLTy,
|
1935
|
+
x = this,
|
1936
|
+
a = x.s;
|
1937
|
+
|
1938
|
+
y = new BigNumber(y, b);
|
1939
|
+
b = y.s;
|
1940
|
+
|
1941
|
+
// Either NaN?
|
1942
|
+
if (!a || !b) return new BigNumber(NaN);
|
1943
|
+
|
1944
|
+
// Signs differ?
|
1945
|
+
if (a != b) {
|
1946
|
+
y.s = -b;
|
1947
|
+
return x.plus(y);
|
1948
|
+
}
|
1949
|
+
|
1950
|
+
var xe = x.e / LOG_BASE,
|
1951
|
+
ye = y.e / LOG_BASE,
|
1952
|
+
xc = x.c,
|
1953
|
+
yc = y.c;
|
1954
|
+
|
1955
|
+
if (!xe || !ye) {
|
1956
|
+
|
1957
|
+
// Either Infinity?
|
1958
|
+
if (!xc || !yc) return xc ? (y.s = -b, y) : new BigNumber(yc ? x : NaN);
|
1959
|
+
|
1960
|
+
// Either zero?
|
1961
|
+
if (!xc[0] || !yc[0]) {
|
1962
|
+
|
1963
|
+
// Return y if y is non-zero, x if x is non-zero, or zero if both are zero.
|
1964
|
+
return yc[0] ? (y.s = -b, y) : new BigNumber(xc[0] ? x :
|
1965
|
+
|
1966
|
+
// IEEE 754 (2008) 6.3: n - n = -0 when rounding to -Infinity
|
1967
|
+
ROUNDING_MODE == 3 ? -0 : 0);
|
1968
|
+
}
|
1969
|
+
}
|
1970
|
+
|
1971
|
+
xe = bitFloor(xe);
|
1972
|
+
ye = bitFloor(ye);
|
1973
|
+
xc = xc.slice();
|
1974
|
+
|
1975
|
+
// Determine which is the bigger number.
|
1976
|
+
if (a = xe - ye) {
|
1977
|
+
|
1978
|
+
if (xLTy = a < 0) {
|
1979
|
+
a = -a;
|
1980
|
+
t = xc;
|
1981
|
+
} else {
|
1982
|
+
ye = xe;
|
1983
|
+
t = yc;
|
1984
|
+
}
|
1985
|
+
|
1986
|
+
t.reverse();
|
1987
|
+
|
1988
|
+
// Prepend zeros to equalise exponents.
|
1989
|
+
for (b = a; b--; t.push(0));
|
1990
|
+
t.reverse();
|
1991
|
+
} else {
|
1992
|
+
|
1993
|
+
// Exponents equal. Check digit by digit.
|
1994
|
+
j = (xLTy = (a = xc.length) < (b = yc.length)) ? a : b;
|
1995
|
+
|
1996
|
+
for (a = b = 0; b < j; b++) {
|
1997
|
+
|
1998
|
+
if (xc[b] != yc[b]) {
|
1999
|
+
xLTy = xc[b] < yc[b];
|
2000
|
+
break;
|
2001
|
+
}
|
2002
|
+
}
|
2003
|
+
}
|
2004
|
+
|
2005
|
+
// x < y? Point xc to the array of the bigger number.
|
2006
|
+
if (xLTy) {
|
2007
|
+
t = xc;
|
2008
|
+
xc = yc;
|
2009
|
+
yc = t;
|
2010
|
+
y.s = -y.s;
|
2011
|
+
}
|
2012
|
+
|
2013
|
+
b = (j = yc.length) - (i = xc.length);
|
2014
|
+
|
2015
|
+
// Append zeros to xc if shorter.
|
2016
|
+
// No need to add zeros to yc if shorter as subtract only needs to start at yc.length.
|
2017
|
+
if (b > 0) for (; b--; xc[i++] = 0);
|
2018
|
+
b = BASE - 1;
|
2019
|
+
|
2020
|
+
// Subtract yc from xc.
|
2021
|
+
for (; j > a;) {
|
2022
|
+
|
2023
|
+
if (xc[--j] < yc[j]) {
|
2024
|
+
for (i = j; i && !xc[--i]; xc[i] = b);
|
2025
|
+
--xc[i];
|
2026
|
+
xc[j] += BASE;
|
2027
|
+
}
|
2028
|
+
|
2029
|
+
xc[j] -= yc[j];
|
2030
|
+
}
|
2031
|
+
|
2032
|
+
// Remove leading zeros and adjust exponent accordingly.
|
2033
|
+
for (; xc[0] == 0; xc.splice(0, 1), --ye);
|
2034
|
+
|
2035
|
+
// Zero?
|
2036
|
+
if (!xc[0]) {
|
2037
|
+
|
2038
|
+
// Following IEEE 754 (2008) 6.3,
|
2039
|
+
// n - n = +0 but n - n = -0 when rounding towards -Infinity.
|
2040
|
+
y.s = ROUNDING_MODE == 3 ? -1 : 1;
|
2041
|
+
y.c = [y.e = 0];
|
2042
|
+
return y;
|
2043
|
+
}
|
2044
|
+
|
2045
|
+
// No need to check for Infinity as +x - +y != Infinity && -x - -y != Infinity
|
2046
|
+
// for finite x and y.
|
2047
|
+
return normalise(y, xc, ye);
|
2048
|
+
};
|
2049
|
+
|
2050
|
+
|
2051
|
+
/*
|
2052
|
+
* n % 0 = N
|
2053
|
+
* n % N = N
|
2054
|
+
* n % I = n
|
2055
|
+
* 0 % n = 0
|
2056
|
+
* -0 % n = -0
|
2057
|
+
* 0 % 0 = N
|
2058
|
+
* 0 % N = N
|
2059
|
+
* 0 % I = 0
|
2060
|
+
* N % n = N
|
2061
|
+
* N % 0 = N
|
2062
|
+
* N % N = N
|
2063
|
+
* N % I = N
|
2064
|
+
* I % n = N
|
2065
|
+
* I % 0 = N
|
2066
|
+
* I % N = N
|
2067
|
+
* I % I = N
|
2068
|
+
*
|
2069
|
+
* Return a new BigNumber whose value is the value of this BigNumber modulo the value of
|
2070
|
+
* BigNumber(y, b). The result depends on the value of MODULO_MODE.
|
2071
|
+
*/
|
2072
|
+
P.modulo = P.mod = function (y, b) {
|
2073
|
+
var q, s,
|
2074
|
+
x = this;
|
2075
|
+
|
2076
|
+
y = new BigNumber(y, b);
|
2077
|
+
|
2078
|
+
// Return NaN if x is Infinity or NaN, or y is NaN or zero.
|
2079
|
+
if (!x.c || !y.s || y.c && !y.c[0]) {
|
2080
|
+
return new BigNumber(NaN);
|
2081
|
+
|
2082
|
+
// Return x if y is Infinity or x is zero.
|
2083
|
+
} else if (!y.c || x.c && !x.c[0]) {
|
2084
|
+
return new BigNumber(x);
|
2085
|
+
}
|
2086
|
+
|
2087
|
+
if (MODULO_MODE == 9) {
|
2088
|
+
|
2089
|
+
// Euclidian division: q = sign(y) * floor(x / abs(y))
|
2090
|
+
// r = x - qy where 0 <= r < abs(y)
|
2091
|
+
s = y.s;
|
2092
|
+
y.s = 1;
|
2093
|
+
q = div(x, y, 0, 3);
|
2094
|
+
y.s = s;
|
2095
|
+
q.s *= s;
|
2096
|
+
} else {
|
2097
|
+
q = div(x, y, 0, MODULO_MODE);
|
2098
|
+
}
|
2099
|
+
|
2100
|
+
y = x.minus(q.times(y));
|
2101
|
+
|
2102
|
+
// To match JavaScript %, ensure sign of zero is sign of dividend.
|
2103
|
+
if (!y.c[0] && MODULO_MODE == 1) y.s = x.s;
|
2104
|
+
|
2105
|
+
return y;
|
2106
|
+
};
|
2107
|
+
|
2108
|
+
|
2109
|
+
/*
|
2110
|
+
* n * 0 = 0
|
2111
|
+
* n * N = N
|
2112
|
+
* n * I = I
|
2113
|
+
* 0 * n = 0
|
2114
|
+
* 0 * 0 = 0
|
2115
|
+
* 0 * N = N
|
2116
|
+
* 0 * I = N
|
2117
|
+
* N * n = N
|
2118
|
+
* N * 0 = N
|
2119
|
+
* N * N = N
|
2120
|
+
* N * I = N
|
2121
|
+
* I * n = I
|
2122
|
+
* I * 0 = N
|
2123
|
+
* I * N = N
|
2124
|
+
* I * I = I
|
2125
|
+
*
|
2126
|
+
* Return a new BigNumber whose value is the value of this BigNumber multiplied by the value
|
2127
|
+
* of BigNumber(y, b).
|
2128
|
+
*/
|
2129
|
+
P.multipliedBy = P.times = function (y, b) {
|
2130
|
+
var c, e, i, j, k, m, xcL, xlo, xhi, ycL, ylo, yhi, zc,
|
2131
|
+
base, sqrtBase,
|
2132
|
+
x = this,
|
2133
|
+
xc = x.c,
|
2134
|
+
yc = (y = new BigNumber(y, b)).c;
|
2135
|
+
|
2136
|
+
// Either NaN, ±Infinity or ±0?
|
2137
|
+
if (!xc || !yc || !xc[0] || !yc[0]) {
|
2138
|
+
|
2139
|
+
// Return NaN if either is NaN, or one is 0 and the other is Infinity.
|
2140
|
+
if (!x.s || !y.s || xc && !xc[0] && !yc || yc && !yc[0] && !xc) {
|
2141
|
+
y.c = y.e = y.s = null;
|
2142
|
+
} else {
|
2143
|
+
y.s *= x.s;
|
2144
|
+
|
2145
|
+
// Return ±Infinity if either is ±Infinity.
|
2146
|
+
if (!xc || !yc) {
|
2147
|
+
y.c = y.e = null;
|
2148
|
+
|
2149
|
+
// Return ±0 if either is ±0.
|
2150
|
+
} else {
|
2151
|
+
y.c = [0];
|
2152
|
+
y.e = 0;
|
2153
|
+
}
|
2154
|
+
}
|
2155
|
+
|
2156
|
+
return y;
|
2157
|
+
}
|
2158
|
+
|
2159
|
+
e = bitFloor(x.e / LOG_BASE) + bitFloor(y.e / LOG_BASE);
|
2160
|
+
y.s *= x.s;
|
2161
|
+
xcL = xc.length;
|
2162
|
+
ycL = yc.length;
|
2163
|
+
|
2164
|
+
// Ensure xc points to longer array and xcL to its length.
|
2165
|
+
if (xcL < ycL) {
|
2166
|
+
zc = xc;
|
2167
|
+
xc = yc;
|
2168
|
+
yc = zc;
|
2169
|
+
i = xcL;
|
2170
|
+
xcL = ycL;
|
2171
|
+
ycL = i;
|
2172
|
+
}
|
2173
|
+
|
2174
|
+
// Initialise the result array with zeros.
|
2175
|
+
for (i = xcL + ycL, zc = []; i--; zc.push(0));
|
2176
|
+
|
2177
|
+
base = BASE;
|
2178
|
+
sqrtBase = SQRT_BASE;
|
2179
|
+
|
2180
|
+
for (i = ycL; --i >= 0;) {
|
2181
|
+
c = 0;
|
2182
|
+
ylo = yc[i] % sqrtBase;
|
2183
|
+
yhi = yc[i] / sqrtBase | 0;
|
2184
|
+
|
2185
|
+
for (k = xcL, j = i + k; j > i;) {
|
2186
|
+
xlo = xc[--k] % sqrtBase;
|
2187
|
+
xhi = xc[k] / sqrtBase | 0;
|
2188
|
+
m = yhi * xlo + xhi * ylo;
|
2189
|
+
xlo = ylo * xlo + ((m % sqrtBase) * sqrtBase) + zc[j] + c;
|
2190
|
+
c = (xlo / base | 0) + (m / sqrtBase | 0) + yhi * xhi;
|
2191
|
+
zc[j--] = xlo % base;
|
2192
|
+
}
|
2193
|
+
|
2194
|
+
zc[j] = c;
|
2195
|
+
}
|
2196
|
+
|
2197
|
+
if (c) {
|
2198
|
+
++e;
|
2199
|
+
} else {
|
2200
|
+
zc.splice(0, 1);
|
2201
|
+
}
|
2202
|
+
|
2203
|
+
return normalise(y, zc, e);
|
2204
|
+
};
|
2205
|
+
|
2206
|
+
|
2207
|
+
/*
|
2208
|
+
* Return a new BigNumber whose value is the value of this BigNumber negated,
|
2209
|
+
* i.e. multiplied by -1.
|
2210
|
+
*/
|
2211
|
+
P.negated = function () {
|
2212
|
+
var x = new BigNumber(this);
|
2213
|
+
x.s = -x.s || null;
|
2214
|
+
return x;
|
2215
|
+
};
|
2216
|
+
|
2217
|
+
|
2218
|
+
/*
|
2219
|
+
* n + 0 = n
|
2220
|
+
* n + N = N
|
2221
|
+
* n + I = I
|
2222
|
+
* 0 + n = n
|
2223
|
+
* 0 + 0 = 0
|
2224
|
+
* 0 + N = N
|
2225
|
+
* 0 + I = I
|
2226
|
+
* N + n = N
|
2227
|
+
* N + 0 = N
|
2228
|
+
* N + N = N
|
2229
|
+
* N + I = N
|
2230
|
+
* I + n = I
|
2231
|
+
* I + 0 = I
|
2232
|
+
* I + N = N
|
2233
|
+
* I + I = I
|
2234
|
+
*
|
2235
|
+
* Return a new BigNumber whose value is the value of this BigNumber plus the value of
|
2236
|
+
* BigNumber(y, b).
|
2237
|
+
*/
|
2238
|
+
P.plus = function (y, b) {
|
2239
|
+
var t,
|
2240
|
+
x = this,
|
2241
|
+
a = x.s;
|
2242
|
+
|
2243
|
+
y = new BigNumber(y, b);
|
2244
|
+
b = y.s;
|
2245
|
+
|
2246
|
+
// Either NaN?
|
2247
|
+
if (!a || !b) return new BigNumber(NaN);
|
2248
|
+
|
2249
|
+
// Signs differ?
|
2250
|
+
if (a != b) {
|
2251
|
+
y.s = -b;
|
2252
|
+
return x.minus(y);
|
2253
|
+
}
|
2254
|
+
|
2255
|
+
var xe = x.e / LOG_BASE,
|
2256
|
+
ye = y.e / LOG_BASE,
|
2257
|
+
xc = x.c,
|
2258
|
+
yc = y.c;
|
2259
|
+
|
2260
|
+
if (!xe || !ye) {
|
2261
|
+
|
2262
|
+
// Return ±Infinity if either ±Infinity.
|
2263
|
+
if (!xc || !yc) return new BigNumber(a / 0);
|
2264
|
+
|
2265
|
+
// Either zero?
|
2266
|
+
// Return y if y is non-zero, x if x is non-zero, or zero if both are zero.
|
2267
|
+
if (!xc[0] || !yc[0]) return yc[0] ? y : new BigNumber(xc[0] ? x : a * 0);
|
2268
|
+
}
|
2269
|
+
|
2270
|
+
xe = bitFloor(xe);
|
2271
|
+
ye = bitFloor(ye);
|
2272
|
+
xc = xc.slice();
|
2273
|
+
|
2274
|
+
// Prepend zeros to equalise exponents. Faster to use reverse then do unshifts.
|
2275
|
+
if (a = xe - ye) {
|
2276
|
+
if (a > 0) {
|
2277
|
+
ye = xe;
|
2278
|
+
t = yc;
|
2279
|
+
} else {
|
2280
|
+
a = -a;
|
2281
|
+
t = xc;
|
2282
|
+
}
|
2283
|
+
|
2284
|
+
t.reverse();
|
2285
|
+
for (; a--; t.push(0));
|
2286
|
+
t.reverse();
|
2287
|
+
}
|
2288
|
+
|
2289
|
+
a = xc.length;
|
2290
|
+
b = yc.length;
|
2291
|
+
|
2292
|
+
// Point xc to the longer array, and b to the shorter length.
|
2293
|
+
if (a - b < 0) {
|
2294
|
+
t = yc;
|
2295
|
+
yc = xc;
|
2296
|
+
xc = t;
|
2297
|
+
b = a;
|
2298
|
+
}
|
2299
|
+
|
2300
|
+
// Only start adding at yc.length - 1 as the further digits of xc can be ignored.
|
2301
|
+
for (a = 0; b;) {
|
2302
|
+
a = (xc[--b] = xc[b] + yc[b] + a) / BASE | 0;
|
2303
|
+
xc[b] = BASE === xc[b] ? 0 : xc[b] % BASE;
|
2304
|
+
}
|
2305
|
+
|
2306
|
+
if (a) {
|
2307
|
+
xc = [a].concat(xc);
|
2308
|
+
++ye;
|
2309
|
+
}
|
2310
|
+
|
2311
|
+
// No need to check for zero, as +x + +y != 0 && -x + -y != 0
|
2312
|
+
// ye = MAX_EXP + 1 possible
|
2313
|
+
return normalise(y, xc, ye);
|
2314
|
+
};
|
2315
|
+
|
2316
|
+
|
2317
|
+
/*
|
2318
|
+
* If sd is undefined or null or true or false, return the number of significant digits of
|
2319
|
+
* the value of this BigNumber, or null if the value of this BigNumber is ±Infinity or NaN.
|
2320
|
+
* If sd is true include integer-part trailing zeros in the count.
|
2321
|
+
*
|
2322
|
+
* Otherwise, if sd is a number, return a new BigNumber whose value is the value of this
|
2323
|
+
* BigNumber rounded to a maximum of sd significant digits using rounding mode rm, or
|
2324
|
+
* ROUNDING_MODE if rm is omitted.
|
2325
|
+
*
|
2326
|
+
* sd {number|boolean} number: significant digits: integer, 1 to MAX inclusive.
|
2327
|
+
* boolean: whether to count integer-part trailing zeros: true or false.
|
2328
|
+
* [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
|
2329
|
+
*
|
2330
|
+
* '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {sd|rm}'
|
2331
|
+
*/
|
2332
|
+
P.precision = P.sd = function (sd, rm) {
|
2333
|
+
var c, n, v,
|
2334
|
+
x = this;
|
2335
|
+
|
2336
|
+
if (sd != null && sd !== !!sd) {
|
2337
|
+
intCheck(sd, 1, MAX);
|
2338
|
+
if (rm == null) rm = ROUNDING_MODE;
|
2339
|
+
else intCheck(rm, 0, 8);
|
2340
|
+
|
2341
|
+
return round(new BigNumber(x), sd, rm);
|
2342
|
+
}
|
2343
|
+
|
2344
|
+
if (!(c = x.c)) return null;
|
2345
|
+
v = c.length - 1;
|
2346
|
+
n = v * LOG_BASE + 1;
|
2347
|
+
|
2348
|
+
if (v = c[v]) {
|
2349
|
+
|
2350
|
+
// Subtract the number of trailing zeros of the last element.
|
2351
|
+
for (; v % 10 == 0; v /= 10, n--);
|
2352
|
+
|
2353
|
+
// Add the number of digits of the first element.
|
2354
|
+
for (v = c[0]; v >= 10; v /= 10, n++);
|
2355
|
+
}
|
2356
|
+
|
2357
|
+
if (sd && x.e + 1 > n) n = x.e + 1;
|
2358
|
+
|
2359
|
+
return n;
|
2360
|
+
};
|
2361
|
+
|
2362
|
+
|
2363
|
+
/*
|
2364
|
+
* Return a new BigNumber whose value is the value of this BigNumber shifted by k places
|
2365
|
+
* (powers of 10). Shift to the right if n > 0, and to the left if n < 0.
|
2366
|
+
*
|
2367
|
+
* k {number} Integer, -MAX_SAFE_INTEGER to MAX_SAFE_INTEGER inclusive.
|
2368
|
+
*
|
2369
|
+
* '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {k}'
|
2370
|
+
*/
|
2371
|
+
P.shiftedBy = function (k) {
|
2372
|
+
intCheck(k, -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER);
|
2373
|
+
return this.times('1e' + k);
|
2374
|
+
};
|
2375
|
+
|
2376
|
+
|
2377
|
+
/*
|
2378
|
+
* sqrt(-n) = N
|
2379
|
+
* sqrt(N) = N
|
2380
|
+
* sqrt(-I) = N
|
2381
|
+
* sqrt(I) = I
|
2382
|
+
* sqrt(0) = 0
|
2383
|
+
* sqrt(-0) = -0
|
2384
|
+
*
|
2385
|
+
* Return a new BigNumber whose value is the square root of the value of this BigNumber,
|
2386
|
+
* rounded according to DECIMAL_PLACES and ROUNDING_MODE.
|
2387
|
+
*/
|
2388
|
+
P.squareRoot = P.sqrt = function () {
|
2389
|
+
var m, n, r, rep, t,
|
2390
|
+
x = this,
|
2391
|
+
c = x.c,
|
2392
|
+
s = x.s,
|
2393
|
+
e = x.e,
|
2394
|
+
dp = DECIMAL_PLACES + 4,
|
2395
|
+
half = new BigNumber('0.5');
|
2396
|
+
|
2397
|
+
// Negative/NaN/Infinity/zero?
|
2398
|
+
if (s !== 1 || !c || !c[0]) {
|
2399
|
+
return new BigNumber(!s || s < 0 && (!c || c[0]) ? NaN : c ? x : 1 / 0);
|
2400
|
+
}
|
2401
|
+
|
2402
|
+
// Initial estimate.
|
2403
|
+
s = Math.sqrt(+valueOf(x));
|
2404
|
+
|
2405
|
+
// Math.sqrt underflow/overflow?
|
2406
|
+
// Pass x to Math.sqrt as integer, then adjust the exponent of the result.
|
2407
|
+
if (s == 0 || s == 1 / 0) {
|
2408
|
+
n = coeffToString(c);
|
2409
|
+
if ((n.length + e) % 2 == 0) n += '0';
|
2410
|
+
s = Math.sqrt(+n);
|
2411
|
+
e = bitFloor((e + 1) / 2) - (e < 0 || e % 2);
|
2412
|
+
|
2413
|
+
if (s == 1 / 0) {
|
2414
|
+
n = '5e' + e;
|
2415
|
+
} else {
|
2416
|
+
n = s.toExponential();
|
2417
|
+
n = n.slice(0, n.indexOf('e') + 1) + e;
|
2418
|
+
}
|
2419
|
+
|
2420
|
+
r = new BigNumber(n);
|
2421
|
+
} else {
|
2422
|
+
r = new BigNumber(s + '');
|
2423
|
+
}
|
2424
|
+
|
2425
|
+
// Check for zero.
|
2426
|
+
// r could be zero if MIN_EXP is changed after the this value was created.
|
2427
|
+
// This would cause a division by zero (x/t) and hence Infinity below, which would cause
|
2428
|
+
// coeffToString to throw.
|
2429
|
+
if (r.c[0]) {
|
2430
|
+
e = r.e;
|
2431
|
+
s = e + dp;
|
2432
|
+
if (s < 3) s = 0;
|
2433
|
+
|
2434
|
+
// Newton-Raphson iteration.
|
2435
|
+
for (; ;) {
|
2436
|
+
t = r;
|
2437
|
+
r = half.times(t.plus(div(x, t, dp, 1)));
|
2438
|
+
|
2439
|
+
if (coeffToString(t.c).slice(0, s) === (n = coeffToString(r.c)).slice(0, s)) {
|
2440
|
+
|
2441
|
+
// The exponent of r may here be one less than the final result exponent,
|
2442
|
+
// e.g 0.0009999 (e-4) --> 0.001 (e-3), so adjust s so the rounding digits
|
2443
|
+
// are indexed correctly.
|
2444
|
+
if (r.e < e) --s;
|
2445
|
+
n = n.slice(s - 3, s + 1);
|
2446
|
+
|
2447
|
+
// The 4th rounding digit may be in error by -1 so if the 4 rounding digits
|
2448
|
+
// are 9999 or 4999 (i.e. approaching a rounding boundary) continue the
|
2449
|
+
// iteration.
|
2450
|
+
if (n == '9999' || !rep && n == '4999') {
|
2451
|
+
|
2452
|
+
// On the first iteration only, check to see if rounding up gives the
|
2453
|
+
// exact result as the nines may infinitely repeat.
|
2454
|
+
if (!rep) {
|
2455
|
+
round(t, t.e + DECIMAL_PLACES + 2, 0);
|
2456
|
+
|
2457
|
+
if (t.times(t).eq(x)) {
|
2458
|
+
r = t;
|
2459
|
+
break;
|
2460
|
+
}
|
2461
|
+
}
|
2462
|
+
|
2463
|
+
dp += 4;
|
2464
|
+
s += 4;
|
2465
|
+
rep = 1;
|
2466
|
+
} else {
|
2467
|
+
|
2468
|
+
// If rounding digits are null, 0{0,4} or 50{0,3}, check for exact
|
2469
|
+
// result. If not, then there are further digits and m will be truthy.
|
2470
|
+
if (!+n || !+n.slice(1) && n.charAt(0) == '5') {
|
2471
|
+
|
2472
|
+
// Truncate to the first rounding digit.
|
2473
|
+
round(r, r.e + DECIMAL_PLACES + 2, 1);
|
2474
|
+
m = !r.times(r).eq(x);
|
2475
|
+
}
|
2476
|
+
|
2477
|
+
break;
|
2478
|
+
}
|
2479
|
+
}
|
2480
|
+
}
|
2481
|
+
}
|
2482
|
+
|
2483
|
+
return round(r, r.e + DECIMAL_PLACES + 1, ROUNDING_MODE, m);
|
2484
|
+
};
|
2485
|
+
|
2486
|
+
|
2487
|
+
/*
|
2488
|
+
* Return a string representing the value of this BigNumber in exponential notation and
|
2489
|
+
* rounded using ROUNDING_MODE to dp fixed decimal places.
|
2490
|
+
*
|
2491
|
+
* [dp] {number} Decimal places. Integer, 0 to MAX inclusive.
|
2492
|
+
* [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
|
2493
|
+
*
|
2494
|
+
* '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {dp|rm}'
|
2495
|
+
*/
|
2496
|
+
P.toExponential = function (dp, rm) {
|
2497
|
+
if (dp != null) {
|
2498
|
+
intCheck(dp, 0, MAX);
|
2499
|
+
dp++;
|
2500
|
+
}
|
2501
|
+
return format(this, dp, rm, 1);
|
2502
|
+
};
|
2503
|
+
|
2504
|
+
|
2505
|
+
/*
|
2506
|
+
* Return a string representing the value of this BigNumber in fixed-point notation rounding
|
2507
|
+
* to dp fixed decimal places using rounding mode rm, or ROUNDING_MODE if rm is omitted.
|
2508
|
+
*
|
2509
|
+
* Note: as with JavaScript's number type, (-0).toFixed(0) is '0',
|
2510
|
+
* but e.g. (-0.00001).toFixed(0) is '-0'.
|
2511
|
+
*
|
2512
|
+
* [dp] {number} Decimal places. Integer, 0 to MAX inclusive.
|
2513
|
+
* [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
|
2514
|
+
*
|
2515
|
+
* '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {dp|rm}'
|
2516
|
+
*/
|
2517
|
+
P.toFixed = function (dp, rm) {
|
2518
|
+
if (dp != null) {
|
2519
|
+
intCheck(dp, 0, MAX);
|
2520
|
+
dp = dp + this.e + 1;
|
2521
|
+
}
|
2522
|
+
return format(this, dp, rm);
|
2523
|
+
};
|
2524
|
+
|
2525
|
+
|
2526
|
+
/*
|
2527
|
+
* Return a string representing the value of this BigNumber in fixed-point notation rounded
|
2528
|
+
* using rm or ROUNDING_MODE to dp decimal places, and formatted according to the properties
|
2529
|
+
* of the format or FORMAT object (see BigNumber.set).
|
2530
|
+
*
|
2531
|
+
* The formatting object may contain some or all of the properties shown below.
|
2532
|
+
*
|
2533
|
+
* FORMAT = {
|
2534
|
+
* prefix: '',
|
2535
|
+
* groupSize: 3,
|
2536
|
+
* secondaryGroupSize: 0,
|
2537
|
+
* groupSeparator: ',',
|
2538
|
+
* decimalSeparator: '.',
|
2539
|
+
* fractionGroupSize: 0,
|
2540
|
+
* fractionGroupSeparator: '\xA0', // non-breaking space
|
2541
|
+
* suffix: ''
|
2542
|
+
* };
|
2543
|
+
*
|
2544
|
+
* [dp] {number} Decimal places. Integer, 0 to MAX inclusive.
|
2545
|
+
* [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
|
2546
|
+
* [format] {object} Formatting options. See FORMAT pbject above.
|
2547
|
+
*
|
2548
|
+
* '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {dp|rm}'
|
2549
|
+
* '[BigNumber Error] Argument not an object: {format}'
|
2550
|
+
*/
|
2551
|
+
P.toFormat = function (dp, rm, format) {
|
2552
|
+
var str,
|
2553
|
+
x = this;
|
2554
|
+
|
2555
|
+
if (format == null) {
|
2556
|
+
if (dp != null && rm && typeof rm == 'object') {
|
2557
|
+
format = rm;
|
2558
|
+
rm = null;
|
2559
|
+
} else if (dp && typeof dp == 'object') {
|
2560
|
+
format = dp;
|
2561
|
+
dp = rm = null;
|
2562
|
+
} else {
|
2563
|
+
format = FORMAT;
|
2564
|
+
}
|
2565
|
+
} else if (typeof format != 'object') {
|
2566
|
+
throw Error
|
2567
|
+
(bignumberError + 'Argument not an object: ' + format);
|
2568
|
+
}
|
2569
|
+
|
2570
|
+
str = x.toFixed(dp, rm);
|
2571
|
+
|
2572
|
+
if (x.c) {
|
2573
|
+
var i,
|
2574
|
+
arr = str.split('.'),
|
2575
|
+
g1 = +format.groupSize,
|
2576
|
+
g2 = +format.secondaryGroupSize,
|
2577
|
+
groupSeparator = format.groupSeparator || '',
|
2578
|
+
intPart = arr[0],
|
2579
|
+
fractionPart = arr[1],
|
2580
|
+
isNeg = x.s < 0,
|
2581
|
+
intDigits = isNeg ? intPart.slice(1) : intPart,
|
2582
|
+
len = intDigits.length;
|
2583
|
+
|
2584
|
+
if (g2) {
|
2585
|
+
i = g1;
|
2586
|
+
g1 = g2;
|
2587
|
+
g2 = i;
|
2588
|
+
len -= i;
|
2589
|
+
}
|
2590
|
+
|
2591
|
+
if (g1 > 0 && len > 0) {
|
2592
|
+
i = len % g1 || g1;
|
2593
|
+
intPart = intDigits.substr(0, i);
|
2594
|
+
for (; i < len; i += g1) intPart += groupSeparator + intDigits.substr(i, g1);
|
2595
|
+
if (g2 > 0) intPart += groupSeparator + intDigits.slice(i);
|
2596
|
+
if (isNeg) intPart = '-' + intPart;
|
2597
|
+
}
|
2598
|
+
|
2599
|
+
str = fractionPart
|
2600
|
+
? intPart + (format.decimalSeparator || '') + ((g2 = +format.fractionGroupSize)
|
2601
|
+
? fractionPart.replace(new RegExp('\\d{' + g2 + '}\\B', 'g'),
|
2602
|
+
'$&' + (format.fractionGroupSeparator || ''))
|
2603
|
+
: fractionPart)
|
2604
|
+
: intPart;
|
2605
|
+
}
|
2606
|
+
|
2607
|
+
return (format.prefix || '') + str + (format.suffix || '');
|
2608
|
+
};
|
2609
|
+
|
2610
|
+
|
2611
|
+
/*
|
2612
|
+
* Return an array of two BigNumbers representing the value of this BigNumber as a simple
|
2613
|
+
* fraction with an integer numerator and an integer denominator.
|
2614
|
+
* The denominator will be a positive non-zero value less than or equal to the specified
|
2615
|
+
* maximum denominator. If a maximum denominator is not specified, the denominator will be
|
2616
|
+
* the lowest value necessary to represent the number exactly.
|
2617
|
+
*
|
2618
|
+
* [md] {number|string|BigNumber} Integer >= 1, or Infinity. The maximum denominator.
|
2619
|
+
*
|
2620
|
+
* '[BigNumber Error] Argument {not an integer|out of range} : {md}'
|
2621
|
+
*/
|
2622
|
+
P.toFraction = function (md) {
|
2623
|
+
var d, d0, d1, d2, e, exp, n, n0, n1, q, r, s,
|
2624
|
+
x = this,
|
2625
|
+
xc = x.c;
|
2626
|
+
|
2627
|
+
if (md != null) {
|
2628
|
+
n = new BigNumber(md);
|
2629
|
+
|
2630
|
+
// Throw if md is less than one or is not an integer, unless it is Infinity.
|
2631
|
+
if (!n.isInteger() && (n.c || n.s !== 1) || n.lt(ONE)) {
|
2632
|
+
throw Error
|
2633
|
+
(bignumberError + 'Argument ' +
|
2634
|
+
(n.isInteger() ? 'out of range: ' : 'not an integer: ') + valueOf(n));
|
2635
|
+
}
|
2636
|
+
}
|
2637
|
+
|
2638
|
+
if (!xc) return new BigNumber(x);
|
2639
|
+
|
2640
|
+
d = new BigNumber(ONE);
|
2641
|
+
n1 = d0 = new BigNumber(ONE);
|
2642
|
+
d1 = n0 = new BigNumber(ONE);
|
2643
|
+
s = coeffToString(xc);
|
2644
|
+
|
2645
|
+
// Determine initial denominator.
|
2646
|
+
// d is a power of 10 and the minimum max denominator that specifies the value exactly.
|
2647
|
+
e = d.e = s.length - x.e - 1;
|
2648
|
+
d.c[0] = POWS_TEN[(exp = e % LOG_BASE) < 0 ? LOG_BASE + exp : exp];
|
2649
|
+
md = !md || n.comparedTo(d) > 0 ? (e > 0 ? d : n1) : n;
|
2650
|
+
|
2651
|
+
exp = MAX_EXP;
|
2652
|
+
MAX_EXP = 1 / 0;
|
2653
|
+
n = new BigNumber(s);
|
2654
|
+
|
2655
|
+
// n0 = d1 = 0
|
2656
|
+
n0.c[0] = 0;
|
2657
|
+
|
2658
|
+
for (; ;) {
|
2659
|
+
q = div(n, d, 0, 1);
|
2660
|
+
d2 = d0.plus(q.times(d1));
|
2661
|
+
if (d2.comparedTo(md) == 1) break;
|
2662
|
+
d0 = d1;
|
2663
|
+
d1 = d2;
|
2664
|
+
n1 = n0.plus(q.times(d2 = n1));
|
2665
|
+
n0 = d2;
|
2666
|
+
d = n.minus(q.times(d2 = d));
|
2667
|
+
n = d2;
|
2668
|
+
}
|
2669
|
+
|
2670
|
+
d2 = div(md.minus(d0), d1, 0, 1);
|
2671
|
+
n0 = n0.plus(d2.times(n1));
|
2672
|
+
d0 = d0.plus(d2.times(d1));
|
2673
|
+
n0.s = n1.s = x.s;
|
2674
|
+
e = e * 2;
|
2675
|
+
|
2676
|
+
// Determine which fraction is closer to x, n0/d0 or n1/d1
|
2677
|
+
r = div(n1, d1, e, ROUNDING_MODE).minus(x).abs().comparedTo(
|
2678
|
+
div(n0, d0, e, ROUNDING_MODE).minus(x).abs()) < 1 ? [n1, d1] : [n0, d0];
|
2679
|
+
|
2680
|
+
MAX_EXP = exp;
|
2681
|
+
|
2682
|
+
return r;
|
2683
|
+
};
|
2684
|
+
|
2685
|
+
|
2686
|
+
/*
|
2687
|
+
* Return the value of this BigNumber converted to a number primitive.
|
2688
|
+
*/
|
2689
|
+
P.toNumber = function () {
|
2690
|
+
return +valueOf(this);
|
2691
|
+
};
|
2692
|
+
|
2693
|
+
|
2694
|
+
/*
|
2695
|
+
* Return a string representing the value of this BigNumber rounded to sd significant digits
|
2696
|
+
* using rounding mode rm or ROUNDING_MODE. If sd is less than the number of digits
|
2697
|
+
* necessary to represent the integer part of the value in fixed-point notation, then use
|
2698
|
+
* exponential notation.
|
2699
|
+
*
|
2700
|
+
* [sd] {number} Significant digits. Integer, 1 to MAX inclusive.
|
2701
|
+
* [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
|
2702
|
+
*
|
2703
|
+
* '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {sd|rm}'
|
2704
|
+
*/
|
2705
|
+
P.toPrecision = function (sd, rm) {
|
2706
|
+
if (sd != null) intCheck(sd, 1, MAX);
|
2707
|
+
return format(this, sd, rm, 2);
|
2708
|
+
};
|
2709
|
+
|
2710
|
+
|
2711
|
+
/*
|
2712
|
+
* Return a string representing the value of this BigNumber in base b, or base 10 if b is
|
2713
|
+
* omitted. If a base is specified, including base 10, round according to DECIMAL_PLACES and
|
2714
|
+
* ROUNDING_MODE. If a base is not specified, and this BigNumber has a positive exponent
|
2715
|
+
* that is equal to or greater than TO_EXP_POS, or a negative exponent equal to or less than
|
2716
|
+
* TO_EXP_NEG, return exponential notation.
|
2717
|
+
*
|
2718
|
+
* [b] {number} Integer, 2 to ALPHABET.length inclusive.
|
2719
|
+
*
|
2720
|
+
* '[BigNumber Error] Base {not a primitive number|not an integer|out of range}: {b}'
|
2721
|
+
*/
|
2722
|
+
P.toString = function (b) {
|
2723
|
+
var str,
|
2724
|
+
n = this,
|
2725
|
+
s = n.s,
|
2726
|
+
e = n.e;
|
2727
|
+
|
2728
|
+
// Infinity or NaN?
|
2729
|
+
if (e === null) {
|
2730
|
+
if (s) {
|
2731
|
+
str = 'Infinity';
|
2732
|
+
if (s < 0) str = '-' + str;
|
2733
|
+
} else {
|
2734
|
+
str = 'NaN';
|
2735
|
+
}
|
2736
|
+
} else {
|
2737
|
+
if (b == null) {
|
2738
|
+
str = e <= TO_EXP_NEG || e >= TO_EXP_POS
|
2739
|
+
? toExponential(coeffToString(n.c), e)
|
2740
|
+
: toFixedPoint(coeffToString(n.c), e, '0');
|
2741
|
+
} else if (b === 10 && alphabetHasNormalDecimalDigits) {
|
2742
|
+
n = round(new BigNumber(n), DECIMAL_PLACES + e + 1, ROUNDING_MODE);
|
2743
|
+
str = toFixedPoint(coeffToString(n.c), n.e, '0');
|
2744
|
+
} else {
|
2745
|
+
intCheck(b, 2, ALPHABET.length, 'Base');
|
2746
|
+
str = convertBase(toFixedPoint(coeffToString(n.c), e, '0'), 10, b, s, true);
|
2747
|
+
}
|
2748
|
+
|
2749
|
+
if (s < 0 && n.c[0]) str = '-' + str;
|
2750
|
+
}
|
2751
|
+
|
2752
|
+
return str;
|
2753
|
+
};
|
2754
|
+
|
2755
|
+
|
2756
|
+
/*
|
2757
|
+
* Return as toString, but do not accept a base argument, and include the minus sign for
|
2758
|
+
* negative zero.
|
2759
|
+
*/
|
2760
|
+
P.valueOf = P.toJSON = function () {
|
2761
|
+
return valueOf(this);
|
2762
|
+
};
|
2763
|
+
|
2764
|
+
|
2765
|
+
P._isBigNumber = true;
|
2766
|
+
|
2767
|
+
if (configObject != null) BigNumber.set(configObject);
|
2768
|
+
|
2769
|
+
return BigNumber;
|
2770
|
+
}
|
2771
|
+
|
2772
|
+
|
2773
|
+
// PRIVATE HELPER FUNCTIONS
|
2774
|
+
|
2775
|
+
// These functions don't need access to variables,
|
2776
|
+
// e.g. DECIMAL_PLACES, in the scope of the `clone` function above.
|
2777
|
+
|
2778
|
+
|
2779
|
+
function bitFloor(n) {
|
2780
|
+
var i = n | 0;
|
2781
|
+
return n > 0 || n === i ? i : i - 1;
|
2782
|
+
}
|
2783
|
+
|
2784
|
+
|
2785
|
+
// Return a coefficient array as a string of base 10 digits.
|
2786
|
+
function coeffToString(a) {
|
2787
|
+
var s, z,
|
2788
|
+
i = 1,
|
2789
|
+
j = a.length,
|
2790
|
+
r = a[0] + '';
|
2791
|
+
|
2792
|
+
for (; i < j;) {
|
2793
|
+
s = a[i++] + '';
|
2794
|
+
z = LOG_BASE - s.length;
|
2795
|
+
for (; z--; s = '0' + s);
|
2796
|
+
r += s;
|
2797
|
+
}
|
2798
|
+
|
2799
|
+
// Determine trailing zeros.
|
2800
|
+
for (j = r.length; r.charCodeAt(--j) === 48;);
|
2801
|
+
|
2802
|
+
return r.slice(0, j + 1 || 1);
|
2803
|
+
}
|
2804
|
+
|
2805
|
+
|
2806
|
+
// Compare the value of BigNumbers x and y.
|
2807
|
+
function compare(x, y) {
|
2808
|
+
var a, b,
|
2809
|
+
xc = x.c,
|
2810
|
+
yc = y.c,
|
2811
|
+
i = x.s,
|
2812
|
+
j = y.s,
|
2813
|
+
k = x.e,
|
2814
|
+
l = y.e;
|
2815
|
+
|
2816
|
+
// Either NaN?
|
2817
|
+
if (!i || !j) return null;
|
2818
|
+
|
2819
|
+
a = xc && !xc[0];
|
2820
|
+
b = yc && !yc[0];
|
2821
|
+
|
2822
|
+
// Either zero?
|
2823
|
+
if (a || b) return a ? b ? 0 : -j : i;
|
2824
|
+
|
2825
|
+
// Signs differ?
|
2826
|
+
if (i != j) return i;
|
2827
|
+
|
2828
|
+
a = i < 0;
|
2829
|
+
b = k == l;
|
2830
|
+
|
2831
|
+
// Either Infinity?
|
2832
|
+
if (!xc || !yc) return b ? 0 : !xc ^ a ? 1 : -1;
|
2833
|
+
|
2834
|
+
// Compare exponents.
|
2835
|
+
if (!b) return k > l ^ a ? 1 : -1;
|
2836
|
+
|
2837
|
+
j = (k = xc.length) < (l = yc.length) ? k : l;
|
2838
|
+
|
2839
|
+
// Compare digit by digit.
|
2840
|
+
for (i = 0; i < j; i++) if (xc[i] != yc[i]) return xc[i] > yc[i] ^ a ? 1 : -1;
|
2841
|
+
|
2842
|
+
// Compare lengths.
|
2843
|
+
return k == l ? 0 : k > l ^ a ? 1 : -1;
|
2844
|
+
}
|
2845
|
+
|
2846
|
+
|
2847
|
+
/*
|
2848
|
+
* Check that n is a primitive number, an integer, and in range, otherwise throw.
|
2849
|
+
*/
|
2850
|
+
function intCheck(n, min, max, name) {
|
2851
|
+
if (n < min || n > max || n !== mathfloor(n)) {
|
2852
|
+
throw Error
|
2853
|
+
(bignumberError + (name || 'Argument') + (typeof n == 'number'
|
2854
|
+
? n < min || n > max ? ' out of range: ' : ' not an integer: '
|
2855
|
+
: ' not a primitive number: ') + String(n));
|
2856
|
+
}
|
2857
|
+
}
|
2858
|
+
|
2859
|
+
|
2860
|
+
// Assumes finite n.
|
2861
|
+
function isOdd(n) {
|
2862
|
+
var k = n.c.length - 1;
|
2863
|
+
return bitFloor(n.e / LOG_BASE) == k && n.c[k] % 2 != 0;
|
2864
|
+
}
|
2865
|
+
|
2866
|
+
|
2867
|
+
function toExponential(str, e) {
|
2868
|
+
return (str.length > 1 ? str.charAt(0) + '.' + str.slice(1) : str) +
|
2869
|
+
(e < 0 ? 'e' : 'e+') + e;
|
2870
|
+
}
|
2871
|
+
|
2872
|
+
|
2873
|
+
function toFixedPoint(str, e, z) {
|
2874
|
+
var len, zs;
|
2875
|
+
|
2876
|
+
// Negative exponent?
|
2877
|
+
if (e < 0) {
|
2878
|
+
|
2879
|
+
// Prepend zeros.
|
2880
|
+
for (zs = z + '.'; ++e; zs += z);
|
2881
|
+
str = zs + str;
|
2882
|
+
|
2883
|
+
// Positive exponent
|
2884
|
+
} else {
|
2885
|
+
len = str.length;
|
2886
|
+
|
2887
|
+
// Append zeros.
|
2888
|
+
if (++e > len) {
|
2889
|
+
for (zs = z, e -= len; --e; zs += z);
|
2890
|
+
str += zs;
|
2891
|
+
} else if (e < len) {
|
2892
|
+
str = str.slice(0, e) + '.' + str.slice(e);
|
2893
|
+
}
|
2894
|
+
}
|
2895
|
+
|
2896
|
+
return str;
|
2897
|
+
}
|
2898
|
+
|
2899
|
+
|
2900
|
+
// EXPORT
|
2901
|
+
|
2902
|
+
|
2903
|
+
BigNumber = clone();
|
2904
|
+
BigNumber['default'] = BigNumber.BigNumber = BigNumber;
|
2905
|
+
|
2906
|
+
// AMD.
|
2907
|
+
if (typeof define == 'function' && define.amd) {
|
2908
|
+
define(function () { return BigNumber; });
|
2909
|
+
|
2910
|
+
// Node.js and other environments that support module.exports.
|
2911
|
+
} else if (typeof module != 'undefined' && module.exports) {
|
2912
|
+
module.exports = BigNumber;
|
2913
|
+
|
2914
|
+
// Browser.
|
2915
|
+
} else {
|
2916
|
+
if (!globalObject) {
|
2917
|
+
globalObject = typeof self != 'undefined' && self ? self : window;
|
2918
|
+
}
|
2919
|
+
|
2920
|
+
globalObject.BigNumber = BigNumber;
|
2921
|
+
}
|
2922
|
+
})(this);
|