orion-design 0.1.0

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