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