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