@universetech-inc/unbig.js 6.2.2

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.
Files changed (5) hide show
  1. package/LICENCE.md +26 -0
  2. package/README.md +207 -0
  3. package/big.js +1043 -0
  4. package/big.mjs +1027 -0
  5. package/package.json +64 -0
package/big.mjs ADDED
@@ -0,0 +1,1027 @@
1
+ /*
2
+ * big.js v6.2.1
3
+ * A small, fast, easy-to-use library for arbitrary-precision decimal arithmetic.
4
+ * Copyright (c) 2022 Michael Mclaughlin
5
+ * https://github.com/MikeMcl/big.js/LICENCE.md
6
+ */
7
+
8
+
9
+ /************************************** EDITABLE DEFAULTS *****************************************/
10
+
11
+
12
+ // The default values below must be integers within the stated ranges.
13
+
14
+ /*
15
+ * The maximum number of decimal places (DP) of the results of operations involving division:
16
+ * div and sqrt, and pow with negative exponents.
17
+ */
18
+ var DP = 20, // 0 to MAX_DP
19
+
20
+ /*
21
+ * The rounding mode (RM) used when rounding to the above decimal places.
22
+ *
23
+ * 0 Towards zero (i.e. truncate, no rounding). (ROUND_DOWN)
24
+ * 1 To nearest neighbour. If equidistant, round up. (ROUND_HALF_UP)
25
+ * 2 To nearest neighbour. If equidistant, to even. (ROUND_HALF_EVEN)
26
+ * 3 Away from zero. (ROUND_UP)
27
+ */
28
+ RM = 1, // 0, 1, 2 or 3
29
+
30
+ // The maximum value of DP and Big.DP.
31
+ MAX_DP = 1E6, // 0 to 1000000
32
+
33
+ // The maximum magnitude of the exponent argument to the pow method.
34
+ MAX_POWER = 1E6, // 1 to 1000000
35
+
36
+ /*
37
+ * The negative exponent (NE) at and beneath which toString returns exponential notation.
38
+ * (JavaScript numbers: -7)
39
+ * -1000000 is the minimum recommended exponent value of a Big.
40
+ */
41
+ NE = -7, // 0 to -1000000
42
+
43
+ /*
44
+ * The positive exponent (PE) at and above which toString returns exponential notation.
45
+ * (JavaScript numbers: 21)
46
+ * 1000000 is the maximum recommended exponent value of a Big, but this limit is not enforced.
47
+ */
48
+ PE = 21, // 0 to 1000000
49
+
50
+ /*
51
+ * When true, an error will be thrown if a primitive number is passed to the Big constructor,
52
+ * or if valueOf is called, or if toNumber is called on a Big which cannot be converted to a
53
+ * primitive number without a loss of precision.
54
+ */
55
+ STRICT = false, // true or false
56
+
57
+
58
+ /**************************************************************************************************/
59
+
60
+
61
+ // Error messages.
62
+ NAME = '[big.js] ',
63
+ INVALID = NAME + 'Invalid ',
64
+ INVALID_DP = INVALID + 'decimal places',
65
+ INVALID_RM = INVALID + 'rounding mode',
66
+ DIV_BY_ZERO = NAME + 'Division by zero',
67
+
68
+ // The shared prototype object.
69
+ P = {},
70
+ UNDEFINED = void 0,
71
+ NUMERIC = /^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;
72
+
73
+
74
+ /*
75
+ * Create and return a Big constructor.
76
+ */
77
+ function _Big_() {
78
+
79
+ /*
80
+ * The Big constructor and exported function.
81
+ * Create and return a new instance of a Big number object.
82
+ *
83
+ * n {number|string|Big} A numeric value.
84
+ */
85
+ function Big(n) {
86
+ var x = this;
87
+
88
+ // Enable constructor usage without new.
89
+ if (!(x instanceof Big)) return n === UNDEFINED ? _Big_() : new Big(n);
90
+
91
+ // Duplicate.
92
+ if (n instanceof Big) {
93
+ x.s = n.s;
94
+ x.e = n.e;
95
+ x.c = n.c.slice();
96
+ } else {
97
+ if (typeof n !== 'string') {
98
+ if (Big.strict === true && typeof n !== 'bigint') {
99
+ throw TypeError(INVALID + 'value');
100
+ }
101
+
102
+ // Minus zero?
103
+ n = n === 0 && 1 / n < 0 ? '-0' : String(n);
104
+ }
105
+
106
+ parse(x, n);
107
+ }
108
+
109
+ // Retain a reference to this Big constructor.
110
+ // Shadow Big.prototype.constructor which points to Object.
111
+ x.constructor = Big;
112
+ }
113
+
114
+ Big.prototype = P;
115
+ Big.DP = DP;
116
+ Big.RM = RM;
117
+ Big.NE = NE;
118
+ Big.PE = PE;
119
+ Big.strict = STRICT;
120
+ Big.roundDown = 0;
121
+ Big.roundHalfUp = 1;
122
+ Big.roundHalfEven = 2;
123
+ Big.roundUp = 3;
124
+
125
+ return Big;
126
+ }
127
+
128
+
129
+ /*
130
+ * Parse the number or string value passed to a Big constructor.
131
+ *
132
+ * x {Big} A Big number instance.
133
+ * n {number|string} A numeric value.
134
+ */
135
+ function parse(x, n) {
136
+ var e, i, nl;
137
+
138
+ if (!NUMERIC.test(n)) {
139
+ throw Error(INVALID + 'number');
140
+ }
141
+
142
+ // Determine sign.
143
+ x.s = n.charAt(0) == '-' ? (n = n.slice(1), -1) : 1;
144
+
145
+ // Decimal point?
146
+ if ((e = n.indexOf('.')) > -1) n = n.replace('.', '');
147
+
148
+ // Exponential form?
149
+ if ((i = n.search(/e/i)) > 0) {
150
+
151
+ // Determine exponent.
152
+ if (e < 0) e = i;
153
+ e += +n.slice(i + 1);
154
+ n = n.substring(0, i);
155
+ } else if (e < 0) {
156
+
157
+ // Integer.
158
+ e = n.length;
159
+ }
160
+
161
+ nl = n.length;
162
+
163
+ // Determine leading zeros.
164
+ for (i = 0; i < nl && n.charAt(i) == '0';) ++i;
165
+
166
+ if (i == nl) {
167
+
168
+ // Zero.
169
+ x.c = [x.e = 0];
170
+ } else {
171
+
172
+ // Determine trailing zeros.
173
+ for (; nl > 0 && n.charAt(--nl) == '0';);
174
+ x.e = e - i - 1;
175
+ x.c = [];
176
+
177
+ // Convert string to array of digits without leading/trailing zeros.
178
+ for (e = 0; i <= nl;) x.c[e++] = +n.charAt(i++);
179
+ }
180
+
181
+ return x;
182
+ }
183
+
184
+
185
+ /*
186
+ * Round Big x to a maximum of sd significant digits using rounding mode rm.
187
+ *
188
+ * x {Big} The Big to round.
189
+ * sd {number} Significant digits: integer, 0 to MAX_DP inclusive.
190
+ * rm {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up).
191
+ * [more] {boolean} Whether the result of division was truncated.
192
+ */
193
+ function round(x, sd, rm, more) {
194
+ var xc = x.c;
195
+
196
+ if (rm === UNDEFINED) rm = x.constructor.RM;
197
+ if (rm !== 0 && rm !== 1 && rm !== 2 && rm !== 3) {
198
+ throw Error(INVALID_RM);
199
+ }
200
+
201
+ if (sd < 1) {
202
+ more =
203
+ rm === 3 && (more || !!xc[0]) || sd === 0 && (
204
+ rm === 1 && xc[0] >= 5 ||
205
+ rm === 2 && (xc[0] > 5 || xc[0] === 5 && (more || xc[1] !== UNDEFINED))
206
+ );
207
+
208
+ xc.length = 1;
209
+
210
+ if (more) {
211
+
212
+ // 1, 0.1, 0.01, 0.001, 0.0001 etc.
213
+ x.e = x.e - sd + 1;
214
+ xc[0] = 1;
215
+ } else {
216
+
217
+ // Zero.
218
+ xc[0] = x.e = 0;
219
+ }
220
+ } else if (sd < xc.length) {
221
+
222
+ // xc[sd] is the digit after the digit that may be rounded up.
223
+ more =
224
+ rm === 1 && xc[sd] >= 5 ||
225
+ rm === 2 && (xc[sd] > 5 || xc[sd] === 5 &&
226
+ (more || xc[sd + 1] !== UNDEFINED || xc[sd - 1] & 1)) ||
227
+ rm === 3 && (more || !!xc[0]);
228
+
229
+ // Remove any digits after the required precision.
230
+ xc.length = sd;
231
+
232
+ // Round up?
233
+ if (more) {
234
+
235
+ // Rounding up may mean the previous digit has to be rounded up.
236
+ for (; ++xc[--sd] > 9;) {
237
+ xc[sd] = 0;
238
+ if (sd === 0) {
239
+ ++x.e;
240
+ xc.unshift(1);
241
+ break;
242
+ }
243
+ }
244
+ }
245
+
246
+ // Remove trailing zeros.
247
+ for (sd = xc.length; !xc[--sd];) xc.pop();
248
+ }
249
+
250
+ return x;
251
+ }
252
+
253
+
254
+ /*
255
+ * Return a string representing the value of Big x in normal or exponential notation.
256
+ * Handles P.toExponential, P.toFixed, P.toJSON, P.toPrecision, P.toString and P.valueOf.
257
+ */
258
+ function stringify(x, doExponential, isNonzero) {
259
+ var e = x.e,
260
+ s = x.c.join(''),
261
+ n = s.length;
262
+
263
+ // Exponential notation?
264
+ if (doExponential) {
265
+ s = s.charAt(0) + (n > 1 ? '.' + s.slice(1) : '') + (e < 0 ? 'e' : 'e+') + e;
266
+
267
+ // Normal notation.
268
+ } else if (e < 0) {
269
+ for (; ++e;) s = '0' + s;
270
+ s = '0.' + s;
271
+ } else if (e > 0) {
272
+ if (++e > n) {
273
+ for (e -= n; e--;) s += '0';
274
+ } else if (e < n) {
275
+ s = s.slice(0, e) + '.' + s.slice(e);
276
+ }
277
+ } else if (n > 1) {
278
+ s = s.charAt(0) + '.' + s.slice(1);
279
+ }
280
+
281
+ return x.s < 0 && isNonzero ? '-' + s : s;
282
+ }
283
+
284
+
285
+ // Prototype/instance methods
286
+
287
+
288
+ /*
289
+ * Return a new Big whose value is the absolute value of this Big.
290
+ */
291
+ P.abs = function () {
292
+ var x = new this.constructor(this);
293
+ x.s = 1;
294
+ return x;
295
+ };
296
+
297
+
298
+ /*
299
+ * Return 1 if the value of this Big is greater than the value of Big y,
300
+ * -1 if the value of this Big is less than the value of Big y, or
301
+ * 0 if they have the same value.
302
+ */
303
+ P.cmp = function (y) {
304
+ var isneg,
305
+ x = this,
306
+ xc = x.c,
307
+ yc = (y = new x.constructor(y)).c,
308
+ i = x.s,
309
+ j = y.s,
310
+ k = x.e,
311
+ l = y.e;
312
+
313
+ // Either zero?
314
+ if (!xc[0] || !yc[0]) return !xc[0] ? !yc[0] ? 0 : -j : i;
315
+
316
+ // Signs differ?
317
+ if (i != j) return i;
318
+
319
+ isneg = i < 0;
320
+
321
+ // Compare exponents.
322
+ if (k != l) return k > l ^ isneg ? 1 : -1;
323
+
324
+ j = (k = xc.length) < (l = yc.length) ? k : l;
325
+
326
+ // Compare digit by digit.
327
+ for (i = -1; ++i < j;) {
328
+ if (xc[i] != yc[i]) return xc[i] > yc[i] ^ isneg ? 1 : -1;
329
+ }
330
+
331
+ // Compare lengths.
332
+ return k == l ? 0 : k > l ^ isneg ? 1 : -1;
333
+ };
334
+
335
+
336
+ /*
337
+ * Return a new Big whose value is the value of this Big divided by the value of Big y, rounded,
338
+ * if necessary, to a maximum of Big.DP decimal places using rounding mode Big.RM.
339
+ */
340
+ P.div = function (y) {
341
+ var x = this,
342
+ Big = x.constructor,
343
+ a = x.c, // dividend
344
+ b = (y = new Big(y)).c, // divisor
345
+ k = x.s == y.s ? 1 : -1,
346
+ dp = Big.DP;
347
+
348
+ if (dp !== ~~dp || dp < 0 || dp > MAX_DP) {
349
+ throw Error(INVALID_DP);
350
+ }
351
+
352
+ // Divisor is zero?
353
+ if (!b[0]) {
354
+ throw Error(DIV_BY_ZERO);
355
+ }
356
+
357
+ // Dividend is 0? Return +-0.
358
+ if (!a[0]) {
359
+ y.s = k;
360
+ y.c = [y.e = 0];
361
+ return y;
362
+ }
363
+
364
+ var bl, bt, n, cmp, ri,
365
+ bz = b.slice(),
366
+ ai = bl = b.length,
367
+ al = a.length,
368
+ r = a.slice(0, bl), // remainder
369
+ rl = r.length,
370
+ q = y, // quotient
371
+ qc = q.c = [],
372
+ qi = 0,
373
+ p = dp + (q.e = x.e - y.e) + 1; // precision of the result
374
+
375
+ q.s = k;
376
+ k = p < 0 ? 0 : p;
377
+
378
+ // Create version of divisor with leading zero.
379
+ bz.unshift(0);
380
+
381
+ // Add zeros to make remainder as long as divisor.
382
+ for (; rl++ < bl;) r.push(0);
383
+
384
+ do {
385
+
386
+ // n is how many times the divisor goes into current remainder.
387
+ for (n = 0; n < 10; n++) {
388
+
389
+ // Compare divisor and remainder.
390
+ if (bl != (rl = r.length)) {
391
+ cmp = bl > rl ? 1 : -1;
392
+ } else {
393
+ for (ri = -1, cmp = 0; ++ri < bl;) {
394
+ if (b[ri] != r[ri]) {
395
+ cmp = b[ri] > r[ri] ? 1 : -1;
396
+ break;
397
+ }
398
+ }
399
+ }
400
+
401
+ // If divisor < remainder, subtract divisor from remainder.
402
+ if (cmp < 0) {
403
+
404
+ // Remainder can't be more than 1 digit longer than divisor.
405
+ // Equalise lengths using divisor with extra leading zero?
406
+ for (bt = rl == bl ? b : bz; rl;) {
407
+ if (r[--rl] < bt[rl]) {
408
+ ri = rl;
409
+ for (; ri && !r[--ri];) r[ri] = 9;
410
+ --r[ri];
411
+ r[rl] += 10;
412
+ }
413
+ r[rl] -= bt[rl];
414
+ }
415
+
416
+ for (; !r[0];) r.shift();
417
+ } else {
418
+ break;
419
+ }
420
+ }
421
+
422
+ // Add the digit n to the result array.
423
+ qc[qi++] = cmp ? n : ++n;
424
+
425
+ // Update the remainder.
426
+ if (r[0] && cmp) r[rl] = a[ai] || 0;
427
+ else r = [a[ai]];
428
+
429
+ } while ((ai++ < al || r[0] !== UNDEFINED) && k--);
430
+
431
+ // Leading zero? Do not remove if result is simply zero (qi == 1).
432
+ if (!qc[0] && qi != 1) {
433
+
434
+ // There can't be more than one zero.
435
+ qc.shift();
436
+ q.e--;
437
+ p--;
438
+ }
439
+
440
+ // Round?
441
+ if (qi > p) round(q, p, Big.RM, r[0] !== UNDEFINED);
442
+
443
+ return q;
444
+ };
445
+
446
+
447
+ /*
448
+ * Return true if the value of this Big is equal to the value of Big y, otherwise return false.
449
+ */
450
+ P.eq = function (y) {
451
+ return this.cmp(y) === 0;
452
+ };
453
+
454
+
455
+ /*
456
+ * Return true if the value of this Big is greater than the value of Big y, otherwise return
457
+ * false.
458
+ */
459
+ P.gt = function (y) {
460
+ return this.cmp(y) > 0;
461
+ };
462
+
463
+
464
+ /*
465
+ * Return true if the value of this Big is greater than or equal to the value of Big y, otherwise
466
+ * return false.
467
+ */
468
+ P.gte = function (y) {
469
+ return this.cmp(y) > -1;
470
+ };
471
+
472
+
473
+ /*
474
+ * Return true if the value of this Big is less than the value of Big y, otherwise return false.
475
+ */
476
+ P.lt = function (y) {
477
+ return this.cmp(y) < 0;
478
+ };
479
+
480
+
481
+ /*
482
+ * Return true if the value of this Big is less than or equal to the value of Big y, otherwise
483
+ * return false.
484
+ */
485
+ P.lte = function (y) {
486
+ return this.cmp(y) < 1;
487
+ };
488
+
489
+
490
+ /*
491
+ * Return a new Big whose value is the value of this Big minus the value of Big y.
492
+ */
493
+ P.minus = P.sub = function (y) {
494
+ var i, j, t, xlty,
495
+ x = this,
496
+ Big = x.constructor,
497
+ a = x.s,
498
+ b = (y = new Big(y)).s;
499
+
500
+ // Signs differ?
501
+ if (a != b) {
502
+ y.s = -b;
503
+ return x.plus(y);
504
+ }
505
+
506
+ var xc = x.c.slice(),
507
+ xe = x.e,
508
+ yc = y.c,
509
+ ye = y.e;
510
+
511
+ // Either zero?
512
+ if (!xc[0] || !yc[0]) {
513
+ if (yc[0]) {
514
+ y.s = -b;
515
+ } else if (xc[0]) {
516
+ y = new Big(x);
517
+ } else {
518
+ y.s = 1;
519
+ }
520
+ return y;
521
+ }
522
+
523
+ // Determine which is the bigger number. Prepend zeros to equalise exponents.
524
+ if (a = xe - ye) {
525
+
526
+ if (xlty = a < 0) {
527
+ a = -a;
528
+ t = xc;
529
+ } else {
530
+ ye = xe;
531
+ t = yc;
532
+ }
533
+
534
+ t.reverse();
535
+ for (b = a; b--;) t.push(0);
536
+ t.reverse();
537
+ } else {
538
+
539
+ // Exponents equal. Check digit by digit.
540
+ j = ((xlty = xc.length < yc.length) ? xc : yc).length;
541
+
542
+ for (a = b = 0; b < j; b++) {
543
+ if (xc[b] != yc[b]) {
544
+ xlty = xc[b] < yc[b];
545
+ break;
546
+ }
547
+ }
548
+ }
549
+
550
+ // x < y? Point xc to the array of the bigger number.
551
+ if (xlty) {
552
+ t = xc;
553
+ xc = yc;
554
+ yc = t;
555
+ y.s = -y.s;
556
+ }
557
+
558
+ /*
559
+ * Append zeros to xc if shorter. No need to add zeros to yc if shorter as subtraction only
560
+ * needs to start at yc.length.
561
+ */
562
+ if ((b = (j = yc.length) - (i = xc.length)) > 0) for (; b--;) xc[i++] = 0;
563
+
564
+ // Subtract yc from xc.
565
+ for (b = i; j > a;) {
566
+ if (xc[--j] < yc[j]) {
567
+ for (i = j; i && !xc[--i];) xc[i] = 9;
568
+ --xc[i];
569
+ xc[j] += 10;
570
+ }
571
+
572
+ xc[j] -= yc[j];
573
+ }
574
+
575
+ // Remove trailing zeros.
576
+ for (; xc[--b] === 0;) xc.pop();
577
+
578
+ // Remove leading zeros and adjust exponent accordingly.
579
+ for (; xc[0] === 0;) {
580
+ xc.shift();
581
+ --ye;
582
+ }
583
+
584
+ if (!xc[0]) {
585
+
586
+ // n - n = +0
587
+ y.s = 1;
588
+
589
+ // Result must be zero.
590
+ xc = [ye = 0];
591
+ }
592
+
593
+ y.c = xc;
594
+ y.e = ye;
595
+
596
+ return y;
597
+ };
598
+
599
+
600
+ /*
601
+ * Return a new Big whose value is the value of this Big modulo the value of Big y.
602
+ */
603
+ P.mod = function (y) {
604
+ var ygtx,
605
+ x = this,
606
+ Big = x.constructor,
607
+ a = x.s,
608
+ b = (y = new Big(y)).s;
609
+
610
+ if (!y.c[0]) {
611
+ throw Error(DIV_BY_ZERO);
612
+ }
613
+
614
+ x.s = y.s = 1;
615
+ ygtx = y.cmp(x) == 1;
616
+ x.s = a;
617
+ y.s = b;
618
+
619
+ if (ygtx) return new Big(x);
620
+
621
+ a = Big.DP;
622
+ b = Big.RM;
623
+ Big.DP = Big.RM = 0;
624
+ x = x.div(y);
625
+ Big.DP = a;
626
+ Big.RM = b;
627
+
628
+ return this.minus(x.times(y));
629
+ };
630
+
631
+
632
+ /*
633
+ * Return a new Big whose value is the value of this Big negated.
634
+ */
635
+ P.neg = function () {
636
+ var x = new this.constructor(this);
637
+ x.s = -x.s;
638
+ return x;
639
+ };
640
+
641
+
642
+ /*
643
+ * Return a new Big whose value is the value of this Big plus the value of Big y.
644
+ */
645
+ P.plus = P.add = function (y) {
646
+ var e, k, t,
647
+ x = this,
648
+ Big = x.constructor;
649
+
650
+ y = new Big(y);
651
+
652
+ // Signs differ?
653
+ if (x.s != y.s) {
654
+ y.s = -y.s;
655
+ return x.minus(y);
656
+ }
657
+
658
+ var xe = x.e,
659
+ xc = x.c,
660
+ ye = y.e,
661
+ yc = y.c;
662
+
663
+ // Either zero?
664
+ if (!xc[0] || !yc[0]) {
665
+ if (!yc[0]) {
666
+ if (xc[0]) {
667
+ y = new Big(x);
668
+ } else {
669
+ y.s = x.s;
670
+ }
671
+ }
672
+ return y;
673
+ }
674
+
675
+ xc = xc.slice();
676
+
677
+ // Prepend zeros to equalise exponents.
678
+ // Note: reverse faster than unshifts.
679
+ if (e = xe - ye) {
680
+ if (e > 0) {
681
+ ye = xe;
682
+ t = yc;
683
+ } else {
684
+ e = -e;
685
+ t = xc;
686
+ }
687
+
688
+ t.reverse();
689
+ for (; e--;) t.push(0);
690
+ t.reverse();
691
+ }
692
+
693
+ // Point xc to the longer array.
694
+ if (xc.length - yc.length < 0) {
695
+ t = yc;
696
+ yc = xc;
697
+ xc = t;
698
+ }
699
+
700
+ e = yc.length;
701
+
702
+ // Only start adding at yc.length - 1 as the further digits of xc can be left as they are.
703
+ for (k = 0; e; xc[e] %= 10) k = (xc[--e] = xc[e] + yc[e] + k) / 10 | 0;
704
+
705
+ // No need to check for zero, as +x + +y != 0 && -x + -y != 0
706
+
707
+ if (k) {
708
+ xc.unshift(k);
709
+ ++ye;
710
+ }
711
+
712
+ // Remove trailing zeros.
713
+ for (e = xc.length; xc[--e] === 0;) xc.pop();
714
+
715
+ y.c = xc;
716
+ y.e = ye;
717
+
718
+ return y;
719
+ };
720
+
721
+
722
+ /*
723
+ * Return a Big whose value is the value of this Big raised to the power n.
724
+ * If n is negative, round to a maximum of Big.DP decimal places using rounding
725
+ * mode Big.RM.
726
+ *
727
+ * n {number} Integer, -MAX_POWER to MAX_POWER inclusive.
728
+ */
729
+ P.pow = function (n) {
730
+ var x = this,
731
+ one = new x.constructor('1'),
732
+ y = one,
733
+ isneg = n < 0;
734
+
735
+ if (n !== ~~n || n < -MAX_POWER || n > MAX_POWER) {
736
+ throw Error(INVALID + 'exponent');
737
+ }
738
+
739
+ if (isneg) n = -n;
740
+
741
+ for (;;) {
742
+ if (n & 1) y = y.times(x);
743
+ n >>= 1;
744
+ if (!n) break;
745
+ x = x.times(x);
746
+ }
747
+
748
+ return isneg ? one.div(y) : y;
749
+ };
750
+
751
+
752
+ /*
753
+ * Return a new Big whose value is the value of this Big rounded to a maximum precision of sd
754
+ * significant digits using rounding mode rm, or Big.RM if rm is not specified.
755
+ *
756
+ * sd {number} Significant digits: integer, 1 to MAX_DP inclusive.
757
+ * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up).
758
+ */
759
+ P.prec = function (sd, rm) {
760
+ if (sd !== ~~sd || sd < 1 || sd > MAX_DP) {
761
+ throw Error(INVALID + 'precision');
762
+ }
763
+ return round(new this.constructor(this), sd, rm);
764
+ };
765
+
766
+
767
+ /*
768
+ * Return a new Big whose value is the value of this Big rounded to a maximum of dp decimal places
769
+ * using rounding mode rm, or Big.RM if rm is not specified.
770
+ * If dp is negative, round to an integer which is a multiple of 10**-dp.
771
+ * If dp is not specified, round to 0 decimal places.
772
+ *
773
+ * dp? {number} Integer, -MAX_DP to MAX_DP inclusive.
774
+ * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up).
775
+ */
776
+ P.round = function (dp, rm) {
777
+ if (dp === UNDEFINED) dp = 0;
778
+ else if (dp !== ~~dp || dp < -MAX_DP || dp > MAX_DP) {
779
+ throw Error(INVALID_DP);
780
+ }
781
+ return round(new this.constructor(this), dp + this.e + 1, rm);
782
+ };
783
+
784
+
785
+ /*
786
+ * Return a new Big whose value is the square root of the value of this Big, rounded, if
787
+ * necessary, to a maximum of Big.DP decimal places using rounding mode Big.RM.
788
+ */
789
+ P.sqrt = function () {
790
+ var r, c, t,
791
+ x = this,
792
+ Big = x.constructor,
793
+ s = x.s,
794
+ e = x.e,
795
+ half = new Big('0.5');
796
+
797
+ // Zero?
798
+ if (!x.c[0]) return new Big(x);
799
+
800
+ // Negative?
801
+ if (s < 0) {
802
+ throw Error(NAME + 'No square root');
803
+ }
804
+
805
+ // Estimate.
806
+ s = Math.sqrt(x + '');
807
+
808
+ // Math.sqrt underflow/overflow?
809
+ // Re-estimate: pass x coefficient to Math.sqrt as integer, then adjust the result exponent.
810
+ if (s === 0 || s === 1 / 0) {
811
+ c = x.c.join('');
812
+ if (!(c.length + e & 1)) c += '0';
813
+ s = Math.sqrt(c);
814
+ e = ((e + 1) / 2 | 0) - (e < 0 || e & 1);
815
+ r = new Big((s == 1 / 0 ? '5e' : (s = s.toExponential()).slice(0, s.indexOf('e') + 1)) + e);
816
+ } else {
817
+ r = new Big(s + '');
818
+ }
819
+
820
+ e = r.e + (Big.DP += 4);
821
+
822
+ // Newton-Raphson iteration.
823
+ do {
824
+ t = r;
825
+ r = half.times(t.plus(x.div(t)));
826
+ } while (t.c.slice(0, e).join('') !== r.c.slice(0, e).join(''));
827
+
828
+ return round(r, (Big.DP -= 4) + r.e + 1, Big.RM);
829
+ };
830
+
831
+
832
+ /*
833
+ * Return a new Big whose value is the value of this Big times the value of Big y.
834
+ */
835
+ P.times = P.mul = function (y) {
836
+ var c,
837
+ x = this,
838
+ Big = x.constructor,
839
+ xc = x.c,
840
+ yc = (y = new Big(y)).c,
841
+ a = xc.length,
842
+ b = yc.length,
843
+ i = x.e,
844
+ j = y.e;
845
+
846
+ // Determine sign of result.
847
+ y.s = x.s == y.s ? 1 : -1;
848
+
849
+ // Return signed 0 if either 0.
850
+ if (!xc[0] || !yc[0]) {
851
+ y.c = [y.e = 0];
852
+ return y;
853
+ }
854
+
855
+ // Initialise exponent of result as x.e + y.e.
856
+ y.e = i + j;
857
+
858
+ // If array xc has fewer digits than yc, swap xc and yc, and lengths.
859
+ if (a < b) {
860
+ c = xc;
861
+ xc = yc;
862
+ yc = c;
863
+ j = a;
864
+ a = b;
865
+ b = j;
866
+ }
867
+
868
+ // Initialise coefficient array of result with zeros.
869
+ for (c = new Array(j = a + b); j--;) c[j] = 0;
870
+
871
+ // Multiply.
872
+
873
+ // i is initially xc.length.
874
+ for (i = b; i--;) {
875
+ b = 0;
876
+
877
+ // a is yc.length.
878
+ for (j = a + i; j > i;) {
879
+
880
+ // Current sum of products at this digit position, plus carry.
881
+ b = c[j] + yc[i] * xc[j - i - 1] + b;
882
+ c[j--] = b % 10;
883
+
884
+ // carry
885
+ b = b / 10 | 0;
886
+ }
887
+
888
+ c[j] = b;
889
+ }
890
+
891
+ // Increment result exponent if there is a final carry, otherwise remove leading zero.
892
+ if (b) ++y.e;
893
+ else c.shift();
894
+
895
+ // Remove trailing zeros.
896
+ for (i = c.length; !c[--i];) c.pop();
897
+ y.c = c;
898
+
899
+ return y;
900
+ };
901
+
902
+
903
+ /*
904
+ * Return a string representing the value of this Big in exponential notation rounded to dp fixed
905
+ * decimal places using rounding mode rm, or Big.RM if rm is not specified.
906
+ *
907
+ * dp? {number} Decimal places: integer, 0 to MAX_DP inclusive.
908
+ * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up).
909
+ */
910
+ P.toExponential = function (dp, rm) {
911
+ var x = this,
912
+ n = x.c[0];
913
+
914
+ if (dp !== UNDEFINED) {
915
+ if (dp !== ~~dp || dp < 0 || dp > MAX_DP) {
916
+ throw Error(INVALID_DP);
917
+ }
918
+ x = round(new x.constructor(x), ++dp, rm);
919
+ for (; x.c.length < dp;) x.c.push(0);
920
+ }
921
+
922
+ return stringify(x, true, !!n);
923
+ };
924
+
925
+
926
+ /*
927
+ * Return a string representing the value of this Big in normal notation rounded to dp fixed
928
+ * decimal places using rounding mode rm, or Big.RM if rm is not specified.
929
+ *
930
+ * dp? {number} Decimal places: integer, 0 to MAX_DP inclusive.
931
+ * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up).
932
+ *
933
+ * (-0).toFixed(0) is '0', but (-0.1).toFixed(0) is '-0'.
934
+ * (-0).toFixed(1) is '0.0', but (-0.01).toFixed(1) is '-0.0'.
935
+ */
936
+ P.toFixed = function (dp, rm) {
937
+ var x = this,
938
+ n = x.c[0];
939
+
940
+ if (dp !== UNDEFINED) {
941
+ if (dp !== ~~dp || dp < 0 || dp > MAX_DP) {
942
+ throw Error(INVALID_DP);
943
+ }
944
+ x = round(new x.constructor(x), dp + x.e + 1, rm);
945
+
946
+ // x.e may have changed if the value is rounded up.
947
+ for (dp = dp + x.e + 1; x.c.length < dp;) x.c.push(0);
948
+ }
949
+
950
+ return stringify(x, false, !!n);
951
+ };
952
+
953
+
954
+ /*
955
+ * Return a string representing the value of this Big.
956
+ * Return exponential notation if this Big has a positive exponent equal to or greater than
957
+ * Big.PE, or a negative exponent equal to or less than Big.NE.
958
+ * Omit the sign for negative zero.
959
+ */
960
+ P[Symbol.for('nodejs.util.inspect.custom')] = P.toJSON = P.toString = function () {
961
+ var x = this,
962
+ Big = x.constructor;
963
+ return stringify(x, x.e <= Big.NE || x.e >= Big.PE, !!x.c[0]);
964
+ };
965
+
966
+
967
+ /*
968
+ * Return the value of this Big as a primitve number.
969
+ */
970
+ P.toNumber = function () {
971
+ var n = Number(stringify(this, true, true));
972
+ if (this.constructor.strict === true && !this.eq(n.toString())) {
973
+ throw Error(NAME + 'Imprecise conversion');
974
+ }
975
+ return n;
976
+ };
977
+
978
+
979
+ /*
980
+ * Return a string representing the value of this Big rounded to sd significant digits using
981
+ * rounding mode rm, or Big.RM if rm is not specified.
982
+ * Use exponential notation if sd is less than the number of digits necessary to represent
983
+ * the integer part of the value in normal notation.
984
+ *
985
+ * sd {number} Significant digits: integer, 1 to MAX_DP inclusive.
986
+ * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up).
987
+ */
988
+ P.toPrecision = function (sd, rm) {
989
+ var x = this,
990
+ Big = x.constructor,
991
+ n = x.c[0];
992
+
993
+ if (sd !== UNDEFINED) {
994
+ if (sd !== ~~sd || sd < 1 || sd > MAX_DP) {
995
+ throw Error(INVALID + 'precision');
996
+ }
997
+ x = round(new Big(x), sd, rm);
998
+ for (; x.c.length < sd;) x.c.push(0);
999
+ }
1000
+
1001
+ return stringify(x, sd <= x.e || x.e <= Big.NE || x.e >= Big.PE, !!n);
1002
+ };
1003
+
1004
+
1005
+ /*
1006
+ * Return a string representing the value of this Big.
1007
+ * Return exponential notation if this Big has a positive exponent equal to or greater than
1008
+ * Big.PE, or a negative exponent equal to or less than Big.NE.
1009
+ * Include the sign for negative zero.
1010
+ */
1011
+ P.valueOf = function () {
1012
+ var x = this,
1013
+ Big = x.constructor;
1014
+ if (Big.strict === true) {
1015
+ throw Error(NAME + 'valueOf disallowed');
1016
+ }
1017
+ return stringify(x, x.e <= Big.NE || x.e >= Big.PE, true);
1018
+ };
1019
+
1020
+
1021
+ // Export
1022
+
1023
+
1024
+ export var Big = _Big_();
1025
+
1026
+ /// <reference types="https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/master/types/big.js/index.d.ts" />
1027
+ export default Big;