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