@xchainjs/xchain-thorchain 0.24.1 → 0.25.2-alpha.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,4230 @@ 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
+
2947
+ var long_1 = Long;
2948
+
2949
+ /**
2950
+ * wasm optimizations, to do native i64 multiplication and divide
2951
+ */
2952
+ var wasm = null;
2953
+
2954
+ try {
2955
+ wasm = new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([
2956
+ 0, 97, 115, 109, 1, 0, 0, 0, 1, 13, 2, 96, 0, 1, 127, 96, 4, 127, 127, 127, 127, 1, 127, 3, 7, 6, 0, 1, 1, 1, 1, 1, 6, 6, 1, 127, 1, 65, 0, 11, 7, 50, 6, 3, 109, 117, 108, 0, 1, 5, 100, 105, 118, 95, 115, 0, 2, 5, 100, 105, 118, 95, 117, 0, 3, 5, 114, 101, 109, 95, 115, 0, 4, 5, 114, 101, 109, 95, 117, 0, 5, 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, 10, 191, 1, 6, 4, 0, 35, 0, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11
2957
+ ])), {}).exports;
2958
+ } catch (e) {
2959
+ // no wasm support :(
2960
+ }
2961
+
2962
+ /**
2963
+ * Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers.
2964
+ * See the from* functions below for more convenient ways of constructing Longs.
2965
+ * @exports Long
2966
+ * @class A Long class for representing a 64 bit two's-complement integer value.
2967
+ * @param {number} low The low (signed) 32 bits of the long
2968
+ * @param {number} high The high (signed) 32 bits of the long
2969
+ * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
2970
+ * @constructor
2971
+ */
2972
+ function Long(low, high, unsigned) {
2973
+
2974
+ /**
2975
+ * The low 32 bits as a signed value.
2976
+ * @type {number}
2977
+ */
2978
+ this.low = low | 0;
2979
+
2980
+ /**
2981
+ * The high 32 bits as a signed value.
2982
+ * @type {number}
2983
+ */
2984
+ this.high = high | 0;
2985
+
2986
+ /**
2987
+ * Whether unsigned or not.
2988
+ * @type {boolean}
2989
+ */
2990
+ this.unsigned = !!unsigned;
2991
+ }
2992
+
2993
+ // The internal representation of a long is the two given signed, 32-bit values.
2994
+ // We use 32-bit pieces because these are the size of integers on which
2995
+ // Javascript performs bit-operations. For operations like addition and
2996
+ // multiplication, we split each number into 16 bit pieces, which can easily be
2997
+ // multiplied within Javascript's floating-point representation without overflow
2998
+ // or change in sign.
2999
+ //
3000
+ // In the algorithms below, we frequently reduce the negative case to the
3001
+ // positive case by negating the input(s) and then post-processing the result.
3002
+ // Note that we must ALWAYS check specially whether those values are MIN_VALUE
3003
+ // (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
3004
+ // a positive number, it overflows back into a negative). Not handling this
3005
+ // case would often result in infinite recursion.
3006
+ //
3007
+ // Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the from*
3008
+ // methods on which they depend.
3009
+
3010
+ /**
3011
+ * An indicator used to reliably determine if an object is a Long or not.
3012
+ * @type {boolean}
3013
+ * @const
3014
+ * @private
3015
+ */
3016
+ Long.prototype.__isLong__;
3017
+
3018
+ Object.defineProperty(Long.prototype, "__isLong__", { value: true });
3019
+
3020
+ /**
3021
+ * @function
3022
+ * @param {*} obj Object
3023
+ * @returns {boolean}
3024
+ * @inner
3025
+ */
3026
+ function isLong(obj) {
3027
+ return (obj && obj["__isLong__"]) === true;
3028
+ }
3029
+
3030
+ /**
3031
+ * Tests if the specified object is a Long.
3032
+ * @function
3033
+ * @param {*} obj Object
3034
+ * @returns {boolean}
3035
+ */
3036
+ Long.isLong = isLong;
3037
+
3038
+ /**
3039
+ * A cache of the Long representations of small integer values.
3040
+ * @type {!Object}
3041
+ * @inner
3042
+ */
3043
+ var INT_CACHE = {};
3044
+
3045
+ /**
3046
+ * A cache of the Long representations of small unsigned integer values.
3047
+ * @type {!Object}
3048
+ * @inner
3049
+ */
3050
+ var UINT_CACHE = {};
3051
+
3052
+ /**
3053
+ * @param {number} value
3054
+ * @param {boolean=} unsigned
3055
+ * @returns {!Long}
3056
+ * @inner
3057
+ */
3058
+ function fromInt(value, unsigned) {
3059
+ var obj, cachedObj, cache;
3060
+ if (unsigned) {
3061
+ value >>>= 0;
3062
+ if (cache = (0 <= value && value < 256)) {
3063
+ cachedObj = UINT_CACHE[value];
3064
+ if (cachedObj)
3065
+ return cachedObj;
3066
+ }
3067
+ obj = fromBits(value, (value | 0) < 0 ? -1 : 0, true);
3068
+ if (cache)
3069
+ UINT_CACHE[value] = obj;
3070
+ return obj;
3071
+ } else {
3072
+ value |= 0;
3073
+ if (cache = (-128 <= value && value < 128)) {
3074
+ cachedObj = INT_CACHE[value];
3075
+ if (cachedObj)
3076
+ return cachedObj;
3077
+ }
3078
+ obj = fromBits(value, value < 0 ? -1 : 0, false);
3079
+ if (cache)
3080
+ INT_CACHE[value] = obj;
3081
+ return obj;
3082
+ }
3083
+ }
3084
+
3085
+ /**
3086
+ * Returns a Long representing the given 32 bit integer value.
3087
+ * @function
3088
+ * @param {number} value The 32 bit integer in question
3089
+ * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
3090
+ * @returns {!Long} The corresponding Long value
3091
+ */
3092
+ Long.fromInt = fromInt;
3093
+
3094
+ /**
3095
+ * @param {number} value
3096
+ * @param {boolean=} unsigned
3097
+ * @returns {!Long}
3098
+ * @inner
3099
+ */
3100
+ function fromNumber(value, unsigned) {
3101
+ if (isNaN(value))
3102
+ return unsigned ? UZERO : ZERO;
3103
+ if (unsigned) {
3104
+ if (value < 0)
3105
+ return UZERO;
3106
+ if (value >= TWO_PWR_64_DBL)
3107
+ return MAX_UNSIGNED_VALUE;
3108
+ } else {
3109
+ if (value <= -TWO_PWR_63_DBL)
3110
+ return MIN_VALUE;
3111
+ if (value + 1 >= TWO_PWR_63_DBL)
3112
+ return MAX_VALUE;
3113
+ }
3114
+ if (value < 0)
3115
+ return fromNumber(-value, unsigned).neg();
3116
+ return fromBits((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
3117
+ }
3118
+
3119
+ /**
3120
+ * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
3121
+ * @function
3122
+ * @param {number} value The number in question
3123
+ * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
3124
+ * @returns {!Long} The corresponding Long value
3125
+ */
3126
+ Long.fromNumber = fromNumber;
3127
+
3128
+ /**
3129
+ * @param {number} lowBits
3130
+ * @param {number} highBits
3131
+ * @param {boolean=} unsigned
3132
+ * @returns {!Long}
3133
+ * @inner
3134
+ */
3135
+ function fromBits(lowBits, highBits, unsigned) {
3136
+ return new Long(lowBits, highBits, unsigned);
3137
+ }
3138
+
3139
+ /**
3140
+ * Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is
3141
+ * assumed to use 32 bits.
3142
+ * @function
3143
+ * @param {number} lowBits The low 32 bits
3144
+ * @param {number} highBits The high 32 bits
3145
+ * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
3146
+ * @returns {!Long} The corresponding Long value
3147
+ */
3148
+ Long.fromBits = fromBits;
3149
+
3150
+ /**
3151
+ * @function
3152
+ * @param {number} base
3153
+ * @param {number} exponent
3154
+ * @returns {number}
3155
+ * @inner
3156
+ */
3157
+ var pow_dbl = Math.pow; // Used 4 times (4*8 to 15+4)
3158
+
3159
+ /**
3160
+ * @param {string} str
3161
+ * @param {(boolean|number)=} unsigned
3162
+ * @param {number=} radix
3163
+ * @returns {!Long}
3164
+ * @inner
3165
+ */
3166
+ function fromString(str, unsigned, radix) {
3167
+ if (str.length === 0)
3168
+ throw Error('empty string');
3169
+ if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity")
3170
+ return ZERO;
3171
+ if (typeof unsigned === 'number') {
3172
+ // For goog.math.long compatibility
3173
+ radix = unsigned,
3174
+ unsigned = false;
3175
+ } else {
3176
+ unsigned = !! unsigned;
3177
+ }
3178
+ radix = radix || 10;
3179
+ if (radix < 2 || 36 < radix)
3180
+ throw RangeError('radix');
3181
+
3182
+ var p;
3183
+ if ((p = str.indexOf('-')) > 0)
3184
+ throw Error('interior hyphen');
3185
+ else if (p === 0) {
3186
+ return fromString(str.substring(1), unsigned, radix).neg();
3187
+ }
3188
+
3189
+ // Do several (8) digits each time through the loop, so as to
3190
+ // minimize the calls to the very expensive emulated div.
3191
+ var radixToPower = fromNumber(pow_dbl(radix, 8));
3192
+
3193
+ var result = ZERO;
3194
+ for (var i = 0; i < str.length; i += 8) {
3195
+ var size = Math.min(8, str.length - i),
3196
+ value = parseInt(str.substring(i, i + size), radix);
3197
+ if (size < 8) {
3198
+ var power = fromNumber(pow_dbl(radix, size));
3199
+ result = result.mul(power).add(fromNumber(value));
3200
+ } else {
3201
+ result = result.mul(radixToPower);
3202
+ result = result.add(fromNumber(value));
3203
+ }
3204
+ }
3205
+ result.unsigned = unsigned;
3206
+ return result;
3207
+ }
3208
+
3209
+ /**
3210
+ * Returns a Long representation of the given string, written using the specified radix.
3211
+ * @function
3212
+ * @param {string} str The textual representation of the Long
3213
+ * @param {(boolean|number)=} unsigned Whether unsigned or not, defaults to signed
3214
+ * @param {number=} radix The radix in which the text is written (2-36), defaults to 10
3215
+ * @returns {!Long} The corresponding Long value
3216
+ */
3217
+ Long.fromString = fromString;
3218
+
3219
+ /**
3220
+ * @function
3221
+ * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val
3222
+ * @param {boolean=} unsigned
3223
+ * @returns {!Long}
3224
+ * @inner
3225
+ */
3226
+ function fromValue(val, unsigned) {
3227
+ if (typeof val === 'number')
3228
+ return fromNumber(val, unsigned);
3229
+ if (typeof val === 'string')
3230
+ return fromString(val, unsigned);
3231
+ // Throws for non-objects, converts non-instanceof Long:
3232
+ return fromBits(val.low, val.high, typeof unsigned === 'boolean' ? unsigned : val.unsigned);
3233
+ }
3234
+
3235
+ /**
3236
+ * Converts the specified value to a Long using the appropriate from* function for its type.
3237
+ * @function
3238
+ * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val Value
3239
+ * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
3240
+ * @returns {!Long}
3241
+ */
3242
+ Long.fromValue = fromValue;
3243
+
3244
+ // NOTE: the compiler should inline these constant values below and then remove these variables, so there should be
3245
+ // no runtime penalty for these.
3246
+
3247
+ /**
3248
+ * @type {number}
3249
+ * @const
3250
+ * @inner
3251
+ */
3252
+ var TWO_PWR_16_DBL = 1 << 16;
3253
+
3254
+ /**
3255
+ * @type {number}
3256
+ * @const
3257
+ * @inner
3258
+ */
3259
+ var TWO_PWR_24_DBL = 1 << 24;
3260
+
3261
+ /**
3262
+ * @type {number}
3263
+ * @const
3264
+ * @inner
3265
+ */
3266
+ var TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL;
3267
+
3268
+ /**
3269
+ * @type {number}
3270
+ * @const
3271
+ * @inner
3272
+ */
3273
+ var TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL;
3274
+
3275
+ /**
3276
+ * @type {number}
3277
+ * @const
3278
+ * @inner
3279
+ */
3280
+ var TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2;
3281
+
3282
+ /**
3283
+ * @type {!Long}
3284
+ * @const
3285
+ * @inner
3286
+ */
3287
+ var TWO_PWR_24 = fromInt(TWO_PWR_24_DBL);
3288
+
3289
+ /**
3290
+ * @type {!Long}
3291
+ * @inner
3292
+ */
3293
+ var ZERO = fromInt(0);
3294
+
3295
+ /**
3296
+ * Signed zero.
3297
+ * @type {!Long}
3298
+ */
3299
+ Long.ZERO = ZERO;
3300
+
3301
+ /**
3302
+ * @type {!Long}
3303
+ * @inner
3304
+ */
3305
+ var UZERO = fromInt(0, true);
3306
+
3307
+ /**
3308
+ * Unsigned zero.
3309
+ * @type {!Long}
3310
+ */
3311
+ Long.UZERO = UZERO;
3312
+
3313
+ /**
3314
+ * @type {!Long}
3315
+ * @inner
3316
+ */
3317
+ var ONE = fromInt(1);
3318
+
3319
+ /**
3320
+ * Signed one.
3321
+ * @type {!Long}
3322
+ */
3323
+ Long.ONE = ONE;
3324
+
3325
+ /**
3326
+ * @type {!Long}
3327
+ * @inner
3328
+ */
3329
+ var UONE = fromInt(1, true);
3330
+
3331
+ /**
3332
+ * Unsigned one.
3333
+ * @type {!Long}
3334
+ */
3335
+ Long.UONE = UONE;
3336
+
3337
+ /**
3338
+ * @type {!Long}
3339
+ * @inner
3340
+ */
3341
+ var NEG_ONE = fromInt(-1);
3342
+
3343
+ /**
3344
+ * Signed negative one.
3345
+ * @type {!Long}
3346
+ */
3347
+ Long.NEG_ONE = NEG_ONE;
3348
+
3349
+ /**
3350
+ * @type {!Long}
3351
+ * @inner
3352
+ */
3353
+ var MAX_VALUE = fromBits(0xFFFFFFFF|0, 0x7FFFFFFF|0, false);
3354
+
3355
+ /**
3356
+ * Maximum signed value.
3357
+ * @type {!Long}
3358
+ */
3359
+ Long.MAX_VALUE = MAX_VALUE;
3360
+
3361
+ /**
3362
+ * @type {!Long}
3363
+ * @inner
3364
+ */
3365
+ var MAX_UNSIGNED_VALUE = fromBits(0xFFFFFFFF|0, 0xFFFFFFFF|0, true);
3366
+
3367
+ /**
3368
+ * Maximum unsigned value.
3369
+ * @type {!Long}
3370
+ */
3371
+ Long.MAX_UNSIGNED_VALUE = MAX_UNSIGNED_VALUE;
3372
+
3373
+ /**
3374
+ * @type {!Long}
3375
+ * @inner
3376
+ */
3377
+ var MIN_VALUE = fromBits(0, 0x80000000|0, false);
3378
+
3379
+ /**
3380
+ * Minimum signed value.
3381
+ * @type {!Long}
3382
+ */
3383
+ Long.MIN_VALUE = MIN_VALUE;
3384
+
3385
+ /**
3386
+ * @alias Long.prototype
3387
+ * @inner
3388
+ */
3389
+ var LongPrototype = Long.prototype;
3390
+
3391
+ /**
3392
+ * Converts the Long to a 32 bit integer, assuming it is a 32 bit integer.
3393
+ * @returns {number}
3394
+ */
3395
+ LongPrototype.toInt = function toInt() {
3396
+ return this.unsigned ? this.low >>> 0 : this.low;
3397
+ };
3398
+
3399
+ /**
3400
+ * Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa).
3401
+ * @returns {number}
3402
+ */
3403
+ LongPrototype.toNumber = function toNumber() {
3404
+ if (this.unsigned)
3405
+ return ((this.high >>> 0) * TWO_PWR_32_DBL) + (this.low >>> 0);
3406
+ return this.high * TWO_PWR_32_DBL + (this.low >>> 0);
3407
+ };
3408
+
3409
+ /**
3410
+ * Converts the Long to a string written in the specified radix.
3411
+ * @param {number=} radix Radix (2-36), defaults to 10
3412
+ * @returns {string}
3413
+ * @override
3414
+ * @throws {RangeError} If `radix` is out of range
3415
+ */
3416
+ LongPrototype.toString = function toString(radix) {
3417
+ radix = radix || 10;
3418
+ if (radix < 2 || 36 < radix)
3419
+ throw RangeError('radix');
3420
+ if (this.isZero())
3421
+ return '0';
3422
+ if (this.isNegative()) { // Unsigned Longs are never negative
3423
+ if (this.eq(MIN_VALUE)) {
3424
+ // We need to change the Long value before it can be negated, so we remove
3425
+ // the bottom-most digit in this base and then recurse to do the rest.
3426
+ var radixLong = fromNumber(radix),
3427
+ div = this.div(radixLong),
3428
+ rem1 = div.mul(radixLong).sub(this);
3429
+ return div.toString(radix) + rem1.toInt().toString(radix);
3430
+ } else
3431
+ return '-' + this.neg().toString(radix);
3432
+ }
3433
+
3434
+ // Do several (6) digits each time through the loop, so as to
3435
+ // minimize the calls to the very expensive emulated div.
3436
+ var radixToPower = fromNumber(pow_dbl(radix, 6), this.unsigned),
3437
+ rem = this;
3438
+ var result = '';
3439
+ while (true) {
3440
+ var remDiv = rem.div(radixToPower),
3441
+ intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0,
3442
+ digits = intval.toString(radix);
3443
+ rem = remDiv;
3444
+ if (rem.isZero())
3445
+ return digits + result;
3446
+ else {
3447
+ while (digits.length < 6)
3448
+ digits = '0' + digits;
3449
+ result = '' + digits + result;
3450
+ }
3451
+ }
3452
+ };
3453
+
3454
+ /**
3455
+ * Gets the high 32 bits as a signed integer.
3456
+ * @returns {number} Signed high bits
3457
+ */
3458
+ LongPrototype.getHighBits = function getHighBits() {
3459
+ return this.high;
3460
+ };
3461
+
3462
+ /**
3463
+ * Gets the high 32 bits as an unsigned integer.
3464
+ * @returns {number} Unsigned high bits
3465
+ */
3466
+ LongPrototype.getHighBitsUnsigned = function getHighBitsUnsigned() {
3467
+ return this.high >>> 0;
3468
+ };
3469
+
3470
+ /**
3471
+ * Gets the low 32 bits as a signed integer.
3472
+ * @returns {number} Signed low bits
3473
+ */
3474
+ LongPrototype.getLowBits = function getLowBits() {
3475
+ return this.low;
3476
+ };
3477
+
3478
+ /**
3479
+ * Gets the low 32 bits as an unsigned integer.
3480
+ * @returns {number} Unsigned low bits
3481
+ */
3482
+ LongPrototype.getLowBitsUnsigned = function getLowBitsUnsigned() {
3483
+ return this.low >>> 0;
3484
+ };
3485
+
3486
+ /**
3487
+ * Gets the number of bits needed to represent the absolute value of this Long.
3488
+ * @returns {number}
3489
+ */
3490
+ LongPrototype.getNumBitsAbs = function getNumBitsAbs() {
3491
+ if (this.isNegative()) // Unsigned Longs are never negative
3492
+ return this.eq(MIN_VALUE) ? 64 : this.neg().getNumBitsAbs();
3493
+ var val = this.high != 0 ? this.high : this.low;
3494
+ for (var bit = 31; bit > 0; bit--)
3495
+ if ((val & (1 << bit)) != 0)
3496
+ break;
3497
+ return this.high != 0 ? bit + 33 : bit + 1;
3498
+ };
3499
+
3500
+ /**
3501
+ * Tests if this Long's value equals zero.
3502
+ * @returns {boolean}
3503
+ */
3504
+ LongPrototype.isZero = function isZero() {
3505
+ return this.high === 0 && this.low === 0;
3506
+ };
3507
+
3508
+ /**
3509
+ * Tests if this Long's value equals zero. This is an alias of {@link Long#isZero}.
3510
+ * @returns {boolean}
3511
+ */
3512
+ LongPrototype.eqz = LongPrototype.isZero;
3513
+
3514
+ /**
3515
+ * Tests if this Long's value is negative.
3516
+ * @returns {boolean}
3517
+ */
3518
+ LongPrototype.isNegative = function isNegative() {
3519
+ return !this.unsigned && this.high < 0;
3520
+ };
3521
+
3522
+ /**
3523
+ * Tests if this Long's value is positive.
3524
+ * @returns {boolean}
3525
+ */
3526
+ LongPrototype.isPositive = function isPositive() {
3527
+ return this.unsigned || this.high >= 0;
3528
+ };
3529
+
3530
+ /**
3531
+ * Tests if this Long's value is odd.
3532
+ * @returns {boolean}
3533
+ */
3534
+ LongPrototype.isOdd = function isOdd() {
3535
+ return (this.low & 1) === 1;
3536
+ };
3537
+
3538
+ /**
3539
+ * Tests if this Long's value is even.
3540
+ * @returns {boolean}
3541
+ */
3542
+ LongPrototype.isEven = function isEven() {
3543
+ return (this.low & 1) === 0;
3544
+ };
3545
+
3546
+ /**
3547
+ * Tests if this Long's value equals the specified's.
3548
+ * @param {!Long|number|string} other Other value
3549
+ * @returns {boolean}
3550
+ */
3551
+ LongPrototype.equals = function equals(other) {
3552
+ if (!isLong(other))
3553
+ other = fromValue(other);
3554
+ if (this.unsigned !== other.unsigned && (this.high >>> 31) === 1 && (other.high >>> 31) === 1)
3555
+ return false;
3556
+ return this.high === other.high && this.low === other.low;
3557
+ };
3558
+
3559
+ /**
3560
+ * Tests if this Long's value equals the specified's. This is an alias of {@link Long#equals}.
3561
+ * @function
3562
+ * @param {!Long|number|string} other Other value
3563
+ * @returns {boolean}
3564
+ */
3565
+ LongPrototype.eq = LongPrototype.equals;
3566
+
3567
+ /**
3568
+ * Tests if this Long's value differs from the specified's.
3569
+ * @param {!Long|number|string} other Other value
3570
+ * @returns {boolean}
3571
+ */
3572
+ LongPrototype.notEquals = function notEquals(other) {
3573
+ return !this.eq(/* validates */ other);
3574
+ };
3575
+
3576
+ /**
3577
+ * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}.
3578
+ * @function
3579
+ * @param {!Long|number|string} other Other value
3580
+ * @returns {boolean}
3581
+ */
3582
+ LongPrototype.neq = LongPrototype.notEquals;
3583
+
3584
+ /**
3585
+ * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}.
3586
+ * @function
3587
+ * @param {!Long|number|string} other Other value
3588
+ * @returns {boolean}
3589
+ */
3590
+ LongPrototype.ne = LongPrototype.notEquals;
3591
+
3592
+ /**
3593
+ * Tests if this Long's value is less than the specified's.
3594
+ * @param {!Long|number|string} other Other value
3595
+ * @returns {boolean}
3596
+ */
3597
+ LongPrototype.lessThan = function lessThan(other) {
3598
+ return this.comp(/* validates */ other) < 0;
3599
+ };
3600
+
3601
+ /**
3602
+ * Tests if this Long's value is less than the specified's. This is an alias of {@link Long#lessThan}.
3603
+ * @function
3604
+ * @param {!Long|number|string} other Other value
3605
+ * @returns {boolean}
3606
+ */
3607
+ LongPrototype.lt = LongPrototype.lessThan;
3608
+
3609
+ /**
3610
+ * Tests if this Long's value is less than or equal the specified's.
3611
+ * @param {!Long|number|string} other Other value
3612
+ * @returns {boolean}
3613
+ */
3614
+ LongPrototype.lessThanOrEqual = function lessThanOrEqual(other) {
3615
+ return this.comp(/* validates */ other) <= 0;
3616
+ };
3617
+
3618
+ /**
3619
+ * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}.
3620
+ * @function
3621
+ * @param {!Long|number|string} other Other value
3622
+ * @returns {boolean}
3623
+ */
3624
+ LongPrototype.lte = LongPrototype.lessThanOrEqual;
3625
+
3626
+ /**
3627
+ * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}.
3628
+ * @function
3629
+ * @param {!Long|number|string} other Other value
3630
+ * @returns {boolean}
3631
+ */
3632
+ LongPrototype.le = LongPrototype.lessThanOrEqual;
3633
+
3634
+ /**
3635
+ * Tests if this Long's value is greater than the specified's.
3636
+ * @param {!Long|number|string} other Other value
3637
+ * @returns {boolean}
3638
+ */
3639
+ LongPrototype.greaterThan = function greaterThan(other) {
3640
+ return this.comp(/* validates */ other) > 0;
3641
+ };
3642
+
3643
+ /**
3644
+ * Tests if this Long's value is greater than the specified's. This is an alias of {@link Long#greaterThan}.
3645
+ * @function
3646
+ * @param {!Long|number|string} other Other value
3647
+ * @returns {boolean}
3648
+ */
3649
+ LongPrototype.gt = LongPrototype.greaterThan;
3650
+
3651
+ /**
3652
+ * Tests if this Long's value is greater than or equal the specified's.
3653
+ * @param {!Long|number|string} other Other value
3654
+ * @returns {boolean}
3655
+ */
3656
+ LongPrototype.greaterThanOrEqual = function greaterThanOrEqual(other) {
3657
+ return this.comp(/* validates */ other) >= 0;
3658
+ };
3659
+
3660
+ /**
3661
+ * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}.
3662
+ * @function
3663
+ * @param {!Long|number|string} other Other value
3664
+ * @returns {boolean}
3665
+ */
3666
+ LongPrototype.gte = LongPrototype.greaterThanOrEqual;
3667
+
3668
+ /**
3669
+ * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}.
3670
+ * @function
3671
+ * @param {!Long|number|string} other Other value
3672
+ * @returns {boolean}
3673
+ */
3674
+ LongPrototype.ge = LongPrototype.greaterThanOrEqual;
3675
+
3676
+ /**
3677
+ * Compares this Long's value with the specified's.
3678
+ * @param {!Long|number|string} other Other value
3679
+ * @returns {number} 0 if they are the same, 1 if the this is greater and -1
3680
+ * if the given one is greater
3681
+ */
3682
+ LongPrototype.compare = function compare(other) {
3683
+ if (!isLong(other))
3684
+ other = fromValue(other);
3685
+ if (this.eq(other))
3686
+ return 0;
3687
+ var thisNeg = this.isNegative(),
3688
+ otherNeg = other.isNegative();
3689
+ if (thisNeg && !otherNeg)
3690
+ return -1;
3691
+ if (!thisNeg && otherNeg)
3692
+ return 1;
3693
+ // At this point the sign bits are the same
3694
+ if (!this.unsigned)
3695
+ return this.sub(other).isNegative() ? -1 : 1;
3696
+ // Both are positive if at least one is unsigned
3697
+ return (other.high >>> 0) > (this.high >>> 0) || (other.high === this.high && (other.low >>> 0) > (this.low >>> 0)) ? -1 : 1;
3698
+ };
3699
+
3700
+ /**
3701
+ * Compares this Long's value with the specified's. This is an alias of {@link Long#compare}.
3702
+ * @function
3703
+ * @param {!Long|number|string} other Other value
3704
+ * @returns {number} 0 if they are the same, 1 if the this is greater and -1
3705
+ * if the given one is greater
3706
+ */
3707
+ LongPrototype.comp = LongPrototype.compare;
3708
+
3709
+ /**
3710
+ * Negates this Long's value.
3711
+ * @returns {!Long} Negated Long
3712
+ */
3713
+ LongPrototype.negate = function negate() {
3714
+ if (!this.unsigned && this.eq(MIN_VALUE))
3715
+ return MIN_VALUE;
3716
+ return this.not().add(ONE);
3717
+ };
3718
+
3719
+ /**
3720
+ * Negates this Long's value. This is an alias of {@link Long#negate}.
3721
+ * @function
3722
+ * @returns {!Long} Negated Long
3723
+ */
3724
+ LongPrototype.neg = LongPrototype.negate;
3725
+
3726
+ /**
3727
+ * Returns the sum of this and the specified Long.
3728
+ * @param {!Long|number|string} addend Addend
3729
+ * @returns {!Long} Sum
3730
+ */
3731
+ LongPrototype.add = function add(addend) {
3732
+ if (!isLong(addend))
3733
+ addend = fromValue(addend);
3734
+
3735
+ // Divide each number into 4 chunks of 16 bits, and then sum the chunks.
3736
+
3737
+ var a48 = this.high >>> 16;
3738
+ var a32 = this.high & 0xFFFF;
3739
+ var a16 = this.low >>> 16;
3740
+ var a00 = this.low & 0xFFFF;
3741
+
3742
+ var b48 = addend.high >>> 16;
3743
+ var b32 = addend.high & 0xFFFF;
3744
+ var b16 = addend.low >>> 16;
3745
+ var b00 = addend.low & 0xFFFF;
3746
+
3747
+ var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
3748
+ c00 += a00 + b00;
3749
+ c16 += c00 >>> 16;
3750
+ c00 &= 0xFFFF;
3751
+ c16 += a16 + b16;
3752
+ c32 += c16 >>> 16;
3753
+ c16 &= 0xFFFF;
3754
+ c32 += a32 + b32;
3755
+ c48 += c32 >>> 16;
3756
+ c32 &= 0xFFFF;
3757
+ c48 += a48 + b48;
3758
+ c48 &= 0xFFFF;
3759
+ return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
3760
+ };
3761
+
3762
+ /**
3763
+ * Returns the difference of this and the specified Long.
3764
+ * @param {!Long|number|string} subtrahend Subtrahend
3765
+ * @returns {!Long} Difference
3766
+ */
3767
+ LongPrototype.subtract = function subtract(subtrahend) {
3768
+ if (!isLong(subtrahend))
3769
+ subtrahend = fromValue(subtrahend);
3770
+ return this.add(subtrahend.neg());
3771
+ };
3772
+
3773
+ /**
3774
+ * Returns the difference of this and the specified Long. This is an alias of {@link Long#subtract}.
3775
+ * @function
3776
+ * @param {!Long|number|string} subtrahend Subtrahend
3777
+ * @returns {!Long} Difference
3778
+ */
3779
+ LongPrototype.sub = LongPrototype.subtract;
3780
+
3781
+ /**
3782
+ * Returns the product of this and the specified Long.
3783
+ * @param {!Long|number|string} multiplier Multiplier
3784
+ * @returns {!Long} Product
3785
+ */
3786
+ LongPrototype.multiply = function multiply(multiplier) {
3787
+ if (this.isZero())
3788
+ return ZERO;
3789
+ if (!isLong(multiplier))
3790
+ multiplier = fromValue(multiplier);
3791
+
3792
+ // use wasm support if present
3793
+ if (wasm) {
3794
+ var low = wasm.mul(this.low,
3795
+ this.high,
3796
+ multiplier.low,
3797
+ multiplier.high);
3798
+ return fromBits(low, wasm.get_high(), this.unsigned);
3799
+ }
3800
+
3801
+ if (multiplier.isZero())
3802
+ return ZERO;
3803
+ if (this.eq(MIN_VALUE))
3804
+ return multiplier.isOdd() ? MIN_VALUE : ZERO;
3805
+ if (multiplier.eq(MIN_VALUE))
3806
+ return this.isOdd() ? MIN_VALUE : ZERO;
3807
+
3808
+ if (this.isNegative()) {
3809
+ if (multiplier.isNegative())
3810
+ return this.neg().mul(multiplier.neg());
3811
+ else
3812
+ return this.neg().mul(multiplier).neg();
3813
+ } else if (multiplier.isNegative())
3814
+ return this.mul(multiplier.neg()).neg();
3815
+
3816
+ // If both longs are small, use float multiplication
3817
+ if (this.lt(TWO_PWR_24) && multiplier.lt(TWO_PWR_24))
3818
+ return fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned);
3819
+
3820
+ // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products.
3821
+ // We can skip products that would overflow.
3822
+
3823
+ var a48 = this.high >>> 16;
3824
+ var a32 = this.high & 0xFFFF;
3825
+ var a16 = this.low >>> 16;
3826
+ var a00 = this.low & 0xFFFF;
3827
+
3828
+ var b48 = multiplier.high >>> 16;
3829
+ var b32 = multiplier.high & 0xFFFF;
3830
+ var b16 = multiplier.low >>> 16;
3831
+ var b00 = multiplier.low & 0xFFFF;
3832
+
3833
+ var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
3834
+ c00 += a00 * b00;
3835
+ c16 += c00 >>> 16;
3836
+ c00 &= 0xFFFF;
3837
+ c16 += a16 * b00;
3838
+ c32 += c16 >>> 16;
3839
+ c16 &= 0xFFFF;
3840
+ c16 += a00 * b16;
3841
+ c32 += c16 >>> 16;
3842
+ c16 &= 0xFFFF;
3843
+ c32 += a32 * b00;
3844
+ c48 += c32 >>> 16;
3845
+ c32 &= 0xFFFF;
3846
+ c32 += a16 * b16;
3847
+ c48 += c32 >>> 16;
3848
+ c32 &= 0xFFFF;
3849
+ c32 += a00 * b32;
3850
+ c48 += c32 >>> 16;
3851
+ c32 &= 0xFFFF;
3852
+ c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
3853
+ c48 &= 0xFFFF;
3854
+ return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
3855
+ };
3856
+
3857
+ /**
3858
+ * Returns the product of this and the specified Long. This is an alias of {@link Long#multiply}.
3859
+ * @function
3860
+ * @param {!Long|number|string} multiplier Multiplier
3861
+ * @returns {!Long} Product
3862
+ */
3863
+ LongPrototype.mul = LongPrototype.multiply;
3864
+
3865
+ /**
3866
+ * Returns this Long divided by the specified. The result is signed if this Long is signed or
3867
+ * unsigned if this Long is unsigned.
3868
+ * @param {!Long|number|string} divisor Divisor
3869
+ * @returns {!Long} Quotient
3870
+ */
3871
+ LongPrototype.divide = function divide(divisor) {
3872
+ if (!isLong(divisor))
3873
+ divisor = fromValue(divisor);
3874
+ if (divisor.isZero())
3875
+ throw Error('division by zero');
3876
+
3877
+ // use wasm support if present
3878
+ if (wasm) {
3879
+ // guard against signed division overflow: the largest
3880
+ // negative number / -1 would be 1 larger than the largest
3881
+ // positive number, due to two's complement.
3882
+ if (!this.unsigned &&
3883
+ this.high === -0x80000000 &&
3884
+ divisor.low === -1 && divisor.high === -1) {
3885
+ // be consistent with non-wasm code path
3886
+ return this;
3887
+ }
3888
+ var low = (this.unsigned ? wasm.div_u : wasm.div_s)(
3889
+ this.low,
3890
+ this.high,
3891
+ divisor.low,
3892
+ divisor.high
3893
+ );
3894
+ return fromBits(low, wasm.get_high(), this.unsigned);
3895
+ }
3896
+
3897
+ if (this.isZero())
3898
+ return this.unsigned ? UZERO : ZERO;
3899
+ var approx, rem, res;
3900
+ if (!this.unsigned) {
3901
+ // This section is only relevant for signed longs and is derived from the
3902
+ // closure library as a whole.
3903
+ if (this.eq(MIN_VALUE)) {
3904
+ if (divisor.eq(ONE) || divisor.eq(NEG_ONE))
3905
+ return MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE
3906
+ else if (divisor.eq(MIN_VALUE))
3907
+ return ONE;
3908
+ else {
3909
+ // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
3910
+ var halfThis = this.shr(1);
3911
+ approx = halfThis.div(divisor).shl(1);
3912
+ if (approx.eq(ZERO)) {
3913
+ return divisor.isNegative() ? ONE : NEG_ONE;
3914
+ } else {
3915
+ rem = this.sub(divisor.mul(approx));
3916
+ res = approx.add(rem.div(divisor));
3917
+ return res;
3918
+ }
3919
+ }
3920
+ } else if (divisor.eq(MIN_VALUE))
3921
+ return this.unsigned ? UZERO : ZERO;
3922
+ if (this.isNegative()) {
3923
+ if (divisor.isNegative())
3924
+ return this.neg().div(divisor.neg());
3925
+ return this.neg().div(divisor).neg();
3926
+ } else if (divisor.isNegative())
3927
+ return this.div(divisor.neg()).neg();
3928
+ res = ZERO;
3929
+ } else {
3930
+ // The algorithm below has not been made for unsigned longs. It's therefore
3931
+ // required to take special care of the MSB prior to running it.
3932
+ if (!divisor.unsigned)
3933
+ divisor = divisor.toUnsigned();
3934
+ if (divisor.gt(this))
3935
+ return UZERO;
3936
+ if (divisor.gt(this.shru(1))) // 15 >>> 1 = 7 ; with divisor = 8 ; true
3937
+ return UONE;
3938
+ res = UZERO;
3939
+ }
3940
+
3941
+ // Repeat the following until the remainder is less than other: find a
3942
+ // floating-point that approximates remainder / other *from below*, add this
3943
+ // into the result, and subtract it from the remainder. It is critical that
3944
+ // the approximate value is less than or equal to the real value so that the
3945
+ // remainder never becomes negative.
3946
+ rem = this;
3947
+ while (rem.gte(divisor)) {
3948
+ // Approximate the result of division. This may be a little greater or
3949
+ // smaller than the actual value.
3950
+ approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber()));
3951
+
3952
+ // We will tweak the approximate result by changing it in the 48-th digit or
3953
+ // the smallest non-fractional digit, whichever is larger.
3954
+ var log2 = Math.ceil(Math.log(approx) / Math.LN2),
3955
+ delta = (log2 <= 48) ? 1 : pow_dbl(2, log2 - 48),
3956
+
3957
+ // Decrease the approximation until it is smaller than the remainder. Note
3958
+ // that if it is too large, the product overflows and is negative.
3959
+ approxRes = fromNumber(approx),
3960
+ approxRem = approxRes.mul(divisor);
3961
+ while (approxRem.isNegative() || approxRem.gt(rem)) {
3962
+ approx -= delta;
3963
+ approxRes = fromNumber(approx, this.unsigned);
3964
+ approxRem = approxRes.mul(divisor);
3965
+ }
3966
+
3967
+ // We know the answer can't be zero... and actually, zero would cause
3968
+ // infinite recursion since we would make no progress.
3969
+ if (approxRes.isZero())
3970
+ approxRes = ONE;
3971
+
3972
+ res = res.add(approxRes);
3973
+ rem = rem.sub(approxRem);
3974
+ }
3975
+ return res;
3976
+ };
3977
+
3978
+ /**
3979
+ * Returns this Long divided by the specified. This is an alias of {@link Long#divide}.
3980
+ * @function
3981
+ * @param {!Long|number|string} divisor Divisor
3982
+ * @returns {!Long} Quotient
3983
+ */
3984
+ LongPrototype.div = LongPrototype.divide;
3985
+
3986
+ /**
3987
+ * Returns this Long modulo the specified.
3988
+ * @param {!Long|number|string} divisor Divisor
3989
+ * @returns {!Long} Remainder
3990
+ */
3991
+ LongPrototype.modulo = function modulo(divisor) {
3992
+ if (!isLong(divisor))
3993
+ divisor = fromValue(divisor);
3994
+
3995
+ // use wasm support if present
3996
+ if (wasm) {
3997
+ var low = (this.unsigned ? wasm.rem_u : wasm.rem_s)(
3998
+ this.low,
3999
+ this.high,
4000
+ divisor.low,
4001
+ divisor.high
4002
+ );
4003
+ return fromBits(low, wasm.get_high(), this.unsigned);
4004
+ }
4005
+
4006
+ return this.sub(this.div(divisor).mul(divisor));
4007
+ };
4008
+
4009
+ /**
4010
+ * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}.
4011
+ * @function
4012
+ * @param {!Long|number|string} divisor Divisor
4013
+ * @returns {!Long} Remainder
4014
+ */
4015
+ LongPrototype.mod = LongPrototype.modulo;
4016
+
4017
+ /**
4018
+ * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}.
4019
+ * @function
4020
+ * @param {!Long|number|string} divisor Divisor
4021
+ * @returns {!Long} Remainder
4022
+ */
4023
+ LongPrototype.rem = LongPrototype.modulo;
4024
+
4025
+ /**
4026
+ * Returns the bitwise NOT of this Long.
4027
+ * @returns {!Long}
4028
+ */
4029
+ LongPrototype.not = function not() {
4030
+ return fromBits(~this.low, ~this.high, this.unsigned);
4031
+ };
4032
+
4033
+ /**
4034
+ * Returns the bitwise AND of this Long and the specified.
4035
+ * @param {!Long|number|string} other Other Long
4036
+ * @returns {!Long}
4037
+ */
4038
+ LongPrototype.and = function and(other) {
4039
+ if (!isLong(other))
4040
+ other = fromValue(other);
4041
+ return fromBits(this.low & other.low, this.high & other.high, this.unsigned);
4042
+ };
4043
+
4044
+ /**
4045
+ * Returns the bitwise OR of this Long and the specified.
4046
+ * @param {!Long|number|string} other Other Long
4047
+ * @returns {!Long}
4048
+ */
4049
+ LongPrototype.or = function or(other) {
4050
+ if (!isLong(other))
4051
+ other = fromValue(other);
4052
+ return fromBits(this.low | other.low, this.high | other.high, this.unsigned);
4053
+ };
4054
+
4055
+ /**
4056
+ * Returns the bitwise XOR of this Long and the given one.
4057
+ * @param {!Long|number|string} other Other Long
4058
+ * @returns {!Long}
4059
+ */
4060
+ LongPrototype.xor = function xor(other) {
4061
+ if (!isLong(other))
4062
+ other = fromValue(other);
4063
+ return fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned);
4064
+ };
4065
+
4066
+ /**
4067
+ * Returns this Long with bits shifted to the left by the given amount.
4068
+ * @param {number|!Long} numBits Number of bits
4069
+ * @returns {!Long} Shifted Long
4070
+ */
4071
+ LongPrototype.shiftLeft = function shiftLeft(numBits) {
4072
+ if (isLong(numBits))
4073
+ numBits = numBits.toInt();
4074
+ if ((numBits &= 63) === 0)
4075
+ return this;
4076
+ else if (numBits < 32)
4077
+ return fromBits(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned);
4078
+ else
4079
+ return fromBits(0, this.low << (numBits - 32), this.unsigned);
4080
+ };
4081
+
4082
+ /**
4083
+ * Returns this Long with bits shifted to the left by the given amount. This is an alias of {@link Long#shiftLeft}.
4084
+ * @function
4085
+ * @param {number|!Long} numBits Number of bits
4086
+ * @returns {!Long} Shifted Long
4087
+ */
4088
+ LongPrototype.shl = LongPrototype.shiftLeft;
4089
+
4090
+ /**
4091
+ * Returns this Long with bits arithmetically shifted to the right by the given amount.
4092
+ * @param {number|!Long} numBits Number of bits
4093
+ * @returns {!Long} Shifted Long
4094
+ */
4095
+ LongPrototype.shiftRight = function shiftRight(numBits) {
4096
+ if (isLong(numBits))
4097
+ numBits = numBits.toInt();
4098
+ if ((numBits &= 63) === 0)
4099
+ return this;
4100
+ else if (numBits < 32)
4101
+ return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned);
4102
+ else
4103
+ return fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned);
4104
+ };
4105
+
4106
+ /**
4107
+ * Returns this Long with bits arithmetically shifted to the right by the given amount. This is an alias of {@link Long#shiftRight}.
4108
+ * @function
4109
+ * @param {number|!Long} numBits Number of bits
4110
+ * @returns {!Long} Shifted Long
4111
+ */
4112
+ LongPrototype.shr = LongPrototype.shiftRight;
4113
+
4114
+ /**
4115
+ * Returns this Long with bits logically shifted to the right by the given amount.
4116
+ * @param {number|!Long} numBits Number of bits
4117
+ * @returns {!Long} Shifted Long
4118
+ */
4119
+ LongPrototype.shiftRightUnsigned = function shiftRightUnsigned(numBits) {
4120
+ if (isLong(numBits))
4121
+ numBits = numBits.toInt();
4122
+ numBits &= 63;
4123
+ if (numBits === 0)
4124
+ return this;
4125
+ else {
4126
+ var high = this.high;
4127
+ if (numBits < 32) {
4128
+ var low = this.low;
4129
+ return fromBits((low >>> numBits) | (high << (32 - numBits)), high >>> numBits, this.unsigned);
4130
+ } else if (numBits === 32)
4131
+ return fromBits(high, 0, this.unsigned);
4132
+ else
4133
+ return fromBits(high >>> (numBits - 32), 0, this.unsigned);
4134
+ }
4135
+ };
4136
+
4137
+ /**
4138
+ * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}.
4139
+ * @function
4140
+ * @param {number|!Long} numBits Number of bits
4141
+ * @returns {!Long} Shifted Long
4142
+ */
4143
+ LongPrototype.shru = LongPrototype.shiftRightUnsigned;
4144
+
4145
+ /**
4146
+ * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}.
4147
+ * @function
4148
+ * @param {number|!Long} numBits Number of bits
4149
+ * @returns {!Long} Shifted Long
4150
+ */
4151
+ LongPrototype.shr_u = LongPrototype.shiftRightUnsigned;
4152
+
4153
+ /**
4154
+ * Converts this Long to signed.
4155
+ * @returns {!Long} Signed long
4156
+ */
4157
+ LongPrototype.toSigned = function toSigned() {
4158
+ if (!this.unsigned)
4159
+ return this;
4160
+ return fromBits(this.low, this.high, false);
4161
+ };
4162
+
4163
+ /**
4164
+ * Converts this Long to unsigned.
4165
+ * @returns {!Long} Unsigned long
4166
+ */
4167
+ LongPrototype.toUnsigned = function toUnsigned() {
4168
+ if (this.unsigned)
4169
+ return this;
4170
+ return fromBits(this.low, this.high, true);
4171
+ };
4172
+
4173
+ /**
4174
+ * Converts this Long to its byte representation.
4175
+ * @param {boolean=} le Whether little or big endian, defaults to big endian
4176
+ * @returns {!Array.<number>} Byte representation
4177
+ */
4178
+ LongPrototype.toBytes = function toBytes(le) {
4179
+ return le ? this.toBytesLE() : this.toBytesBE();
4180
+ };
4181
+
4182
+ /**
4183
+ * Converts this Long to its little endian byte representation.
4184
+ * @returns {!Array.<number>} Little endian byte representation
4185
+ */
4186
+ LongPrototype.toBytesLE = function toBytesLE() {
4187
+ var hi = this.high,
4188
+ lo = this.low;
4189
+ return [
4190
+ lo & 0xff,
4191
+ lo >>> 8 & 0xff,
4192
+ lo >>> 16 & 0xff,
4193
+ lo >>> 24 ,
4194
+ hi & 0xff,
4195
+ hi >>> 8 & 0xff,
4196
+ hi >>> 16 & 0xff,
4197
+ hi >>> 24
4198
+ ];
4199
+ };
4200
+
4201
+ /**
4202
+ * Converts this Long to its big endian byte representation.
4203
+ * @returns {!Array.<number>} Big endian byte representation
4204
+ */
4205
+ LongPrototype.toBytesBE = function toBytesBE() {
4206
+ var hi = this.high,
4207
+ lo = this.low;
4208
+ return [
4209
+ hi >>> 24 ,
4210
+ hi >>> 16 & 0xff,
4211
+ hi >>> 8 & 0xff,
4212
+ hi & 0xff,
4213
+ lo >>> 24 ,
4214
+ lo >>> 16 & 0xff,
4215
+ lo >>> 8 & 0xff,
4216
+ lo & 0xff
4217
+ ];
4218
+ };
4219
+
4220
+ /**
4221
+ * Creates a Long from its byte representation.
4222
+ * @param {!Array.<number>} bytes Byte representation
4223
+ * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
4224
+ * @param {boolean=} le Whether little or big endian, defaults to big endian
4225
+ * @returns {Long} The corresponding Long value
4226
+ */
4227
+ Long.fromBytes = function fromBytes(bytes, unsigned, le) {
4228
+ return le ? Long.fromBytesLE(bytes, unsigned) : Long.fromBytesBE(bytes, unsigned);
4229
+ };
4230
+
4231
+ /**
4232
+ * Creates a Long from its little endian byte representation.
4233
+ * @param {!Array.<number>} bytes Little endian byte representation
4234
+ * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
4235
+ * @returns {Long} The corresponding Long value
4236
+ */
4237
+ Long.fromBytesLE = function fromBytesLE(bytes, unsigned) {
4238
+ return new Long(
4239
+ bytes[0] |
4240
+ bytes[1] << 8 |
4241
+ bytes[2] << 16 |
4242
+ bytes[3] << 24,
4243
+ bytes[4] |
4244
+ bytes[5] << 8 |
4245
+ bytes[6] << 16 |
4246
+ bytes[7] << 24,
4247
+ unsigned
4248
+ );
4249
+ };
4250
+
4251
+ /**
4252
+ * Creates a Long from its big endian byte representation.
4253
+ * @param {!Array.<number>} bytes Big endian byte representation
4254
+ * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
4255
+ * @returns {Long} The corresponding Long value
4256
+ */
4257
+ Long.fromBytesBE = function fromBytesBE(bytes, unsigned) {
4258
+ return new Long(
4259
+ bytes[4] << 24 |
4260
+ bytes[5] << 16 |
4261
+ bytes[6] << 8 |
4262
+ bytes[7],
4263
+ bytes[0] << 24 |
4264
+ bytes[1] << 16 |
4265
+ bytes[2] << 8 |
4266
+ bytes[3],
4267
+ unsigned
4268
+ );
4269
+ };
4270
+
47
4271
  var aspromise = asPromise;
