@lango-project/lango-js 0.1.0 → 0.1.1

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