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