48
4272
 
49
4273
  /**
@@ -5384,8 +9608,9 @@ $root.cosmos = (function() {
5384
9608
  var MsgCompiled = $root;
5385
9609
 
5386
9610
  const DECIMAL = 8;
5387
- const DEFAULT_GAS_VALUE = '4000000';
5388
- const DEPOSIT_GAS_VALUE = '500000000';
9611
+ const DEFAULT_GAS_ADJUSTMENT = 2;
9612
+ const DEFAULT_GAS_LIMIT_VALUE = '4000000';
9613
+ const DEPOSIT_GAS_LIMIT_VALUE = '600000000';
5389
9614
  const MAX_TX_COUNT = 100;
5390
9615
  /**
5391
9616
  * Checks whether an asset is `AssetRuneNative`
@@ -5563,11 +9788,36 @@ const buildUnsignedTx = ({ cosmosSdk, txBody, signerPubkey, sequence, gasLimit,
5563
9788
  ],
5564
9789
  fee: {
5565
9790
  amount: null,
5566
- gas_limit: core.cosmosclient.Long.fromString(gasLimit),
9791
+ gas_limit: gasLimit || null,
5567
9792
  },
5568
9793
  });
5569
9794
  return new core.cosmosclient.TxBuilder(cosmosSdk, txBody, authInfo);
5570
9795
  };
9796
+ /**
9797
+ * Estimates usage of gas
9798
+ *
9799
+ * Note: Be careful by using this helper function,
9800
+ * it's still experimental and result might be incorrect.
9801
+ * Change `multiplier` to get a valid estimation of gas.
9802
+ */
9803
+ const getEstimatedGas = ({ cosmosSDKClient, txBody, privKey, accountNumber, accountSequence, multiplier, }) => __awaiter(void 0, void 0, void 0, function* () {
9804
+ var _b, _c, _d;
9805
+ const pubKey = privKey.pubKey();
9806
+ const txBuilder = buildUnsignedTx({
9807
+ cosmosSdk: cosmosSDKClient.sdk,
9808
+ txBody: txBody,
9809
+ signerPubkey: core.cosmosclient.codec.instanceToProtoAny(pubKey),
9810
+ sequence: accountSequence,
9811
+ });
9812
+ const signDocBytes = txBuilder.signDocBytes(accountNumber);
9813
+ txBuilder.addSignature(privKey.sign(signDocBytes));
9814
+ const resp = yield core.rest.tx.simulate(cosmosSDKClient.sdk, { tx_bytes: txBuilder.txBytes() });
9815
+ const estimatedGas = (_d = (_c = (_b = resp.data) === null || _b === void 0 ? void 0 : _b.gas_info) === null || _c === void 0 ? void 0 : _c.gas_used) !== null && _d !== void 0 ? _d : null;
9816
+ if (!estimatedGas) {
9817
+ throw new Error('Could not get data of estimated gas');
9818
+ }
9819
+ return long_1.fromString(estimatedGas).multiply(multiplier || DEFAULT_GAS_ADJUSTMENT);
9820
+ });
5571
9821
  /**
5572
9822
  * Structure a MsgDeposit
5573
9823
  *
@@ -5593,7 +9843,7 @@ const buildDepositTx = ({ msgNativeTx, nodeUrl, chainId, }) => __awaiter(void 0,
5593
9843
  };
5594
9844
  const depositMsg = MsgCompiled.types.MsgDeposit.fromObject(msgDepositObj);
5595
9845
  return new core.proto.cosmos.tx.v1beta1.TxBody({
5596
- messages: [core.cosmosclient.codec.packAny(depositMsg)],
9846
+ messages: [core.cosmosclient.codec.instanceToProtoAny(depositMsg)],
5597
9847
  memo: msgNativeTx.memo,
5598
9848
  });
5599
9849
  });
@@ -5627,7 +9877,7 @@ const buildTransferTx = ({ fromAddress, toAddress, assetAmount, assetDenom, memo
5627
9877
  };
5628
9878
  const transferMsg = MsgCompiled.types.MsgSend.fromObject(transferObj);
5629
9879
  return new core.proto.cosmos.tx.v1beta1.TxBody({
5630
- messages: [core.cosmosclient.codec.packAny(transferMsg)],
9880
+ messages: [core.cosmosclient.codec.instanceToProtoAny(transferMsg)],
5631
9881
  memo,
5632
9882
  });
5633
9883
  });
@@ -5839,8 +10089,15 @@ class Client extends xchainClient.BaseXChainClient {
5839
10089
  * Thrown if network has not been set before.
5840
10090
  */
