@eternl/big 0.10.16 → 0.10.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +382 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +30 -0
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +30 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +382 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -2
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
//#region src/index.ts
|
|
2
2
|
/************************************** EDITABLE DEFAULTS *****************************************/
|
|
3
3
|
var DP = 20, RM = 1, MAX_DP = 1e6, MAX_POWER = 1e6, NE = -7, PE = 21, STRICT = false, NAME = "[big.js] ", INVALID = NAME + "Invalid ", INVALID_DP = INVALID + "decimal places", INVALID_RM = INVALID + "rounding mode", DIV_BY_ZERO = NAME + "Division by zero", P = {}, UNDEFINED = void 0, NUMERIC = /^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;
|
|
4
|
+
const SOURCE_BUFFER = [];
|
|
5
|
+
let SOURCE_C = SOURCE_BUFFER;
|
|
6
|
+
let SOURCE_E = 0;
|
|
7
|
+
let SOURCE_S = 1;
|
|
4
8
|
function _Big_() {
|
|
5
9
|
function Big(n) {
|
|
6
10
|
const Ctor = Big;
|
|
@@ -37,6 +41,12 @@ function _Big_() {
|
|
|
37
41
|
function negateSign(s) {
|
|
38
42
|
return s === 1 ? -1 : 1;
|
|
39
43
|
}
|
|
44
|
+
function mutateFrom(x, y) {
|
|
45
|
+
x.c = y.c;
|
|
46
|
+
x.e = y.e;
|
|
47
|
+
x.s = y.s;
|
|
48
|
+
return x;
|
|
49
|
+
}
|
|
40
50
|
function parse(x, n) {
|
|
41
51
|
var e, i, nl;
|
|
42
52
|
if (!NUMERIC.test(n)) throw Error(INVALID + "number");
|
|
@@ -58,6 +68,50 @@ function parse(x, n) {
|
|
|
58
68
|
}
|
|
59
69
|
return x;
|
|
60
70
|
}
|
|
71
|
+
function isInternalBigSource(value) {
|
|
72
|
+
return typeof value === "object";
|
|
73
|
+
}
|
|
74
|
+
function parseSourceString(n) {
|
|
75
|
+
let e;
|
|
76
|
+
let i;
|
|
77
|
+
let nl;
|
|
78
|
+
if (!NUMERIC.test(n)) throw Error(INVALID + "number");
|
|
79
|
+
SOURCE_S = n.charAt(0) == "-" ? (n = n.slice(1), -1) : 1;
|
|
80
|
+
if ((e = n.indexOf(".")) > -1) n = n.replace(".", "");
|
|
81
|
+
if ((i = n.search(/e/i)) > 0) {
|
|
82
|
+
if (e < 0) e = i;
|
|
83
|
+
e += +n.slice(i + 1);
|
|
84
|
+
n = n.substring(0, i);
|
|
85
|
+
} else if (e < 0) e = n.length;
|
|
86
|
+
nl = n.length;
|
|
87
|
+
for (i = 0; i < nl && n.charAt(i) == "0";) ++i;
|
|
88
|
+
if (i == nl) {
|
|
89
|
+
SOURCE_E = 0;
|
|
90
|
+
SOURCE_C = SOURCE_BUFFER;
|
|
91
|
+
SOURCE_C.length = 1;
|
|
92
|
+
SOURCE_C[0] = 0;
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
for (; nl > 0 && n.charAt(--nl) == "0";);
|
|
96
|
+
SOURCE_E = e - i - 1;
|
|
97
|
+
SOURCE_C = SOURCE_BUFFER;
|
|
98
|
+
SOURCE_C.length = 0;
|
|
99
|
+
for (e = 0; i <= nl;) SOURCE_C[e++] = +n.charAt(i++);
|
|
100
|
+
}
|
|
101
|
+
function loadSourceParts(Big, value) {
|
|
102
|
+
if (isInternalBigSource(value)) {
|
|
103
|
+
SOURCE_C = value.c;
|
|
104
|
+
SOURCE_E = value.e;
|
|
105
|
+
SOURCE_S = value.s;
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
let n = value;
|
|
109
|
+
if (typeof n !== "string") {
|
|
110
|
+
if (Big.strict === true && typeof n !== "bigint") throw TypeError(INVALID + "value");
|
|
111
|
+
n = n === 0 && 1 / n < 0 ? "-0" : String(n);
|
|
112
|
+
}
|
|
113
|
+
parseSourceString(n);
|
|
114
|
+
}
|
|
61
115
|
function round(x, sd, rm, more) {
|
|
62
116
|
var xc = x.c;
|
|
63
117
|
var d0 = xc[0] ?? 0;
|
|
@@ -103,6 +157,22 @@ P.abs = function() {
|
|
|
103
157
|
x.s = 1;
|
|
104
158
|
return x;
|
|
105
159
|
};
|
|
160
|
+
P._abs = function() {
|
|
161
|
+
if (this.s < 0) this.s = 1;
|
|
162
|
+
return this;
|
|
163
|
+
};
|
|
164
|
+
P._set = function(y) {
|
|
165
|
+
const x = this;
|
|
166
|
+
const Big = x.constructor;
|
|
167
|
+
loadSourceParts(Big, y);
|
|
168
|
+
const yc = SOURCE_C;
|
|
169
|
+
const yl = yc.length;
|
|
170
|
+
x.c.length = yl;
|
|
171
|
+
for (let i = 0; i < yl; i++) x.c[i] = yc[i] ?? 0;
|
|
172
|
+
x.e = SOURCE_E;
|
|
173
|
+
x.s = SOURCE_S;
|
|
174
|
+
return x;
|
|
175
|
+
};
|
|
106
176
|
P.cmp = function(y) {
|
|
107
177
|
var i, j, isneg, x = this, xc = x.c, yBig = new x.constructor(y), yc = yBig.c, xs = x.s, ys = yBig.s, k = x.e, l = yBig.e;
|
|
108
178
|
if (!xc[0] || !yc[0]) return !xc[0] ? !yc[0] ? 0 : negateSign(ys) : xs;
|
|
@@ -158,6 +228,51 @@ P.div = function(y) {
|
|
|
158
228
|
if (qi > p) round(q, p, Big.RM, r[0] !== UNDEFINED);
|
|
159
229
|
return q;
|
|
160
230
|
};
|
|
231
|
+
P._div = function(y) {
|
|
232
|
+
var x = this, Big = x.constructor, a = x.c.slice(), yBig = new Big(y), b = yBig.c, sign = x.s == yBig.s ? 1 : -1, dp = Big.DP;
|
|
233
|
+
if (dp !== ~~dp || dp < 0 || dp > MAX_DP) throw Error(INVALID_DP);
|
|
234
|
+
if (!b[0]) throw Error(DIV_BY_ZERO);
|
|
235
|
+
if (!a[0]) {
|
|
236
|
+
x.s = sign;
|
|
237
|
+
x.c = [x.e = 0];
|
|
238
|
+
return x;
|
|
239
|
+
}
|
|
240
|
+
var bl, bt, n, cmp, ri, bz = b.slice(), ai = bl = b.length, al = a.length, r = a.slice(0, bl), rl = r.length, q = x, qc = q.c = [], qi = 0, p = dp + (q.e = x.e - yBig.e) + 1, k = p < 0 ? 0 : p;
|
|
241
|
+
q.s = sign;
|
|
242
|
+
bz.unshift(0);
|
|
243
|
+
for (; rl++ < bl;) r.push(0);
|
|
244
|
+
do {
|
|
245
|
+
for (n = 0; n < 10; n++) {
|
|
246
|
+
if (bl != (rl = r.length)) cmp = bl > rl ? 1 : -1;
|
|
247
|
+
else for (ri = -1, cmp = 0; ++ri < bl;) if (b[ri] != r[ri]) {
|
|
248
|
+
cmp = b[ri] > r[ri] ? 1 : -1;
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
if (cmp < 0) {
|
|
252
|
+
for (bt = rl == bl ? b : bz; rl;) {
|
|
253
|
+
if (r[--rl] < bt[rl]) {
|
|
254
|
+
ri = rl;
|
|
255
|
+
for (; ri && !r[--ri];) r[ri] = 9;
|
|
256
|
+
--r[ri];
|
|
257
|
+
r[rl] = (r[rl] ?? 0) + 10;
|
|
258
|
+
}
|
|
259
|
+
r[rl] = (r[rl] ?? 0) - bt[rl];
|
|
260
|
+
}
|
|
261
|
+
for (; !r[0];) r.shift();
|
|
262
|
+
} else break;
|
|
263
|
+
}
|
|
264
|
+
qc[qi++] = cmp ? n : ++n;
|
|
265
|
+
if (r[0] && cmp) r[rl] = a[ai] || 0;
|
|
266
|
+
else r = [a[ai]];
|
|
267
|
+
} while ((ai++ < al || r[0] !== UNDEFINED) && k--);
|
|
268
|
+
if (!qc[0] && qi != 1) {
|
|
269
|
+
qc.shift();
|
|
270
|
+
q.e--;
|
|
271
|
+
p--;
|
|
272
|
+
}
|
|
273
|
+
if (qi > p) round(q, p, Big.RM, r[0] !== UNDEFINED);
|
|
274
|
+
return q;
|
|
275
|
+
};
|
|
161
276
|
P.eq = function(y) {
|
|
162
277
|
return this.cmp(y) === 0;
|
|
163
278
|
};
|
|
@@ -232,6 +347,67 @@ P.minus = P.sub = function(y) {
|
|
|
232
347
|
yBig.e = ye;
|
|
233
348
|
return yBig;
|
|
234
349
|
};
|
|
350
|
+
P._minus = P._sub = function(y) {
|
|
351
|
+
var i, j, t, xlty, x = this, Big = x.constructor, yBig = new Big(y), xSign = x.s, ySign = yBig.s, xc = x.c.slice(), xe = x.e, yc = yBig.c, ye = yBig.e, eDiff = xe - ye;
|
|
352
|
+
if (xSign != ySign) {
|
|
353
|
+
yBig.s = negateSign(ySign);
|
|
354
|
+
return x._plus(yBig);
|
|
355
|
+
}
|
|
356
|
+
if (!xc[0] || !yc[0]) {
|
|
357
|
+
if (!yc[0]) return x;
|
|
358
|
+
x.c = yc.slice();
|
|
359
|
+
x.e = ye;
|
|
360
|
+
x.s = negateSign(ySign);
|
|
361
|
+
if (!x.c[0]) x.s = 1;
|
|
362
|
+
return x;
|
|
363
|
+
}
|
|
364
|
+
if (eDiff) {
|
|
365
|
+
if (xlty = eDiff < 0) {
|
|
366
|
+
eDiff = -eDiff;
|
|
367
|
+
t = xc;
|
|
368
|
+
} else {
|
|
369
|
+
ye = xe;
|
|
370
|
+
t = yc;
|
|
371
|
+
}
|
|
372
|
+
t.reverse();
|
|
373
|
+
for (j = eDiff; j--;) t.push(0);
|
|
374
|
+
t.reverse();
|
|
375
|
+
} else {
|
|
376
|
+
j = ((xlty = xc.length < yc.length) ? xc : yc).length;
|
|
377
|
+
for (i = 0; i < j; i++) if (xc[i] != yc[i]) {
|
|
378
|
+
xlty = xc[i] < yc[i];
|
|
379
|
+
break;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
if (xlty) {
|
|
383
|
+
t = xc;
|
|
384
|
+
xc = yc;
|
|
385
|
+
yc = t;
|
|
386
|
+
xSign = negateSign(xSign);
|
|
387
|
+
}
|
|
388
|
+
if ((j = yc.length - (i = xc.length)) > 0) for (; j--;) xc[i++] = 0;
|
|
389
|
+
for (j = i; j > eDiff;) {
|
|
390
|
+
if (xc[--j] < (yc[j] ?? 0)) {
|
|
391
|
+
for (i = j; i && !xc[--i];) xc[i] = 9;
|
|
392
|
+
--xc[i];
|
|
393
|
+
xc[j] = (xc[j] ?? 0) + 10;
|
|
394
|
+
}
|
|
395
|
+
xc[j] = (xc[j] ?? 0) - (yc[j] ?? 0);
|
|
396
|
+
}
|
|
397
|
+
for (j = xc.length; xc[--j] === 0;) xc.pop();
|
|
398
|
+
for (; xc[0] === 0;) {
|
|
399
|
+
xc.shift();
|
|
400
|
+
--ye;
|
|
401
|
+
}
|
|
402
|
+
if (!xc[0]) {
|
|
403
|
+
xSign = 1;
|
|
404
|
+
xc = [ye = 0];
|
|
405
|
+
}
|
|
406
|
+
x.c = xc;
|
|
407
|
+
x.e = ye;
|
|
408
|
+
x.s = xSign;
|
|
409
|
+
return x;
|
|
410
|
+
};
|
|
235
411
|
P.mod = function(y) {
|
|
236
412
|
var ygtx, x = this, Big = x.constructor, yBig = new Big(y), xSign = x.s, ySign = yBig.s, dp, rm;
|
|
237
413
|
if (!yBig.c[0]) throw Error(DIV_BY_ZERO);
|
|
@@ -249,11 +425,32 @@ P.mod = function(y) {
|
|
|
249
425
|
Big.RM = rm;
|
|
250
426
|
return this.minus(x.times(yBig));
|
|
251
427
|
};
|
|
428
|
+
P._mod = function(y) {
|
|
429
|
+
var ygtx, x = this, Big = x.constructor, yBig = new Big(y), xSign = x.s, ySign = yBig.s, dp, rm, quotient;
|
|
430
|
+
if (!yBig.c[0]) throw Error(DIV_BY_ZERO);
|
|
431
|
+
x.s = yBig.s = 1;
|
|
432
|
+
ygtx = yBig.cmp(x) == 1;
|
|
433
|
+
x.s = xSign;
|
|
434
|
+
yBig.s = ySign;
|
|
435
|
+
if (ygtx) return x;
|
|
436
|
+
dp = Big.DP;
|
|
437
|
+
rm = Big.RM;
|
|
438
|
+
Big.DP = 0;
|
|
439
|
+
Big.RM = 0;
|
|
440
|
+
quotient = new Big(x).div(yBig);
|
|
441
|
+
Big.DP = dp;
|
|
442
|
+
Big.RM = rm;
|
|
443
|
+
return x._minus(quotient.times(yBig));
|
|
444
|
+
};
|
|
252
445
|
P.neg = function() {
|
|
253
446
|
var x = new this.constructor(this);
|
|
254
447
|
x.s = negateSign(x.s);
|
|
255
448
|
return x;
|
|
256
449
|
};
|
|
450
|
+
P._neg = function() {
|
|
451
|
+
this.s = negateSign(this.s);
|
|
452
|
+
return this;
|
|
453
|
+
};
|
|
257
454
|
P.plus = P.add = function(y) {
|
|
258
455
|
var e, k, t, x = this, Big = x.constructor, yBig = new Big(y);
|
|
259
456
|
if (x.s != yBig.s) {
|
|
@@ -298,6 +495,114 @@ P.plus = P.add = function(y) {
|
|
|
298
495
|
yBig.e = ye;
|
|
299
496
|
return yBig;
|
|
300
497
|
};
|
|
498
|
+
P._plus = P._add = function(y) {
|
|
499
|
+
const x = this;
|
|
500
|
+
const Big = x.constructor;
|
|
501
|
+
loadSourceParts(Big, y);
|
|
502
|
+
const yc = SOURCE_C;
|
|
503
|
+
const ye = SOURCE_E;
|
|
504
|
+
const ys = SOURCE_S;
|
|
505
|
+
const xc = x.c;
|
|
506
|
+
const xe = x.e;
|
|
507
|
+
const xs = x.s;
|
|
508
|
+
const xLen = xc.length;
|
|
509
|
+
const yLen = yc.length;
|
|
510
|
+
if (!xc[0] || !yc[0]) {
|
|
511
|
+
if (!yc[0]) return x;
|
|
512
|
+
xc.length = yLen;
|
|
513
|
+
for (let i = 0; i < yLen; i++) xc[i] = yc[i] ?? 0;
|
|
514
|
+
x.e = ye;
|
|
515
|
+
x.s = ys;
|
|
516
|
+
return x;
|
|
517
|
+
}
|
|
518
|
+
const topExponent = xe > ye ? xe : ye;
|
|
519
|
+
const shiftX = topExponent - xe;
|
|
520
|
+
const shiftY = topExponent - ye;
|
|
521
|
+
if (xs == ys) {
|
|
522
|
+
const lenX = xLen + shiftX;
|
|
523
|
+
const lenY = yLen + shiftY;
|
|
524
|
+
let len = lenX > lenY ? lenX : lenY;
|
|
525
|
+
let carry = 0;
|
|
526
|
+
xc.length = len;
|
|
527
|
+
for (let i = len - 1; i >= 0; i--) {
|
|
528
|
+
const ix = i - shiftX;
|
|
529
|
+
const iy = i - shiftY;
|
|
530
|
+
const sum = (ix >= 0 && ix < xLen ? xc[ix] ?? 0 : 0) + (iy >= 0 && iy < yLen ? yc[iy] ?? 0 : 0) + carry;
|
|
531
|
+
xc[i] = sum % 10;
|
|
532
|
+
carry = sum / 10 | 0;
|
|
533
|
+
}
|
|
534
|
+
if (carry) {
|
|
535
|
+
xc.length = len + 1;
|
|
536
|
+
for (let i = len; i > 0; i--) xc[i] = xc[i - 1] ?? 0;
|
|
537
|
+
xc[0] = carry;
|
|
538
|
+
len += 1;
|
|
539
|
+
x.e = topExponent + 1;
|
|
540
|
+
} else x.e = topExponent;
|
|
541
|
+
while (len > 1 && xc[len - 1] === 0) --len;
|
|
542
|
+
xc.length = len;
|
|
543
|
+
x.s = xs;
|
|
544
|
+
return x;
|
|
545
|
+
}
|
|
546
|
+
let cmp = 0;
|
|
547
|
+
if (xe != ye) cmp = xe > ye ? 1 : -1;
|
|
548
|
+
else if (xLen != yLen) cmp = xLen > yLen ? 1 : -1;
|
|
549
|
+
else for (let i = 0; i < xLen; i++) {
|
|
550
|
+
const xd = xc[i] ?? 0;
|
|
551
|
+
const yd = yc[i] ?? 0;
|
|
552
|
+
if (xd != yd) {
|
|
553
|
+
cmp = xd > yd ? 1 : -1;
|
|
554
|
+
break;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
if (cmp === 0) {
|
|
558
|
+
xc.length = 1;
|
|
559
|
+
xc[0] = 0;
|
|
560
|
+
x.e = 0;
|
|
561
|
+
x.s = 1;
|
|
562
|
+
return x;
|
|
563
|
+
}
|
|
564
|
+
const ac = cmp > 0 ? xc : yc;
|
|
565
|
+
const ae = cmp > 0 ? xe : ye;
|
|
566
|
+
const al = cmp > 0 ? xLen : yLen;
|
|
567
|
+
const bc = cmp > 0 ? yc : xc;
|
|
568
|
+
const be = cmp > 0 ? ye : xe;
|
|
569
|
+
const bl = cmp > 0 ? yLen : xLen;
|
|
570
|
+
const resultSign = cmp > 0 ? xs : ys;
|
|
571
|
+
const alignedExponent = ae > be ? ae : be;
|
|
572
|
+
const shiftA = alignedExponent - ae;
|
|
573
|
+
const shiftB = alignedExponent - be;
|
|
574
|
+
const lenA = al + shiftA;
|
|
575
|
+
const lenB = bl + shiftB;
|
|
576
|
+
let len = lenA > lenB ? lenA : lenB;
|
|
577
|
+
let borrow = 0;
|
|
578
|
+
xc.length = len;
|
|
579
|
+
for (let i = len - 1; i >= 0; i--) {
|
|
580
|
+
const ia = i - shiftA;
|
|
581
|
+
const ib = i - shiftB;
|
|
582
|
+
let da = ia >= 0 && ia < al ? ac[ia] ?? 0 : 0;
|
|
583
|
+
const db = (ib >= 0 && ib < bl ? bc[ib] ?? 0 : 0) + borrow;
|
|
584
|
+
if (da < db) {
|
|
585
|
+
da += 10;
|
|
586
|
+
borrow = 1;
|
|
587
|
+
} else borrow = 0;
|
|
588
|
+
xc[i] = da - db;
|
|
589
|
+
}
|
|
590
|
+
let head = 0;
|
|
591
|
+
while (head < len - 1 && xc[head] === 0) ++head;
|
|
592
|
+
if (head) {
|
|
593
|
+
for (let i = 0; i < len - head; i++) xc[i] = xc[i + head] ?? 0;
|
|
594
|
+
len -= head;
|
|
595
|
+
}
|
|
596
|
+
while (len > 1 && xc[len - 1] === 0) --len;
|
|
597
|
+
xc.length = len;
|
|
598
|
+
x.e = alignedExponent - head;
|
|
599
|
+
x.s = resultSign;
|
|
600
|
+
if (!xc[0]) {
|
|
601
|
+
x.e = 0;
|
|
602
|
+
x.s = 1;
|
|
603
|
+
}
|
|
604
|
+
return x;
|
|
605
|
+
};
|
|
301
606
|
P.pow = function(n) {
|
|
302
607
|
var x = this, one = new x.constructor("1"), y = one, isneg = n < 0;
|
|
303
608
|
if (n !== ~~n || n < -MAX_POWER || n > MAX_POWER) throw Error(INVALID + "exponent");
|
|
@@ -310,15 +615,36 @@ P.pow = function(n) {
|
|
|
310
615
|
}
|
|
311
616
|
return isneg ? one.div(y) : y;
|
|
312
617
|
};
|
|
618
|
+
P._pow = function(n) {
|
|
619
|
+
var x = this, one = new x.constructor("1"), y = one, base = new x.constructor(x), isneg = n < 0;
|
|
620
|
+
if (n !== ~~n || n < -MAX_POWER || n > MAX_POWER) throw Error(INVALID + "exponent");
|
|
621
|
+
if (isneg) n = -n;
|
|
622
|
+
for (;;) {
|
|
623
|
+
if (n & 1) y._times(base);
|
|
624
|
+
n >>= 1;
|
|
625
|
+
if (!n) break;
|
|
626
|
+
base._times(base);
|
|
627
|
+
}
|
|
628
|
+
return isneg ? mutateFrom(x, one._div(y)) : mutateFrom(x, y);
|
|
629
|
+
};
|
|
313
630
|
P.prec = function(sd, rm) {
|
|
314
631
|
if (sd !== ~~sd || sd < 1 || sd > MAX_DP) throw Error(INVALID + "precision");
|
|
315
632
|
return round(new this.constructor(this), sd, rm);
|
|
316
633
|
};
|
|
634
|
+
P._prec = function(sd, rm) {
|
|
635
|
+
if (sd !== ~~sd || sd < 1 || sd > MAX_DP) throw Error(INVALID + "precision");
|
|
636
|
+
return round(this, sd, rm);
|
|
637
|
+
};
|
|
317
638
|
P.round = function(dp, rm) {
|
|
318
639
|
if (dp === UNDEFINED) dp = 0;
|
|
319
640
|
else if (dp !== ~~dp || dp < -MAX_DP || dp > MAX_DP) throw Error(INVALID_DP);
|
|
320
641
|
return round(new this.constructor(this), dp + this.e + 1, rm);
|
|
321
642
|
};
|
|
643
|
+
P._round = function(dp, rm) {
|
|
644
|
+
if (dp === UNDEFINED) dp = 0;
|
|
645
|
+
else if (dp !== ~~dp || dp < -MAX_DP || dp > MAX_DP) throw Error(INVALID_DP);
|
|
646
|
+
return round(this, dp + this.e + 1, rm);
|
|
647
|
+
};
|
|
322
648
|
P.sqrt = function() {
|
|
323
649
|
var r, c, t, x = this, Big = x.constructor, sign = x.s, e = x.e, half = new Big("0.5"), s;
|
|
324
650
|
if (!x.c[0]) return new Big(x);
|
|
@@ -342,6 +668,29 @@ P.sqrt = function() {
|
|
|
342
668
|
} while (t.c.slice(0, e).join("") !== r.c.slice(0, e).join(""));
|
|
343
669
|
return round(r, (Big.DP -= 4) + r.e + 1, Big.RM);
|
|
344
670
|
};
|
|
671
|
+
P._sqrt = function() {
|
|
672
|
+
var r, c, t, x = this, Big = x.constructor, sign = x.s, e = x.e, half = new Big("0.5"), s;
|
|
673
|
+
if (!x.c[0]) return x;
|
|
674
|
+
if (sign < 0) throw Error(NAME + "No square root");
|
|
675
|
+
s = Math.sqrt(+stringify(x, true, true));
|
|
676
|
+
if (s === 0 || s === Infinity) {
|
|
677
|
+
c = x.c.join("");
|
|
678
|
+
if (!(c.length + e & 1)) c += "0";
|
|
679
|
+
s = Math.sqrt(+c);
|
|
680
|
+
e = ((e + 1) / 2 | 0) - (e < 0 || (e & 1) !== 0 ? 1 : 0);
|
|
681
|
+
if (s == Infinity) r = new Big("5e" + e);
|
|
682
|
+
else {
|
|
683
|
+
c = s.toExponential();
|
|
684
|
+
r = new Big(c.slice(0, c.indexOf("e") + 1) + e);
|
|
685
|
+
}
|
|
686
|
+
} else r = new Big(s + "");
|
|
687
|
+
e = r.e + (Big.DP += 4);
|
|
688
|
+
do {
|
|
689
|
+
t = r;
|
|
690
|
+
r = half.times(t.plus(x.div(t)));
|
|
691
|
+
} while (t.c.slice(0, e).join("") !== r.c.slice(0, e).join(""));
|
|
692
|
+
return mutateFrom(x, round(r, (Big.DP -= 4) + r.e + 1, Big.RM));
|
|
693
|
+
};
|
|
345
694
|
P.times = P.mul = function(y) {
|
|
346
695
|
var c, x = this, Big = x.constructor, xc = x.c, yBig = new Big(y), yc = yBig.c, a = xc.length, b = yc.length, i = x.e, j = yBig.e;
|
|
347
696
|
yBig.s = x.s == yBig.s ? 1 : -1;
|
|
@@ -374,6 +723,39 @@ P.times = P.mul = function(y) {
|
|
|
374
723
|
yBig.c = c;
|
|
375
724
|
return yBig;
|
|
376
725
|
};
|
|
726
|
+
P._times = P._mul = function(y) {
|
|
727
|
+
var c, x = this, Big = x.constructor, xc = x.c, yBig = new Big(y), yc = yBig.c, a = xc.length, b = yc.length, i = x.e, j = yBig.e, sign = x.s == yBig.s ? 1 : -1;
|
|
728
|
+
if (!xc[0] || !yc[0]) {
|
|
729
|
+
x.s = sign;
|
|
730
|
+
x.c = [x.e = 0];
|
|
731
|
+
return x;
|
|
732
|
+
}
|
|
733
|
+
x.e = i + j;
|
|
734
|
+
if (a < b) {
|
|
735
|
+
c = xc;
|
|
736
|
+
xc = yc;
|
|
737
|
+
yc = c;
|
|
738
|
+
j = a;
|
|
739
|
+
a = b;
|
|
740
|
+
b = j;
|
|
741
|
+
}
|
|
742
|
+
for (c = new Array(j = a + b); j--;) c[j] = 0;
|
|
743
|
+
for (i = b; i--;) {
|
|
744
|
+
b = 0;
|
|
745
|
+
for (j = a + i; j > i;) {
|
|
746
|
+
b = (c[j] ?? 0) + (yc[i] ?? 0) * (xc[j - i - 1] ?? 0) + b;
|
|
747
|
+
c[j--] = b % 10;
|
|
748
|
+
b = b / 10 | 0;
|
|
749
|
+
}
|
|
750
|
+
c[j] = b;
|
|
751
|
+
}
|
|
752
|
+
if (b) ++x.e;
|
|
753
|
+
else c.shift();
|
|
754
|
+
for (i = c.length; !c[--i];) c.pop();
|
|
755
|
+
x.c = c;
|
|
756
|
+
x.s = sign;
|
|
757
|
+
return x;
|
|
758
|
+
};
|
|
377
759
|
P.toExponential = function(dp, rm) {
|
|
378
760
|
var x = this, n = x.c[0];
|
|
379
761
|
if (dp !== UNDEFINED) {
|