5841
10091
  setNetwork(network) {
10092
+ // dirty check to avoid using and re-creation of same data
10093
+ if (network === this.network)
10094
+ return;
5842
10095
  super.setNetwork(network);
5843
- this.cosmosClient.updatePrefix(getPrefix(this.network));
10096
+ this.cosmosClient = new xchainCosmos.CosmosSDKClient({
10097
+ server: this.getClientUrl().node,
10098
+ chainId: this.getChainId(network),
10099
+ prefix: getPrefix(network),
10100
+ });
5844
10101
  }
5845
10102
  /**
5846
10103
  * Set/update the client URL.
@@ -6049,9 +10306,9 @@ class Client extends xchainClient.BaseXChainClient {
6049
10306
  * @returns {TxHash} The transaction hash.
6050
10307
  *
6051
10308
  * @throws {"insufficient funds"} Thrown if the wallet has insufficient funds.
6052
- * @throws {"failed to broadcast transaction"} Thrown if failed to broadcast transaction.
10309
+ * @throws {"Invalid transaction hash"} Thrown by missing tx hash
6053
10310
  */
6054
- deposit({ walletIndex = 0, asset = xchainUtil.AssetRuneNative, amount, memo }) {
10311
+ deposit({ walletIndex = 0, asset = xchainUtil.AssetRuneNative, amount, memo, gasLimit = new bignumber(DEPOSIT_GAS_LIMIT_VALUE), }) {
6055
10312
  var _a, _b, _c, _d;
6056
10313
  return __awaiter(this, void 0, void 0, function* () {
6057
10314
  const balances = yield this.getBalance(this.getAddress(walletIndex));
@@ -6092,11 +10349,14 @@ class Client extends xchainClient.BaseXChainClient {
6092
10349
  const txBuilder = buildUnsignedTx({
6093
10350
  cosmosSdk: this.getCosmosClient().sdk,
6094
10351
  txBody: depositTxBody,
6095
- signerPubkey: core.cosmosclient.codec.packAny(signerPubkey),
6096
- gasLimit: DEPOSIT_GAS_VALUE,
6097
- sequence: account.sequence || core.cosmosclient.Long.ZERO,
10352
+ signerPubkey: core.cosmosclient.codec.instanceToProtoAny(signerPubkey),
10353
+ gasLimit: long_1.fromString(gasLimit.toFixed(0)),
10354
+ sequence: account.sequence || long_1.ZERO,
6098
10355
  });
6099
- return (yield this.getCosmosClient().signAndBroadcast(txBuilder, privKey, account)) || '';
10356
+ const txHash = yield this.getCosmosClient().signAndBroadcast(txBuilder, privKey, account);
10357
+ if (!txHash)
10358
+ throw Error(`Invalid transaction hash: ${txHash}`);
10359
+ return txHash;
6100
10360
  });
6101
10361
  }
6102
10362
  /**
@@ -6104,8 +10364,11 @@ class Client extends xchainClient.BaseXChainClient {
6104
10364
  *
6105
10365
  * @param {TxParams} params The transfer options.
6106
10366
  * @returns {TxHash} The transaction hash.
10367
+ *
10368
+ * @throws {"insufficient funds"} Thrown if the wallet has insufficient funds.
10369
+ * @throws {"Invalid transaction hash"} Thrown by missing tx hash
6107
10370
  */
6108
- transfer({ walletIndex = 0, asset = xchainUtil.AssetRuneNative, amount, recipient, memo }) {
10371
+ transfer({ walletIndex = 0, asset = xchainUtil.AssetRuneNative, amount, recipient, memo, gasLimit = new bignumber(DEFAULT_GAS_LIMIT_VALUE), }) {
6109
10372
  var _a, _b, _c, _d;
6110
10373
  return __awaiter(this, void 0, void 0, function* () {
6111
10374
  const balances = yield this.getBalance(this.getAddress(walletIndex));
@@ -6139,14 +10402,18 @@ class Client extends xchainClient.BaseXChainClient {
6139
10402
  nodeUrl: this.getClientUrl().node,
6140
10403
  });
6141
10404
  const account = yield this.getCosmosClient().getAccount(accAddress);
10405
+ const accountSequence = account.sequence || long_1.ZERO;
6142
10406
  const txBuilder = buildUnsignedTx({
6143
10407
  cosmosSdk: this.getCosmosClient().sdk,
6144
10408
  txBody: txBody,
6145
- gasLimit: DEFAULT_GAS_VALUE,
6146
- signerPubkey: core.cosmosclient.codec.packAny(signerPubkey),
6147
- sequence: account.sequence || core.cosmosclient.Long.ZERO,
10409
+ gasLimit: long_1.fromString(gasLimit.toString()),
10410
+ signerPubkey: core.cosmosclient.codec.instanceToProtoAny(signerPubkey),
10411
+ sequence: accountSequence,
6148
10412
  });
6149
- return (yield this.cosmosClient.signAndBroadcast(txBuilder, privKey, account)) || '';
10413
+ const txHash = yield this.cosmosClient.signAndBroadcast(txBuilder, privKey, account);
10414
+ if (!txHash)
10415
+ throw Error(`Invalid transaction hash: ${txHash}`);
10416
+ return txHash;
6150
10417
  });
6151
10418
  }
6152
10419
  /**
@@ -6155,7 +10422,7 @@ class Client extends xchainClient.BaseXChainClient {
6155
10422
  * @param {TxOfflineParams} params The transfer offline options.
6156
10423
  * @returns {string} The signed transaction bytes.
6157
10424
  */
6158
- transferOffline({ walletIndex = 0, asset = xchainUtil.AssetRuneNative, amount, recipient, memo, from_rune_balance, from_asset_balance = xchainUtil.baseAmount(0, DECIMAL), from_account_number = '0', from_sequence = '0', }) {
10425
+ transferOffline({ walletIndex = 0, asset = xchainUtil.AssetRuneNative, amount, recipient, memo, fromRuneBalance: from_rune_balance, fromAssetBalance: from_asset_balance = xchainUtil.baseAmount(0, DECIMAL), fromAccountNumber = long_1.ZERO, fromSequence = long_1.ZERO, gasLimit = new bignumber(DEFAULT_GAS_LIMIT_VALUE), }) {
6159
10426
  return __awaiter(this, void 0, void 0, function* () {
6160
10427
  const fee = (yield this.getFees()).average;
6161
10428
  if (isAssetRuneNative(asset)) {
@@ -6183,11 +10450,11 @@ class Client extends xchainClient.BaseXChainClient {
6183
10450
  const txBuilder = buildUnsignedTx({
6184
10451
  cosmosSdk: this.getCosmosClient().sdk,
6185
10452
  txBody: txBody,
6186
- gasLimit: DEFAULT_GAS_VALUE,
6187
- signerPubkey: core.cosmosclient.codec.packAny(privKey.pubKey()),
6188
- sequence: core.cosmosclient.Long.fromString(from_sequence) || core.cosmosclient.Long.ZERO,
10453
+ gasLimit: long_1.fromString(gasLimit.toFixed(0)),
10454
+ signerPubkey: core.cosmosclient.codec.instanceToProtoAny(privKey.pubKey()),
10455
+ sequence: fromSequence,
6189
10456
  });
6190
- const signDocBytes = txBuilder.signDocBytes(core.cosmosclient.Long.fromString(from_account_number));
10457
+ const signDocBytes = txBuilder.signDocBytes(fromAccountNumber);
6191
10458
  txBuilder.addSignature(privKey.sign(signDocBytes));
6192
10459
  return txBuilder.txBytes();
6193
10460
  });
@@ -6232,8 +10499,9 @@ const msgNativeTxFromJson = (value) => {
6232
10499
 
6233
10500
  exports.Client = Client;
6234
10501
  exports.DECIMAL = DECIMAL;
6235
- exports.DEFAULT_GAS_VALUE = DEFAULT_GAS_VALUE;
6236
- exports.DEPOSIT_GAS_VALUE = DEPOSIT_GAS_VALUE;
10502
+ exports.DEFAULT_GAS_ADJUSTMENT = DEFAULT_GAS_ADJUSTMENT;
10503
+ exports.DEFAULT_GAS_LIMIT_VALUE = DEFAULT_GAS_LIMIT_VALUE;
10504
+ exports.DEPOSIT_GAS_LIMIT_VALUE = DEPOSIT_GAS_LIMIT_VALUE;
6237
10505
  exports.MAX_TX_COUNT = MAX_TX_COUNT;
6238
10506
  exports.MsgNativeTx = MsgNativeTx;
6239
10507
  exports.assetFromDenom = assetFromDenom;
@@ -6248,6 +10516,7 @@ exports.getDefaultExplorerUrls = getDefaultExplorerUrls;
6248
10516
  exports.getDefaultFees = getDefaultFees;
6249
10517
  exports.getDenom = getDenom;
6250
10518
  exports.getDepositTxDataFromLogs = getDepositTxDataFromLogs;
10519
+ exports.getEstimatedGas = getEstimatedGas;
6251
10520
  exports.getExplorerAddressUrl = getExplorerAddressUrl;
6252
10521
  exports.getExplorerTxUrl = getExplorerTxUrl;
6253
10522
  exports.getExplorerUrl = getExplorerUrl;