@bulletxyz/bullet-sdk 0.16.0-rc.0 → 0.16.1-rc.0

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.
@@ -8,8 +8,8 @@ var __typeError = (msg) => {
8
8
  throw TypeError(msg);
9
9
  };
10
10
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
- var __commonJS = (cb, mod) => function __require() {
12
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
11
+ var __commonJS = (cb, mod2) => function __require() {
12
+ return mod2 || (0, cb[__getOwnPropNames(cb)[0]])((mod2 = { exports: {} }).exports, mod2), mod2.exports;
13
13
  };
14
14
  var __copyProps = (to, from, except, desc) => {
15
15
  if (from && typeof from === "object" || typeof from === "function") {
@@ -19,13 +19,13 @@ var __copyProps = (to, from, except, desc) => {
19
19
  }
20
20
  return to;
21
21
  };
22
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ var __toESM = (mod2, isNodeMode, target) => (target = mod2 != null ? __create(__getProtoOf(mod2)) : {}, __copyProps(
23
23
  // If the importer is in node compatibility mode or this is not an ESM
24
24
  // file that has been converted to a CommonJS file using a Babel-
25
25
  // compatible transform (i.e. "__esModule" has not been set), then set
26
26
  // "default" to the CommonJS "module.exports" for node compatibility.
27
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
28
- mod
27
+ isNodeMode || !mod2 || !mod2.__esModule ? __defProp(target, "default", { value: mod2, enumerable: true }) : target,
28
+ mod2
29
29
  ));
30
30
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
31
31
  var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
@@ -206,6 +206,2185 @@ var require_eventemitter3 = __commonJS({
206
206
 
207
207
  // src/utils.ts
208
208
  import bs58 from "bs58";
209
+
210
+ // ../node_modules/.pnpm/decimal.js@10.5.0/node_modules/decimal.js/decimal.mjs
211
+ var EXP_LIMIT = 9e15;
212
+ var MAX_DIGITS = 1e9;
213
+ var NUMERALS = "0123456789abcdef";
214
+ var LN10 = "2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058";
215
+ var PI = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789";
216
+ var DEFAULTS = {
217
+ // These values must be integers within the stated ranges (inclusive).
218
+ // Most of these values can be changed at run-time using the `Decimal.config` method.
219
+ // The maximum number of significant digits of the result of a calculation or base conversion.
220
+ // E.g. `Decimal.config({ precision: 20 });`
221
+ precision: 20,
222
+ // 1 to MAX_DIGITS
223
+ // The rounding mode used when rounding to `precision`.
224
+ //
225
+ // ROUND_UP 0 Away from zero.
226
+ // ROUND_DOWN 1 Towards zero.
227
+ // ROUND_CEIL 2 Towards +Infinity.
228
+ // ROUND_FLOOR 3 Towards -Infinity.
229
+ // ROUND_HALF_UP 4 Towards nearest neighbour. If equidistant, up.
230
+ // ROUND_HALF_DOWN 5 Towards nearest neighbour. If equidistant, down.
231
+ // ROUND_HALF_EVEN 6 Towards nearest neighbour. If equidistant, towards even neighbour.
232
+ // ROUND_HALF_CEIL 7 Towards nearest neighbour. If equidistant, towards +Infinity.
233
+ // ROUND_HALF_FLOOR 8 Towards nearest neighbour. If equidistant, towards -Infinity.
234
+ //
235
+ // E.g.
236
+ // `Decimal.rounding = 4;`
237
+ // `Decimal.rounding = Decimal.ROUND_HALF_UP;`
238
+ rounding: 4,
239
+ // 0 to 8
240
+ // The modulo mode used when calculating the modulus: a mod n.
241
+ // The quotient (q = a / n) is calculated according to the corresponding rounding mode.
242
+ // The remainder (r) is calculated as: r = a - n * q.
243
+ //
244
+ // UP 0 The remainder is positive if the dividend is negative, else is negative.
245
+ // DOWN 1 The remainder has the same sign as the dividend (JavaScript %).
246
+ // FLOOR 3 The remainder has the same sign as the divisor (Python %).
247
+ // HALF_EVEN 6 The IEEE 754 remainder function.
248
+ // EUCLID 9 Euclidian division. q = sign(n) * floor(a / abs(n)). Always positive.
249
+ //
250
+ // Truncated division (1), floored division (3), the IEEE 754 remainder (6), and Euclidian
251
+ // division (9) are commonly used for the modulus operation. The other rounding modes can also
252
+ // be used, but they may not give useful results.
253
+ modulo: 1,
254
+ // 0 to 9
255
+ // The exponent value at and beneath which `toString` returns exponential notation.
256
+ // JavaScript numbers: -7
257
+ toExpNeg: -7,
258
+ // 0 to -EXP_LIMIT
259
+ // The exponent value at and above which `toString` returns exponential notation.
260
+ // JavaScript numbers: 21
261
+ toExpPos: 21,
262
+ // 0 to EXP_LIMIT
263
+ // The minimum exponent value, beneath which underflow to zero occurs.
264
+ // JavaScript numbers: -324 (5e-324)
265
+ minE: -EXP_LIMIT,
266
+ // -1 to -EXP_LIMIT
267
+ // The maximum exponent value, above which overflow to Infinity occurs.
268
+ // JavaScript numbers: 308 (1.7976931348623157e+308)
269
+ maxE: EXP_LIMIT,
270
+ // 1 to EXP_LIMIT
271
+ // Whether to use cryptographically-secure random number generation, if available.
272
+ crypto: false
273
+ // true/false
274
+ };
275
+ var inexact;
276
+ var quadrant;
277
+ var external = true;
278
+ var decimalError = "[DecimalError] ";
279
+ var invalidArgument = decimalError + "Invalid argument: ";
280
+ var precisionLimitExceeded = decimalError + "Precision limit exceeded";
281
+ var cryptoUnavailable = decimalError + "crypto unavailable";
282
+ var tag = "[object Decimal]";
283
+ var mathfloor = Math.floor;
284
+ var mathpow = Math.pow;
285
+ var isBinary = /^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i;
286
+ var isHex = /^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i;
287
+ var isOctal = /^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i;
288
+ var isDecimal = /^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;
289
+ var BASE = 1e7;
290
+ var LOG_BASE = 7;
291
+ var MAX_SAFE_INTEGER = 9007199254740991;
292
+ var LN10_PRECISION = LN10.length - 1;
293
+ var PI_PRECISION = PI.length - 1;
294
+ var P = { toStringTag: tag };
295
+ P.absoluteValue = P.abs = function() {
296
+ var x = new this.constructor(this);
297
+ if (x.s < 0) x.s = 1;
298
+ return finalise(x);
299
+ };
300
+ P.ceil = function() {
301
+ return finalise(new this.constructor(this), this.e + 1, 2);
302
+ };
303
+ P.clampedTo = P.clamp = function(min2, max2) {
304
+ var k, x = this, Ctor = x.constructor;
305
+ min2 = new Ctor(min2);
306
+ max2 = new Ctor(max2);
307
+ if (!min2.s || !max2.s) return new Ctor(NaN);
308
+ if (min2.gt(max2)) throw Error(invalidArgument + max2);
309
+ k = x.cmp(min2);
310
+ return k < 0 ? min2 : x.cmp(max2) > 0 ? max2 : new Ctor(x);
311
+ };
312
+ P.comparedTo = P.cmp = function(y) {
313
+ var i, j, xdL, ydL, x = this, xd = x.d, yd = (y = new x.constructor(y)).d, xs = x.s, ys = y.s;
314
+ if (!xd || !yd) {
315
+ return !xs || !ys ? NaN : xs !== ys ? xs : xd === yd ? 0 : !xd ^ xs < 0 ? 1 : -1;
316
+ }
317
+ if (!xd[0] || !yd[0]) return xd[0] ? xs : yd[0] ? -ys : 0;
318
+ if (xs !== ys) return xs;
319
+ if (x.e !== y.e) return x.e > y.e ^ xs < 0 ? 1 : -1;
320
+ xdL = xd.length;
321
+ ydL = yd.length;
322
+ for (i = 0, j = xdL < ydL ? xdL : ydL; i < j; ++i) {
323
+ if (xd[i] !== yd[i]) return xd[i] > yd[i] ^ xs < 0 ? 1 : -1;
324
+ }
325
+ return xdL === ydL ? 0 : xdL > ydL ^ xs < 0 ? 1 : -1;
326
+ };
327
+ P.cosine = P.cos = function() {
328
+ var pr, rm, x = this, Ctor = x.constructor;
329
+ if (!x.d) return new Ctor(NaN);
330
+ if (!x.d[0]) return new Ctor(1);
331
+ pr = Ctor.precision;
332
+ rm = Ctor.rounding;
333
+ Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
334
+ Ctor.rounding = 1;
335
+ x = cosine(Ctor, toLessThanHalfPi(Ctor, x));
336
+ Ctor.precision = pr;
337
+ Ctor.rounding = rm;
338
+ return finalise(quadrant == 2 || quadrant == 3 ? x.neg() : x, pr, rm, true);
339
+ };
340
+ P.cubeRoot = P.cbrt = function() {
341
+ var e, m, n, r, rep, s, sd, t, t3, t3plusx, x = this, Ctor = x.constructor;
342
+ if (!x.isFinite() || x.isZero()) return new Ctor(x);
343
+ external = false;
344
+ s = x.s * mathpow(x.s * x, 1 / 3);
345
+ if (!s || Math.abs(s) == 1 / 0) {
346
+ n = digitsToString(x.d);
347
+ e = x.e;
348
+ if (s = (e - n.length + 1) % 3) n += s == 1 || s == -2 ? "0" : "00";
349
+ s = mathpow(n, 1 / 3);
350
+ e = mathfloor((e + 1) / 3) - (e % 3 == (e < 0 ? -1 : 2));
351
+ if (s == 1 / 0) {
352
+ n = "5e" + e;
353
+ } else {
354
+ n = s.toExponential();
355
+ n = n.slice(0, n.indexOf("e") + 1) + e;
356
+ }
357
+ r = new Ctor(n);
358
+ r.s = x.s;
359
+ } else {
360
+ r = new Ctor(s.toString());
361
+ }
362
+ sd = (e = Ctor.precision) + 3;
363
+ for (; ; ) {
364
+ t = r;
365
+ t3 = t.times(t).times(t);
366
+ t3plusx = t3.plus(x);
367
+ r = divide(t3plusx.plus(x).times(t), t3plusx.plus(t3), sd + 2, 1);
368
+ if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
369
+ n = n.slice(sd - 3, sd + 1);
370
+ if (n == "9999" || !rep && n == "4999") {
371
+ if (!rep) {
372
+ finalise(t, e + 1, 0);
373
+ if (t.times(t).times(t).eq(x)) {
374
+ r = t;
375
+ break;
376
+ }
377
+ }
378
+ sd += 4;
379
+ rep = 1;
380
+ } else {
381
+ if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
382
+ finalise(r, e + 1, 1);
383
+ m = !r.times(r).times(r).eq(x);
384
+ }
385
+ break;
386
+ }
387
+ }
388
+ }
389
+ external = true;
390
+ return finalise(r, e, Ctor.rounding, m);
391
+ };
392
+ P.decimalPlaces = P.dp = function() {
393
+ var w, d = this.d, n = NaN;
394
+ if (d) {
395
+ w = d.length - 1;
396
+ n = (w - mathfloor(this.e / LOG_BASE)) * LOG_BASE;
397
+ w = d[w];
398
+ if (w) for (; w % 10 == 0; w /= 10) n--;
399
+ if (n < 0) n = 0;
400
+ }
401
+ return n;
402
+ };
403
+ P.dividedBy = P.div = function(y) {
404
+ return divide(this, new this.constructor(y));
405
+ };
406
+ P.dividedToIntegerBy = P.divToInt = function(y) {
407
+ var x = this, Ctor = x.constructor;
408
+ return finalise(divide(x, new Ctor(y), 0, 1, 1), Ctor.precision, Ctor.rounding);
409
+ };
410
+ P.equals = P.eq = function(y) {
411
+ return this.cmp(y) === 0;
412
+ };
413
+ P.floor = function() {
414
+ return finalise(new this.constructor(this), this.e + 1, 3);
415
+ };
416
+ P.greaterThan = P.gt = function(y) {
417
+ return this.cmp(y) > 0;
418
+ };
419
+ P.greaterThanOrEqualTo = P.gte = function(y) {
420
+ var k = this.cmp(y);
421
+ return k == 1 || k === 0;
422
+ };
423
+ P.hyperbolicCosine = P.cosh = function() {
424
+ var k, n, pr, rm, len, x = this, Ctor = x.constructor, one = new Ctor(1);
425
+ if (!x.isFinite()) return new Ctor(x.s ? 1 / 0 : NaN);
426
+ if (x.isZero()) return one;
427
+ pr = Ctor.precision;
428
+ rm = Ctor.rounding;
429
+ Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
430
+ Ctor.rounding = 1;
431
+ len = x.d.length;
432
+ if (len < 32) {
433
+ k = Math.ceil(len / 3);
434
+ n = (1 / tinyPow(4, k)).toString();
435
+ } else {
436
+ k = 16;
437
+ n = "2.3283064365386962890625e-10";
438
+ }
439
+ x = taylorSeries(Ctor, 1, x.times(n), new Ctor(1), true);
440
+ var cosh2_x, i = k, d8 = new Ctor(8);
441
+ for (; i--; ) {
442
+ cosh2_x = x.times(x);
443
+ x = one.minus(cosh2_x.times(d8.minus(cosh2_x.times(d8))));
444
+ }
445
+ return finalise(x, Ctor.precision = pr, Ctor.rounding = rm, true);
446
+ };
447
+ P.hyperbolicSine = P.sinh = function() {
448
+ var k, pr, rm, len, x = this, Ctor = x.constructor;
449
+ if (!x.isFinite() || x.isZero()) return new Ctor(x);
450
+ pr = Ctor.precision;
451
+ rm = Ctor.rounding;
452
+ Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
453
+ Ctor.rounding = 1;
454
+ len = x.d.length;
455
+ if (len < 3) {
456
+ x = taylorSeries(Ctor, 2, x, x, true);
457
+ } else {
458
+ k = 1.4 * Math.sqrt(len);
459
+ k = k > 16 ? 16 : k | 0;
460
+ x = x.times(1 / tinyPow(5, k));
461
+ x = taylorSeries(Ctor, 2, x, x, true);
462
+ var sinh2_x, d5 = new Ctor(5), d16 = new Ctor(16), d20 = new Ctor(20);
463
+ for (; k--; ) {
464
+ sinh2_x = x.times(x);
465
+ x = x.times(d5.plus(sinh2_x.times(d16.times(sinh2_x).plus(d20))));
466
+ }
467
+ }
468
+ Ctor.precision = pr;
469
+ Ctor.rounding = rm;
470
+ return finalise(x, pr, rm, true);
471
+ };
472
+ P.hyperbolicTangent = P.tanh = function() {
473
+ var pr, rm, x = this, Ctor = x.constructor;
474
+ if (!x.isFinite()) return new Ctor(x.s);
475
+ if (x.isZero()) return new Ctor(x);
476
+ pr = Ctor.precision;
477
+ rm = Ctor.rounding;
478
+ Ctor.precision = pr + 7;
479
+ Ctor.rounding = 1;
480
+ return divide(x.sinh(), x.cosh(), Ctor.precision = pr, Ctor.rounding = rm);
481
+ };
482
+ P.inverseCosine = P.acos = function() {
483
+ var x = this, Ctor = x.constructor, k = x.abs().cmp(1), pr = Ctor.precision, rm = Ctor.rounding;
484
+ if (k !== -1) {
485
+ return k === 0 ? x.isNeg() ? getPi(Ctor, pr, rm) : new Ctor(0) : new Ctor(NaN);
486
+ }
487
+ if (x.isZero()) return getPi(Ctor, pr + 4, rm).times(0.5);
488
+ Ctor.precision = pr + 6;
489
+ Ctor.rounding = 1;
490
+ x = new Ctor(1).minus(x).div(x.plus(1)).sqrt().atan();
491
+ Ctor.precision = pr;
492
+ Ctor.rounding = rm;
493
+ return x.times(2);
494
+ };
495
+ P.inverseHyperbolicCosine = P.acosh = function() {
496
+ var pr, rm, x = this, Ctor = x.constructor;
497
+ if (x.lte(1)) return new Ctor(x.eq(1) ? 0 : NaN);
498
+ if (!x.isFinite()) return new Ctor(x);
499
+ pr = Ctor.precision;
500
+ rm = Ctor.rounding;
501
+ Ctor.precision = pr + Math.max(Math.abs(x.e), x.sd()) + 4;
502
+ Ctor.rounding = 1;
503
+ external = false;
504
+ x = x.times(x).minus(1).sqrt().plus(x);
505
+ external = true;
506
+ Ctor.precision = pr;
507
+ Ctor.rounding = rm;
508
+ return x.ln();
509
+ };
510
+ P.inverseHyperbolicSine = P.asinh = function() {
511
+ var pr, rm, x = this, Ctor = x.constructor;
512
+ if (!x.isFinite() || x.isZero()) return new Ctor(x);
513
+ pr = Ctor.precision;
514
+ rm = Ctor.rounding;
515
+ Ctor.precision = pr + 2 * Math.max(Math.abs(x.e), x.sd()) + 6;
516
+ Ctor.rounding = 1;
517
+ external = false;
518
+ x = x.times(x).plus(1).sqrt().plus(x);
519
+ external = true;
520
+ Ctor.precision = pr;
521
+ Ctor.rounding = rm;
522
+ return x.ln();
523
+ };
524
+ P.inverseHyperbolicTangent = P.atanh = function() {
525
+ var pr, rm, wpr, xsd, x = this, Ctor = x.constructor;
526
+ if (!x.isFinite()) return new Ctor(NaN);
527
+ if (x.e >= 0) return new Ctor(x.abs().eq(1) ? x.s / 0 : x.isZero() ? x : NaN);
528
+ pr = Ctor.precision;
529
+ rm = Ctor.rounding;
530
+ xsd = x.sd();
531
+ if (Math.max(xsd, pr) < 2 * -x.e - 1) return finalise(new Ctor(x), pr, rm, true);
532
+ Ctor.precision = wpr = xsd - x.e;
533
+ x = divide(x.plus(1), new Ctor(1).minus(x), wpr + pr, 1);
534
+ Ctor.precision = pr + 4;
535
+ Ctor.rounding = 1;
536
+ x = x.ln();
537
+ Ctor.precision = pr;
538
+ Ctor.rounding = rm;
539
+ return x.times(0.5);
540
+ };
541
+ P.inverseSine = P.asin = function() {
542
+ var halfPi, k, pr, rm, x = this, Ctor = x.constructor;
543
+ if (x.isZero()) return new Ctor(x);
544
+ k = x.abs().cmp(1);
545
+ pr = Ctor.precision;
546
+ rm = Ctor.rounding;
547
+ if (k !== -1) {
548
+ if (k === 0) {
549
+ halfPi = getPi(Ctor, pr + 4, rm).times(0.5);
550
+ halfPi.s = x.s;
551
+ return halfPi;
552
+ }
553
+ return new Ctor(NaN);
554
+ }
555
+ Ctor.precision = pr + 6;
556
+ Ctor.rounding = 1;
557
+ x = x.div(new Ctor(1).minus(x.times(x)).sqrt().plus(1)).atan();
558
+ Ctor.precision = pr;
559
+ Ctor.rounding = rm;
560
+ return x.times(2);
561
+ };
562
+ P.inverseTangent = P.atan = function() {
563
+ var i, j, k, n, px, t, r, wpr, x2, x = this, Ctor = x.constructor, pr = Ctor.precision, rm = Ctor.rounding;
564
+ if (!x.isFinite()) {
565
+ if (!x.s) return new Ctor(NaN);
566
+ if (pr + 4 <= PI_PRECISION) {
567
+ r = getPi(Ctor, pr + 4, rm).times(0.5);
568
+ r.s = x.s;
569
+ return r;
570
+ }
571
+ } else if (x.isZero()) {
572
+ return new Ctor(x);
573
+ } else if (x.abs().eq(1) && pr + 4 <= PI_PRECISION) {
574
+ r = getPi(Ctor, pr + 4, rm).times(0.25);
575
+ r.s = x.s;
576
+ return r;
577
+ }
578
+ Ctor.precision = wpr = pr + 10;
579
+ Ctor.rounding = 1;
580
+ k = Math.min(28, wpr / LOG_BASE + 2 | 0);
581
+ for (i = k; i; --i) x = x.div(x.times(x).plus(1).sqrt().plus(1));
582
+ external = false;
583
+ j = Math.ceil(wpr / LOG_BASE);
584
+ n = 1;
585
+ x2 = x.times(x);
586
+ r = new Ctor(x);
587
+ px = x;
588
+ for (; i !== -1; ) {
589
+ px = px.times(x2);
590
+ t = r.minus(px.div(n += 2));
591
+ px = px.times(x2);
592
+ r = t.plus(px.div(n += 2));
593
+ if (r.d[j] !== void 0) for (i = j; r.d[i] === t.d[i] && i--; ) ;
594
+ }
595
+ if (k) r = r.times(2 << k - 1);
596
+ external = true;
597
+ return finalise(r, Ctor.precision = pr, Ctor.rounding = rm, true);
598
+ };
599
+ P.isFinite = function() {
600
+ return !!this.d;
601
+ };
602
+ P.isInteger = P.isInt = function() {
603
+ return !!this.d && mathfloor(this.e / LOG_BASE) > this.d.length - 2;
604
+ };
605
+ P.isNaN = function() {
606
+ return !this.s;
607
+ };
608
+ P.isNegative = P.isNeg = function() {
609
+ return this.s < 0;
610
+ };
611
+ P.isPositive = P.isPos = function() {
612
+ return this.s > 0;
613
+ };
614
+ P.isZero = function() {
615
+ return !!this.d && this.d[0] === 0;
616
+ };
617
+ P.lessThan = P.lt = function(y) {
618
+ return this.cmp(y) < 0;
619
+ };
620
+ P.lessThanOrEqualTo = P.lte = function(y) {
621
+ return this.cmp(y) < 1;
622
+ };
623
+ P.logarithm = P.log = function(base) {
624
+ var isBase10, d, denominator, k, inf, num, sd, r, arg = this, Ctor = arg.constructor, pr = Ctor.precision, rm = Ctor.rounding, guard = 5;
625
+ if (base == null) {
626
+ base = new Ctor(10);
627
+ isBase10 = true;
628
+ } else {
629
+ base = new Ctor(base);
630
+ d = base.d;
631
+ if (base.s < 0 || !d || !d[0] || base.eq(1)) return new Ctor(NaN);
632
+ isBase10 = base.eq(10);
633
+ }
634
+ d = arg.d;
635
+ if (arg.s < 0 || !d || !d[0] || arg.eq(1)) {
636
+ return new Ctor(d && !d[0] ? -1 / 0 : arg.s != 1 ? NaN : d ? 0 : 1 / 0);
637
+ }
638
+ if (isBase10) {
639
+ if (d.length > 1) {
640
+ inf = true;
641
+ } else {
642
+ for (k = d[0]; k % 10 === 0; ) k /= 10;
643
+ inf = k !== 1;
644
+ }
645
+ }
646
+ external = false;
647
+ sd = pr + guard;
648
+ num = naturalLogarithm(arg, sd);
649
+ denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
650
+ r = divide(num, denominator, sd, 1);
651
+ if (checkRoundingDigits(r.d, k = pr, rm)) {
652
+ do {
653
+ sd += 10;
654
+ num = naturalLogarithm(arg, sd);
655
+ denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
656
+ r = divide(num, denominator, sd, 1);
657
+ if (!inf) {
658
+ if (+digitsToString(r.d).slice(k + 1, k + 15) + 1 == 1e14) {
659
+ r = finalise(r, pr + 1, 0);
660
+ }
661
+ break;
662
+ }
663
+ } while (checkRoundingDigits(r.d, k += 10, rm));
664
+ }
665
+ external = true;
666
+ return finalise(r, pr, rm);
667
+ };
668
+ P.minus = P.sub = function(y) {
669
+ var d, e, i, j, k, len, pr, rm, xd, xe, xLTy, yd, x = this, Ctor = x.constructor;
670
+ y = new Ctor(y);
671
+ if (!x.d || !y.d) {
672
+ if (!x.s || !y.s) y = new Ctor(NaN);
673
+ else if (x.d) y.s = -y.s;
674
+ else y = new Ctor(y.d || x.s !== y.s ? x : NaN);
675
+ return y;
676
+ }
677
+ if (x.s != y.s) {
678
+ y.s = -y.s;
679
+ return x.plus(y);
680
+ }
681
+ xd = x.d;
682
+ yd = y.d;
683
+ pr = Ctor.precision;
684
+ rm = Ctor.rounding;
685
+ if (!xd[0] || !yd[0]) {
686
+ if (yd[0]) y.s = -y.s;
687
+ else if (xd[0]) y = new Ctor(x);
688
+ else return new Ctor(rm === 3 ? -0 : 0);
689
+ return external ? finalise(y, pr, rm) : y;
690
+ }
691
+ e = mathfloor(y.e / LOG_BASE);
692
+ xe = mathfloor(x.e / LOG_BASE);
693
+ xd = xd.slice();
694
+ k = xe - e;
695
+ if (k) {
696
+ xLTy = k < 0;
697
+ if (xLTy) {
698
+ d = xd;
699
+ k = -k;
700
+ len = yd.length;
701
+ } else {
702
+ d = yd;
703
+ e = xe;
704
+ len = xd.length;
705
+ }
706
+ i = Math.max(Math.ceil(pr / LOG_BASE), len) + 2;
707
+ if (k > i) {
708
+ k = i;
709
+ d.length = 1;
710
+ }
711
+ d.reverse();
712
+ for (i = k; i--; ) d.push(0);
713
+ d.reverse();
714
+ } else {
715
+ i = xd.length;
716
+ len = yd.length;
717
+ xLTy = i < len;
718
+ if (xLTy) len = i;
719
+ for (i = 0; i < len; i++) {
720
+ if (xd[i] != yd[i]) {
721
+ xLTy = xd[i] < yd[i];
722
+ break;
723
+ }
724
+ }
725
+ k = 0;
726
+ }
727
+ if (xLTy) {
728
+ d = xd;
729
+ xd = yd;
730
+ yd = d;
731
+ y.s = -y.s;
732
+ }
733
+ len = xd.length;
734
+ for (i = yd.length - len; i > 0; --i) xd[len++] = 0;
735
+ for (i = yd.length; i > k; ) {
736
+ if (xd[--i] < yd[i]) {
737
+ for (j = i; j && xd[--j] === 0; ) xd[j] = BASE - 1;
738
+ --xd[j];
739
+ xd[i] += BASE;
740
+ }
741
+ xd[i] -= yd[i];
742
+ }
743
+ for (; xd[--len] === 0; ) xd.pop();
744
+ for (; xd[0] === 0; xd.shift()) --e;
745
+ if (!xd[0]) return new Ctor(rm === 3 ? -0 : 0);
746
+ y.d = xd;
747
+ y.e = getBase10Exponent(xd, e);
748
+ return external ? finalise(y, pr, rm) : y;
749
+ };
750
+ P.modulo = P.mod = function(y) {
751
+ var q, x = this, Ctor = x.constructor;
752
+ y = new Ctor(y);
753
+ if (!x.d || !y.s || y.d && !y.d[0]) return new Ctor(NaN);
754
+ if (!y.d || x.d && !x.d[0]) {
755
+ return finalise(new Ctor(x), Ctor.precision, Ctor.rounding);
756
+ }
757
+ external = false;
758
+ if (Ctor.modulo == 9) {
759
+ q = divide(x, y.abs(), 0, 3, 1);
760
+ q.s *= y.s;
761
+ } else {
762
+ q = divide(x, y, 0, Ctor.modulo, 1);
763
+ }
764
+ q = q.times(y);
765
+ external = true;
766
+ return x.minus(q);
767
+ };
768
+ P.naturalExponential = P.exp = function() {
769
+ return naturalExponential(this);
770
+ };
771
+ P.naturalLogarithm = P.ln = function() {
772
+ return naturalLogarithm(this);
773
+ };
774
+ P.negated = P.neg = function() {
775
+ var x = new this.constructor(this);
776
+ x.s = -x.s;
777
+ return finalise(x);
778
+ };
779
+ P.plus = P.add = function(y) {
780
+ var carry, d, e, i, k, len, pr, rm, xd, yd, x = this, Ctor = x.constructor;
781
+ y = new Ctor(y);
782
+ if (!x.d || !y.d) {
783
+ if (!x.s || !y.s) y = new Ctor(NaN);
784
+ else if (!x.d) y = new Ctor(y.d || x.s === y.s ? x : NaN);
785
+ return y;
786
+ }
787
+ if (x.s != y.s) {
788
+ y.s = -y.s;
789
+ return x.minus(y);
790
+ }
791
+ xd = x.d;
792
+ yd = y.d;
793
+ pr = Ctor.precision;
794
+ rm = Ctor.rounding;
795
+ if (!xd[0] || !yd[0]) {
796
+ if (!yd[0]) y = new Ctor(x);
797
+ return external ? finalise(y, pr, rm) : y;
798
+ }
799
+ k = mathfloor(x.e / LOG_BASE);
800
+ e = mathfloor(y.e / LOG_BASE);
801
+ xd = xd.slice();
802
+ i = k - e;
803
+ if (i) {
804
+ if (i < 0) {
805
+ d = xd;
806
+ i = -i;
807
+ len = yd.length;
808
+ } else {
809
+ d = yd;
810
+ e = k;
811
+ len = xd.length;
812
+ }
813
+ k = Math.ceil(pr / LOG_BASE);
814
+ len = k > len ? k + 1 : len + 1;
815
+ if (i > len) {
816
+ i = len;
817
+ d.length = 1;
818
+ }
819
+ d.reverse();
820
+ for (; i--; ) d.push(0);
821
+ d.reverse();
822
+ }
823
+ len = xd.length;
824
+ i = yd.length;
825
+ if (len - i < 0) {
826
+ i = len;
827
+ d = yd;
828
+ yd = xd;
829
+ xd = d;
830
+ }
831
+ for (carry = 0; i; ) {
832
+ carry = (xd[--i] = xd[i] + yd[i] + carry) / BASE | 0;
833
+ xd[i] %= BASE;
834
+ }
835
+ if (carry) {
836
+ xd.unshift(carry);
837
+ ++e;
838
+ }
839
+ for (len = xd.length; xd[--len] == 0; ) xd.pop();
840
+ y.d = xd;
841
+ y.e = getBase10Exponent(xd, e);
842
+ return external ? finalise(y, pr, rm) : y;
843
+ };
844
+ P.precision = P.sd = function(z2) {
845
+ var k, x = this;
846
+ if (z2 !== void 0 && z2 !== !!z2 && z2 !== 1 && z2 !== 0) throw Error(invalidArgument + z2);
847
+ if (x.d) {
848
+ k = getPrecision(x.d);
849
+ if (z2 && x.e + 1 > k) k = x.e + 1;
850
+ } else {
851
+ k = NaN;
852
+ }
853
+ return k;
854
+ };
855
+ P.round = function() {
856
+ var x = this, Ctor = x.constructor;
857
+ return finalise(new Ctor(x), x.e + 1, Ctor.rounding);
858
+ };
859
+ P.sine = P.sin = function() {
860
+ var pr, rm, x = this, Ctor = x.constructor;
861
+ if (!x.isFinite()) return new Ctor(NaN);
862
+ if (x.isZero()) return new Ctor(x);
863
+ pr = Ctor.precision;
864
+ rm = Ctor.rounding;
865
+ Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
866
+ Ctor.rounding = 1;
867
+ x = sine(Ctor, toLessThanHalfPi(Ctor, x));
868
+ Ctor.precision = pr;
869
+ Ctor.rounding = rm;
870
+ return finalise(quadrant > 2 ? x.neg() : x, pr, rm, true);
871
+ };
872
+ P.squareRoot = P.sqrt = function() {
873
+ var m, n, sd, r, rep, t, x = this, d = x.d, e = x.e, s = x.s, Ctor = x.constructor;
874
+ if (s !== 1 || !d || !d[0]) {
875
+ return new Ctor(!s || s < 0 && (!d || d[0]) ? NaN : d ? x : 1 / 0);
876
+ }
877
+ external = false;
878
+ s = Math.sqrt(+x);
879
+ if (s == 0 || s == 1 / 0) {
880
+ n = digitsToString(d);
881
+ if ((n.length + e) % 2 == 0) n += "0";
882
+ s = Math.sqrt(n);
883
+ e = mathfloor((e + 1) / 2) - (e < 0 || e % 2);
884
+ if (s == 1 / 0) {
885
+ n = "5e" + e;
886
+ } else {
887
+ n = s.toExponential();
888
+ n = n.slice(0, n.indexOf("e") + 1) + e;
889
+ }
890
+ r = new Ctor(n);
891
+ } else {
892
+ r = new Ctor(s.toString());
893
+ }
894
+ sd = (e = Ctor.precision) + 3;
895
+ for (; ; ) {
896
+ t = r;
897
+ r = t.plus(divide(x, t, sd + 2, 1)).times(0.5);
898
+ if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
899
+ n = n.slice(sd - 3, sd + 1);
900
+ if (n == "9999" || !rep && n == "4999") {
901
+ if (!rep) {
902
+ finalise(t, e + 1, 0);
903
+ if (t.times(t).eq(x)) {
904
+ r = t;
905
+ break;
906
+ }
907
+ }
908
+ sd += 4;
909
+ rep = 1;
910
+ } else {
911
+ if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
912
+ finalise(r, e + 1, 1);
913
+ m = !r.times(r).eq(x);
914
+ }
915
+ break;
916
+ }
917
+ }
918
+ }
919
+ external = true;
920
+ return finalise(r, e, Ctor.rounding, m);
921
+ };
922
+ P.tangent = P.tan = function() {
923
+ var pr, rm, x = this, Ctor = x.constructor;
924
+ if (!x.isFinite()) return new Ctor(NaN);
925
+ if (x.isZero()) return new Ctor(x);
926
+ pr = Ctor.precision;
927
+ rm = Ctor.rounding;
928
+ Ctor.precision = pr + 10;
929
+ Ctor.rounding = 1;
930
+ x = x.sin();
931
+ x.s = 1;
932
+ x = divide(x, new Ctor(1).minus(x.times(x)).sqrt(), pr + 10, 0);
933
+ Ctor.precision = pr;
934
+ Ctor.rounding = rm;
935
+ return finalise(quadrant == 2 || quadrant == 4 ? x.neg() : x, pr, rm, true);
936
+ };
937
+ P.times = P.mul = function(y) {
938
+ var carry, e, i, k, r, rL, t, xdL, ydL, x = this, Ctor = x.constructor, xd = x.d, yd = (y = new Ctor(y)).d;
939
+ y.s *= x.s;
940
+ if (!xd || !xd[0] || !yd || !yd[0]) {
941
+ return new Ctor(!y.s || xd && !xd[0] && !yd || yd && !yd[0] && !xd ? NaN : !xd || !yd ? y.s / 0 : y.s * 0);
942
+ }
943
+ e = mathfloor(x.e / LOG_BASE) + mathfloor(y.e / LOG_BASE);
944
+ xdL = xd.length;
945
+ ydL = yd.length;
946
+ if (xdL < ydL) {
947
+ r = xd;
948
+ xd = yd;
949
+ yd = r;
950
+ rL = xdL;
951
+ xdL = ydL;
952
+ ydL = rL;
953
+ }
954
+ r = [];
955
+ rL = xdL + ydL;
956
+ for (i = rL; i--; ) r.push(0);
957
+ for (i = ydL; --i >= 0; ) {
958
+ carry = 0;
959
+ for (k = xdL + i; k > i; ) {
960
+ t = r[k] + yd[i] * xd[k - i - 1] + carry;
961
+ r[k--] = t % BASE | 0;
962
+ carry = t / BASE | 0;
963
+ }
964
+ r[k] = (r[k] + carry) % BASE | 0;
965
+ }
966
+ for (; !r[--rL]; ) r.pop();
967
+ if (carry) ++e;
968
+ else r.shift();
969
+ y.d = r;
970
+ y.e = getBase10Exponent(r, e);
971
+ return external ? finalise(y, Ctor.precision, Ctor.rounding) : y;
972
+ };
973
+ P.toBinary = function(sd, rm) {
974
+ return toStringBinary(this, 2, sd, rm);
975
+ };
976
+ P.toDecimalPlaces = P.toDP = function(dp, rm) {
977
+ var x = this, Ctor = x.constructor;
978
+ x = new Ctor(x);
979
+ if (dp === void 0) return x;
980
+ checkInt32(dp, 0, MAX_DIGITS);
981
+ if (rm === void 0) rm = Ctor.rounding;
982
+ else checkInt32(rm, 0, 8);
983
+ return finalise(x, dp + x.e + 1, rm);
984
+ };
985
+ P.toExponential = function(dp, rm) {
986
+ var str, x = this, Ctor = x.constructor;
987
+ if (dp === void 0) {
988
+ str = finiteToString(x, true);
989
+ } else {
990
+ checkInt32(dp, 0, MAX_DIGITS);
991
+ if (rm === void 0) rm = Ctor.rounding;
992
+ else checkInt32(rm, 0, 8);
993
+ x = finalise(new Ctor(x), dp + 1, rm);
994
+ str = finiteToString(x, true, dp + 1);
995
+ }
996
+ return x.isNeg() && !x.isZero() ? "-" + str : str;
997
+ };
998
+ P.toFixed = function(dp, rm) {
999
+ var str, y, x = this, Ctor = x.constructor;
1000
+ if (dp === void 0) {
1001
+ str = finiteToString(x);
1002
+ } else {
1003
+ checkInt32(dp, 0, MAX_DIGITS);
1004
+ if (rm === void 0) rm = Ctor.rounding;
1005
+ else checkInt32(rm, 0, 8);
1006
+ y = finalise(new Ctor(x), dp + x.e + 1, rm);
1007
+ str = finiteToString(y, false, dp + y.e + 1);
1008
+ }
1009
+ return x.isNeg() && !x.isZero() ? "-" + str : str;
1010
+ };
1011
+ P.toFraction = function(maxD) {
1012
+ var d, d0, d1, d2, e, k, n, n0, n1, pr, q, r, x = this, xd = x.d, Ctor = x.constructor;
1013
+ if (!xd) return new Ctor(x);
1014
+ n1 = d0 = new Ctor(1);
1015
+ d1 = n0 = new Ctor(0);
1016
+ d = new Ctor(d1);
1017
+ e = d.e = getPrecision(xd) - x.e - 1;
1018
+ k = e % LOG_BASE;
1019
+ d.d[0] = mathpow(10, k < 0 ? LOG_BASE + k : k);
1020
+ if (maxD == null) {
1021
+ maxD = e > 0 ? d : n1;
1022
+ } else {
1023
+ n = new Ctor(maxD);
1024
+ if (!n.isInt() || n.lt(n1)) throw Error(invalidArgument + n);
1025
+ maxD = n.gt(d) ? e > 0 ? d : n1 : n;
1026
+ }
1027
+ external = false;
1028
+ n = new Ctor(digitsToString(xd));
1029
+ pr = Ctor.precision;
1030
+ Ctor.precision = e = xd.length * LOG_BASE * 2;
1031
+ for (; ; ) {
1032
+ q = divide(n, d, 0, 1, 1);
1033
+ d2 = d0.plus(q.times(d1));
1034
+ if (d2.cmp(maxD) == 1) break;
1035
+ d0 = d1;
1036
+ d1 = d2;
1037
+ d2 = n1;
1038
+ n1 = n0.plus(q.times(d2));
1039
+ n0 = d2;
1040
+ d2 = d;
1041
+ d = n.minus(q.times(d2));
1042
+ n = d2;
1043
+ }
1044
+ d2 = divide(maxD.minus(d0), d1, 0, 1, 1);
1045
+ n0 = n0.plus(d2.times(n1));
1046
+ d0 = d0.plus(d2.times(d1));
1047
+ n0.s = n1.s = x.s;
1048
+ r = divide(n1, d1, e, 1).minus(x).abs().cmp(divide(n0, d0, e, 1).minus(x).abs()) < 1 ? [n1, d1] : [n0, d0];
1049
+ Ctor.precision = pr;
1050
+ external = true;
1051
+ return r;
1052
+ };
1053
+ P.toHexadecimal = P.toHex = function(sd, rm) {
1054
+ return toStringBinary(this, 16, sd, rm);
1055
+ };
1056
+ P.toNearest = function(y, rm) {
1057
+ var x = this, Ctor = x.constructor;
1058
+ x = new Ctor(x);
1059
+ if (y == null) {
1060
+ if (!x.d) return x;
1061
+ y = new Ctor(1);
1062
+ rm = Ctor.rounding;
1063
+ } else {
1064
+ y = new Ctor(y);
1065
+ if (rm === void 0) {
1066
+ rm = Ctor.rounding;
1067
+ } else {
1068
+ checkInt32(rm, 0, 8);
1069
+ }
1070
+ if (!x.d) return y.s ? x : y;
1071
+ if (!y.d) {
1072
+ if (y.s) y.s = x.s;
1073
+ return y;
1074
+ }
1075
+ }
1076
+ if (y.d[0]) {
1077
+ external = false;
1078
+ x = divide(x, y, 0, rm, 1).times(y);
1079
+ external = true;
1080
+ finalise(x);
1081
+ } else {
1082
+ y.s = x.s;
1083
+ x = y;
1084
+ }
1085
+ return x;
1086
+ };
1087
+ P.toNumber = function() {
1088
+ return +this;
1089
+ };
1090
+ P.toOctal = function(sd, rm) {
1091
+ return toStringBinary(this, 8, sd, rm);
1092
+ };
1093
+ P.toPower = P.pow = function(y) {
1094
+ var e, k, pr, r, rm, s, x = this, Ctor = x.constructor, yn = +(y = new Ctor(y));
1095
+ if (!x.d || !y.d || !x.d[0] || !y.d[0]) return new Ctor(mathpow(+x, yn));
1096
+ x = new Ctor(x);
1097
+ if (x.eq(1)) return x;
1098
+ pr = Ctor.precision;
1099
+ rm = Ctor.rounding;
1100
+ if (y.eq(1)) return finalise(x, pr, rm);
1101
+ e = mathfloor(y.e / LOG_BASE);
1102
+ if (e >= y.d.length - 1 && (k = yn < 0 ? -yn : yn) <= MAX_SAFE_INTEGER) {
1103
+ r = intPow(Ctor, x, k, pr);
1104
+ return y.s < 0 ? new Ctor(1).div(r) : finalise(r, pr, rm);
1105
+ }
1106
+ s = x.s;
1107
+ if (s < 0) {
1108
+ if (e < y.d.length - 1) return new Ctor(NaN);
1109
+ if ((y.d[e] & 1) == 0) s = 1;
1110
+ if (x.e == 0 && x.d[0] == 1 && x.d.length == 1) {
1111
+ x.s = s;
1112
+ return x;
1113
+ }
1114
+ }
1115
+ k = mathpow(+x, yn);
1116
+ e = k == 0 || !isFinite(k) ? mathfloor(yn * (Math.log("0." + digitsToString(x.d)) / Math.LN10 + x.e + 1)) : new Ctor(k + "").e;
1117
+ if (e > Ctor.maxE + 1 || e < Ctor.minE - 1) return new Ctor(e > 0 ? s / 0 : 0);
1118
+ external = false;
1119
+ Ctor.rounding = x.s = 1;
1120
+ k = Math.min(12, (e + "").length);
1121
+ r = naturalExponential(y.times(naturalLogarithm(x, pr + k)), pr);
1122
+ if (r.d) {
1123
+ r = finalise(r, pr + 5, 1);
1124
+ if (checkRoundingDigits(r.d, pr, rm)) {
1125
+ e = pr + 10;
1126
+ r = finalise(naturalExponential(y.times(naturalLogarithm(x, e + k)), e), e + 5, 1);
1127
+ if (+digitsToString(r.d).slice(pr + 1, pr + 15) + 1 == 1e14) {
1128
+ r = finalise(r, pr + 1, 0);
1129
+ }
1130
+ }
1131
+ }
1132
+ r.s = s;
1133
+ external = true;
1134
+ Ctor.rounding = rm;
1135
+ return finalise(r, pr, rm);
1136
+ };
1137
+ P.toPrecision = function(sd, rm) {
1138
+ var str, x = this, Ctor = x.constructor;
1139
+ if (sd === void 0) {
1140
+ str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
1141
+ } else {
1142
+ checkInt32(sd, 1, MAX_DIGITS);
1143
+ if (rm === void 0) rm = Ctor.rounding;
1144
+ else checkInt32(rm, 0, 8);
1145
+ x = finalise(new Ctor(x), sd, rm);
1146
+ str = finiteToString(x, sd <= x.e || x.e <= Ctor.toExpNeg, sd);
1147
+ }
1148
+ return x.isNeg() && !x.isZero() ? "-" + str : str;
1149
+ };
1150
+ P.toSignificantDigits = P.toSD = function(sd, rm) {
1151
+ var x = this, Ctor = x.constructor;
1152
+ if (sd === void 0) {
1153
+ sd = Ctor.precision;
1154
+ rm = Ctor.rounding;
1155
+ } else {
1156
+ checkInt32(sd, 1, MAX_DIGITS);
1157
+ if (rm === void 0) rm = Ctor.rounding;
1158
+ else checkInt32(rm, 0, 8);
1159
+ }
1160
+ return finalise(new Ctor(x), sd, rm);
1161
+ };
1162
+ P.toString = function() {
1163
+ var x = this, Ctor = x.constructor, str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
1164
+ return x.isNeg() && !x.isZero() ? "-" + str : str;
1165
+ };
1166
+ P.truncated = P.trunc = function() {
1167
+ return finalise(new this.constructor(this), this.e + 1, 1);
1168
+ };
1169
+ P.valueOf = P.toJSON = function() {
1170
+ var x = this, Ctor = x.constructor, str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
1171
+ return x.isNeg() ? "-" + str : str;
1172
+ };
1173
+ function digitsToString(d) {
1174
+ var i, k, ws, indexOfLastWord = d.length - 1, str = "", w = d[0];
1175
+ if (indexOfLastWord > 0) {
1176
+ str += w;
1177
+ for (i = 1; i < indexOfLastWord; i++) {
1178
+ ws = d[i] + "";
1179
+ k = LOG_BASE - ws.length;
1180
+ if (k) str += getZeroString(k);
1181
+ str += ws;
1182
+ }
1183
+ w = d[i];
1184
+ ws = w + "";
1185
+ k = LOG_BASE - ws.length;
1186
+ if (k) str += getZeroString(k);
1187
+ } else if (w === 0) {
1188
+ return "0";
1189
+ }
1190
+ for (; w % 10 === 0; ) w /= 10;
1191
+ return str + w;
1192
+ }
1193
+ function checkInt32(i, min2, max2) {
1194
+ if (i !== ~~i || i < min2 || i > max2) {
1195
+ throw Error(invalidArgument + i);
1196
+ }
1197
+ }
1198
+ function checkRoundingDigits(d, i, rm, repeating) {
1199
+ var di, k, r, rd;
1200
+ for (k = d[0]; k >= 10; k /= 10) --i;
1201
+ if (--i < 0) {
1202
+ i += LOG_BASE;
1203
+ di = 0;
1204
+ } else {
1205
+ di = Math.ceil((i + 1) / LOG_BASE);
1206
+ i %= LOG_BASE;
1207
+ }
1208
+ k = mathpow(10, LOG_BASE - i);
1209
+ rd = d[di] % k | 0;
1210
+ if (repeating == null) {
1211
+ if (i < 3) {
1212
+ if (i == 0) rd = rd / 100 | 0;
1213
+ else if (i == 1) rd = rd / 10 | 0;
1214
+ r = rm < 4 && rd == 99999 || rm > 3 && rd == 49999 || rd == 5e4 || rd == 0;
1215
+ } else {
1216
+ r = (rm < 4 && rd + 1 == k || rm > 3 && rd + 1 == k / 2) && (d[di + 1] / k / 100 | 0) == mathpow(10, i - 2) - 1 || (rd == k / 2 || rd == 0) && (d[di + 1] / k / 100 | 0) == 0;
1217
+ }
1218
+ } else {
1219
+ if (i < 4) {
1220
+ if (i == 0) rd = rd / 1e3 | 0;
1221
+ else if (i == 1) rd = rd / 100 | 0;
1222
+ else if (i == 2) rd = rd / 10 | 0;
1223
+ r = (repeating || rm < 4) && rd == 9999 || !repeating && rm > 3 && rd == 4999;
1224
+ } else {
1225
+ r = ((repeating || rm < 4) && rd + 1 == k || !repeating && rm > 3 && rd + 1 == k / 2) && (d[di + 1] / k / 1e3 | 0) == mathpow(10, i - 3) - 1;
1226
+ }
1227
+ }
1228
+ return r;
1229
+ }
1230
+ function convertBase(str, baseIn, baseOut) {
1231
+ var j, arr = [0], arrL, i = 0, strL = str.length;
1232
+ for (; i < strL; ) {
1233
+ for (arrL = arr.length; arrL--; ) arr[arrL] *= baseIn;
1234
+ arr[0] += NUMERALS.indexOf(str.charAt(i++));
1235
+ for (j = 0; j < arr.length; j++) {
1236
+ if (arr[j] > baseOut - 1) {
1237
+ if (arr[j + 1] === void 0) arr[j + 1] = 0;
1238
+ arr[j + 1] += arr[j] / baseOut | 0;
1239
+ arr[j] %= baseOut;
1240
+ }
1241
+ }
1242
+ }
1243
+ return arr.reverse();
1244
+ }
1245
+ function cosine(Ctor, x) {
1246
+ var k, len, y;
1247
+ if (x.isZero()) return x;
1248
+ len = x.d.length;
1249
+ if (len < 32) {
1250
+ k = Math.ceil(len / 3);
1251
+ y = (1 / tinyPow(4, k)).toString();
1252
+ } else {
1253
+ k = 16;
1254
+ y = "2.3283064365386962890625e-10";
1255
+ }
1256
+ Ctor.precision += k;
1257
+ x = taylorSeries(Ctor, 1, x.times(y), new Ctor(1));
1258
+ for (var i = k; i--; ) {
1259
+ var cos2x = x.times(x);
1260
+ x = cos2x.times(cos2x).minus(cos2x).times(8).plus(1);
1261
+ }
1262
+ Ctor.precision -= k;
1263
+ return x;
1264
+ }
1265
+ var divide = /* @__PURE__ */ function() {
1266
+ function multiplyInteger(x, k, base) {
1267
+ var temp, carry = 0, i = x.length;
1268
+ for (x = x.slice(); i--; ) {
1269
+ temp = x[i] * k + carry;
1270
+ x[i] = temp % base | 0;
1271
+ carry = temp / base | 0;
1272
+ }
1273
+ if (carry) x.unshift(carry);
1274
+ return x;
1275
+ }
1276
+ function compare(a, b, aL, bL) {
1277
+ var i, r;
1278
+ if (aL != bL) {
1279
+ r = aL > bL ? 1 : -1;
1280
+ } else {
1281
+ for (i = r = 0; i < aL; i++) {
1282
+ if (a[i] != b[i]) {
1283
+ r = a[i] > b[i] ? 1 : -1;
1284
+ break;
1285
+ }
1286
+ }
1287
+ }
1288
+ return r;
1289
+ }
1290
+ function subtract(a, b, aL, base) {
1291
+ var i = 0;
1292
+ for (; aL--; ) {
1293
+ a[aL] -= i;
1294
+ i = a[aL] < b[aL] ? 1 : 0;
1295
+ a[aL] = i * base + a[aL] - b[aL];
1296
+ }
1297
+ for (; !a[0] && a.length > 1; ) a.shift();
1298
+ }
1299
+ return function(x, y, pr, rm, dp, base) {
1300
+ var cmp, e, i, k, logBase, more, prod, prodL, q, qd, rem, remL, rem0, sd, t, xi, xL, yd0, yL, yz, Ctor = x.constructor, sign2 = x.s == y.s ? 1 : -1, xd = x.d, yd = y.d;
1301
+ if (!xd || !xd[0] || !yd || !yd[0]) {
1302
+ return new Ctor(
1303
+ // Return NaN if either NaN, or both Infinity or 0.
1304
+ !x.s || !y.s || (xd ? yd && xd[0] == yd[0] : !yd) ? NaN : (
1305
+ // Return ±0 if x is 0 or y is ±Infinity, or return ±Infinity as y is 0.
1306
+ xd && xd[0] == 0 || !yd ? sign2 * 0 : sign2 / 0
1307
+ )
1308
+ );
1309
+ }
1310
+ if (base) {
1311
+ logBase = 1;
1312
+ e = x.e - y.e;
1313
+ } else {
1314
+ base = BASE;
1315
+ logBase = LOG_BASE;
1316
+ e = mathfloor(x.e / logBase) - mathfloor(y.e / logBase);
1317
+ }
1318
+ yL = yd.length;
1319
+ xL = xd.length;
1320
+ q = new Ctor(sign2);
1321
+ qd = q.d = [];
1322
+ for (i = 0; yd[i] == (xd[i] || 0); i++) ;
1323
+ if (yd[i] > (xd[i] || 0)) e--;
1324
+ if (pr == null) {
1325
+ sd = pr = Ctor.precision;
1326
+ rm = Ctor.rounding;
1327
+ } else if (dp) {
1328
+ sd = pr + (x.e - y.e) + 1;
1329
+ } else {
1330
+ sd = pr;
1331
+ }
1332
+ if (sd < 0) {
1333
+ qd.push(1);
1334
+ more = true;
1335
+ } else {
1336
+ sd = sd / logBase + 2 | 0;
1337
+ i = 0;
1338
+ if (yL == 1) {
1339
+ k = 0;
1340
+ yd = yd[0];
1341
+ sd++;
1342
+ for (; (i < xL || k) && sd--; i++) {
1343
+ t = k * base + (xd[i] || 0);
1344
+ qd[i] = t / yd | 0;
1345
+ k = t % yd | 0;
1346
+ }
1347
+ more = k || i < xL;
1348
+ } else {
1349
+ k = base / (yd[0] + 1) | 0;
1350
+ if (k > 1) {
1351
+ yd = multiplyInteger(yd, k, base);
1352
+ xd = multiplyInteger(xd, k, base);
1353
+ yL = yd.length;
1354
+ xL = xd.length;
1355
+ }
1356
+ xi = yL;
1357
+ rem = xd.slice(0, yL);
1358
+ remL = rem.length;
1359
+ for (; remL < yL; ) rem[remL++] = 0;
1360
+ yz = yd.slice();
1361
+ yz.unshift(0);
1362
+ yd0 = yd[0];
1363
+ if (yd[1] >= base / 2) ++yd0;
1364
+ do {
1365
+ k = 0;
1366
+ cmp = compare(yd, rem, yL, remL);
1367
+ if (cmp < 0) {
1368
+ rem0 = rem[0];
1369
+ if (yL != remL) rem0 = rem0 * base + (rem[1] || 0);
1370
+ k = rem0 / yd0 | 0;
1371
+ if (k > 1) {
1372
+ if (k >= base) k = base - 1;
1373
+ prod = multiplyInteger(yd, k, base);
1374
+ prodL = prod.length;
1375
+ remL = rem.length;
1376
+ cmp = compare(prod, rem, prodL, remL);
1377
+ if (cmp == 1) {
1378
+ k--;
1379
+ subtract(prod, yL < prodL ? yz : yd, prodL, base);
1380
+ }
1381
+ } else {
1382
+ if (k == 0) cmp = k = 1;
1383
+ prod = yd.slice();
1384
+ }
1385
+ prodL = prod.length;
1386
+ if (prodL < remL) prod.unshift(0);
1387
+ subtract(rem, prod, remL, base);
1388
+ if (cmp == -1) {
1389
+ remL = rem.length;
1390
+ cmp = compare(yd, rem, yL, remL);
1391
+ if (cmp < 1) {
1392
+ k++;
1393
+ subtract(rem, yL < remL ? yz : yd, remL, base);
1394
+ }
1395
+ }
1396
+ remL = rem.length;
1397
+ } else if (cmp === 0) {
1398
+ k++;
1399
+ rem = [0];
1400
+ }
1401
+ qd[i++] = k;
1402
+ if (cmp && rem[0]) {
1403
+ rem[remL++] = xd[xi] || 0;
1404
+ } else {
1405
+ rem = [xd[xi]];
1406
+ remL = 1;
1407
+ }
1408
+ } while ((xi++ < xL || rem[0] !== void 0) && sd--);
1409
+ more = rem[0] !== void 0;
1410
+ }
1411
+ if (!qd[0]) qd.shift();
1412
+ }
1413
+ if (logBase == 1) {
1414
+ q.e = e;
1415
+ inexact = more;
1416
+ } else {
1417
+ for (i = 1, k = qd[0]; k >= 10; k /= 10) i++;
1418
+ q.e = i + e * logBase - 1;
1419
+ finalise(q, dp ? pr + q.e + 1 : pr, rm, more);
1420
+ }
1421
+ return q;
1422
+ };
1423
+ }();
1424
+ function finalise(x, sd, rm, isTruncated) {
1425
+ var digits, i, j, k, rd, roundUp, w, xd, xdi, Ctor = x.constructor;
1426
+ out: if (sd != null) {
1427
+ xd = x.d;
1428
+ if (!xd) return x;
1429
+ for (digits = 1, k = xd[0]; k >= 10; k /= 10) digits++;
1430
+ i = sd - digits;
1431
+ if (i < 0) {
1432
+ i += LOG_BASE;
1433
+ j = sd;
1434
+ w = xd[xdi = 0];
1435
+ rd = w / mathpow(10, digits - j - 1) % 10 | 0;
1436
+ } else {
1437
+ xdi = Math.ceil((i + 1) / LOG_BASE);
1438
+ k = xd.length;
1439
+ if (xdi >= k) {
1440
+ if (isTruncated) {
1441
+ for (; k++ <= xdi; ) xd.push(0);
1442
+ w = rd = 0;
1443
+ digits = 1;
1444
+ i %= LOG_BASE;
1445
+ j = i - LOG_BASE + 1;
1446
+ } else {
1447
+ break out;
1448
+ }
1449
+ } else {
1450
+ w = k = xd[xdi];
1451
+ for (digits = 1; k >= 10; k /= 10) digits++;
1452
+ i %= LOG_BASE;
1453
+ j = i - LOG_BASE + digits;
1454
+ rd = j < 0 ? 0 : w / mathpow(10, digits - j - 1) % 10 | 0;
1455
+ }
1456
+ }
1457
+ isTruncated = isTruncated || sd < 0 || xd[xdi + 1] !== void 0 || (j < 0 ? w : w % mathpow(10, digits - j - 1));
1458
+ roundUp = rm < 4 ? (rd || isTruncated) && (rm == 0 || rm == (x.s < 0 ? 3 : 2)) : rd > 5 || rd == 5 && (rm == 4 || isTruncated || rm == 6 && // Check whether the digit to the left of the rounding digit is odd.
1459
+ (i > 0 ? j > 0 ? w / mathpow(10, digits - j) : 0 : xd[xdi - 1]) % 10 & 1 || rm == (x.s < 0 ? 8 : 7));
1460
+ if (sd < 1 || !xd[0]) {
1461
+ xd.length = 0;
1462
+ if (roundUp) {
1463
+ sd -= x.e + 1;
1464
+ xd[0] = mathpow(10, (LOG_BASE - sd % LOG_BASE) % LOG_BASE);
1465
+ x.e = -sd || 0;
1466
+ } else {
1467
+ xd[0] = x.e = 0;
1468
+ }
1469
+ return x;
1470
+ }
1471
+ if (i == 0) {
1472
+ xd.length = xdi;
1473
+ k = 1;
1474
+ xdi--;
1475
+ } else {
1476
+ xd.length = xdi + 1;
1477
+ k = mathpow(10, LOG_BASE - i);
1478
+ xd[xdi] = j > 0 ? (w / mathpow(10, digits - j) % mathpow(10, j) | 0) * k : 0;
1479
+ }
1480
+ if (roundUp) {
1481
+ for (; ; ) {
1482
+ if (xdi == 0) {
1483
+ for (i = 1, j = xd[0]; j >= 10; j /= 10) i++;
1484
+ j = xd[0] += k;
1485
+ for (k = 1; j >= 10; j /= 10) k++;
1486
+ if (i != k) {
1487
+ x.e++;
1488
+ if (xd[0] == BASE) xd[0] = 1;
1489
+ }
1490
+ break;
1491
+ } else {
1492
+ xd[xdi] += k;
1493
+ if (xd[xdi] != BASE) break;
1494
+ xd[xdi--] = 0;
1495
+ k = 1;
1496
+ }
1497
+ }
1498
+ }
1499
+ for (i = xd.length; xd[--i] === 0; ) xd.pop();
1500
+ }
1501
+ if (external) {
1502
+ if (x.e > Ctor.maxE) {
1503
+ x.d = null;
1504
+ x.e = NaN;
1505
+ } else if (x.e < Ctor.minE) {
1506
+ x.e = 0;
1507
+ x.d = [0];
1508
+ }
1509
+ }
1510
+ return x;
1511
+ }
1512
+ function finiteToString(x, isExp, sd) {
1513
+ if (!x.isFinite()) return nonFiniteToString(x);
1514
+ var k, e = x.e, str = digitsToString(x.d), len = str.length;
1515
+ if (isExp) {
1516
+ if (sd && (k = sd - len) > 0) {
1517
+ str = str.charAt(0) + "." + str.slice(1) + getZeroString(k);
1518
+ } else if (len > 1) {
1519
+ str = str.charAt(0) + "." + str.slice(1);
1520
+ }
1521
+ str = str + (x.e < 0 ? "e" : "e+") + x.e;
1522
+ } else if (e < 0) {
1523
+ str = "0." + getZeroString(-e - 1) + str;
1524
+ if (sd && (k = sd - len) > 0) str += getZeroString(k);
1525
+ } else if (e >= len) {
1526
+ str += getZeroString(e + 1 - len);
1527
+ if (sd && (k = sd - e - 1) > 0) str = str + "." + getZeroString(k);
1528
+ } else {
1529
+ if ((k = e + 1) < len) str = str.slice(0, k) + "." + str.slice(k);
1530
+ if (sd && (k = sd - len) > 0) {
1531
+ if (e + 1 === len) str += ".";
1532
+ str += getZeroString(k);
1533
+ }
1534
+ }
1535
+ return str;
1536
+ }
1537
+ function getBase10Exponent(digits, e) {
1538
+ var w = digits[0];
1539
+ for (e *= LOG_BASE; w >= 10; w /= 10) e++;
1540
+ return e;
1541
+ }
1542
+ function getLn10(Ctor, sd, pr) {
1543
+ if (sd > LN10_PRECISION) {
1544
+ external = true;
1545
+ if (pr) Ctor.precision = pr;
1546
+ throw Error(precisionLimitExceeded);
1547
+ }
1548
+ return finalise(new Ctor(LN10), sd, 1, true);
1549
+ }
1550
+ function getPi(Ctor, sd, rm) {
1551
+ if (sd > PI_PRECISION) throw Error(precisionLimitExceeded);
1552
+ return finalise(new Ctor(PI), sd, rm, true);
1553
+ }
1554
+ function getPrecision(digits) {
1555
+ var w = digits.length - 1, len = w * LOG_BASE + 1;
1556
+ w = digits[w];
1557
+ if (w) {
1558
+ for (; w % 10 == 0; w /= 10) len--;
1559
+ for (w = digits[0]; w >= 10; w /= 10) len++;
1560
+ }
1561
+ return len;
1562
+ }
1563
+ function getZeroString(k) {
1564
+ var zs = "";
1565
+ for (; k--; ) zs += "0";
1566
+ return zs;
1567
+ }
1568
+ function intPow(Ctor, x, n, pr) {
1569
+ var isTruncated, r = new Ctor(1), k = Math.ceil(pr / LOG_BASE + 4);
1570
+ external = false;
1571
+ for (; ; ) {
1572
+ if (n % 2) {
1573
+ r = r.times(x);
1574
+ if (truncate(r.d, k)) isTruncated = true;
1575
+ }
1576
+ n = mathfloor(n / 2);
1577
+ if (n === 0) {
1578
+ n = r.d.length - 1;
1579
+ if (isTruncated && r.d[n] === 0) ++r.d[n];
1580
+ break;
1581
+ }
1582
+ x = x.times(x);
1583
+ truncate(x.d, k);
1584
+ }
1585
+ external = true;
1586
+ return r;
1587
+ }
1588
+ function isOdd(n) {
1589
+ return n.d[n.d.length - 1] & 1;
1590
+ }
1591
+ function maxOrMin(Ctor, args, n) {
1592
+ var k, y, x = new Ctor(args[0]), i = 0;
1593
+ for (; ++i < args.length; ) {
1594
+ y = new Ctor(args[i]);
1595
+ if (!y.s) {
1596
+ x = y;
1597
+ break;
1598
+ }
1599
+ k = x.cmp(y);
1600
+ if (k === n || k === 0 && x.s === n) {
1601
+ x = y;
1602
+ }
1603
+ }
1604
+ return x;
1605
+ }
1606
+ function naturalExponential(x, sd) {
1607
+ var denominator, guard, j, pow2, sum2, t, wpr, rep = 0, i = 0, k = 0, Ctor = x.constructor, rm = Ctor.rounding, pr = Ctor.precision;
1608
+ if (!x.d || !x.d[0] || x.e > 17) {
1609
+ return new Ctor(x.d ? !x.d[0] ? 1 : x.s < 0 ? 0 : 1 / 0 : x.s ? x.s < 0 ? 0 : x : 0 / 0);
1610
+ }
1611
+ if (sd == null) {
1612
+ external = false;
1613
+ wpr = pr;
1614
+ } else {
1615
+ wpr = sd;
1616
+ }
1617
+ t = new Ctor(0.03125);
1618
+ while (x.e > -2) {
1619
+ x = x.times(t);
1620
+ k += 5;
1621
+ }
1622
+ guard = Math.log(mathpow(2, k)) / Math.LN10 * 2 + 5 | 0;
1623
+ wpr += guard;
1624
+ denominator = pow2 = sum2 = new Ctor(1);
1625
+ Ctor.precision = wpr;
1626
+ for (; ; ) {
1627
+ pow2 = finalise(pow2.times(x), wpr, 1);
1628
+ denominator = denominator.times(++i);
1629
+ t = sum2.plus(divide(pow2, denominator, wpr, 1));
1630
+ if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum2.d).slice(0, wpr)) {
1631
+ j = k;
1632
+ while (j--) sum2 = finalise(sum2.times(sum2), wpr, 1);
1633
+ if (sd == null) {
1634
+ if (rep < 3 && checkRoundingDigits(sum2.d, wpr - guard, rm, rep)) {
1635
+ Ctor.precision = wpr += 10;
1636
+ denominator = pow2 = t = new Ctor(1);
1637
+ i = 0;
1638
+ rep++;
1639
+ } else {
1640
+ return finalise(sum2, Ctor.precision = pr, rm, external = true);
1641
+ }
1642
+ } else {
1643
+ Ctor.precision = pr;
1644
+ return sum2;
1645
+ }
1646
+ }
1647
+ sum2 = t;
1648
+ }
1649
+ }
1650
+ function naturalLogarithm(y, sd) {
1651
+ var c, c0, denominator, e, numerator, rep, sum2, t, wpr, x1, x2, n = 1, guard = 10, x = y, xd = x.d, Ctor = x.constructor, rm = Ctor.rounding, pr = Ctor.precision;
1652
+ if (x.s < 0 || !xd || !xd[0] || !x.e && xd[0] == 1 && xd.length == 1) {
1653
+ return new Ctor(xd && !xd[0] ? -1 / 0 : x.s != 1 ? NaN : xd ? 0 : x);
1654
+ }
1655
+ if (sd == null) {
1656
+ external = false;
1657
+ wpr = pr;
1658
+ } else {
1659
+ wpr = sd;
1660
+ }
1661
+ Ctor.precision = wpr += guard;
1662
+ c = digitsToString(xd);
1663
+ c0 = c.charAt(0);
1664
+ if (Math.abs(e = x.e) < 15e14) {
1665
+ while (c0 < 7 && c0 != 1 || c0 == 1 && c.charAt(1) > 3) {
1666
+ x = x.times(y);
1667
+ c = digitsToString(x.d);
1668
+ c0 = c.charAt(0);
1669
+ n++;
1670
+ }
1671
+ e = x.e;
1672
+ if (c0 > 1) {
1673
+ x = new Ctor("0." + c);
1674
+ e++;
1675
+ } else {
1676
+ x = new Ctor(c0 + "." + c.slice(1));
1677
+ }
1678
+ } else {
1679
+ t = getLn10(Ctor, wpr + 2, pr).times(e + "");
1680
+ x = naturalLogarithm(new Ctor(c0 + "." + c.slice(1)), wpr - guard).plus(t);
1681
+ Ctor.precision = pr;
1682
+ return sd == null ? finalise(x, pr, rm, external = true) : x;
1683
+ }
1684
+ x1 = x;
1685
+ sum2 = numerator = x = divide(x.minus(1), x.plus(1), wpr, 1);
1686
+ x2 = finalise(x.times(x), wpr, 1);
1687
+ denominator = 3;
1688
+ for (; ; ) {
1689
+ numerator = finalise(numerator.times(x2), wpr, 1);
1690
+ t = sum2.plus(divide(numerator, new Ctor(denominator), wpr, 1));
1691
+ if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum2.d).slice(0, wpr)) {
1692
+ sum2 = sum2.times(2);
1693
+ if (e !== 0) sum2 = sum2.plus(getLn10(Ctor, wpr + 2, pr).times(e + ""));
1694
+ sum2 = divide(sum2, new Ctor(n), wpr, 1);
1695
+ if (sd == null) {
1696
+ if (checkRoundingDigits(sum2.d, wpr - guard, rm, rep)) {
1697
+ Ctor.precision = wpr += guard;
1698
+ t = numerator = x = divide(x1.minus(1), x1.plus(1), wpr, 1);
1699
+ x2 = finalise(x.times(x), wpr, 1);
1700
+ denominator = rep = 1;
1701
+ } else {
1702
+ return finalise(sum2, Ctor.precision = pr, rm, external = true);
1703
+ }
1704
+ } else {
1705
+ Ctor.precision = pr;
1706
+ return sum2;
1707
+ }
1708
+ }
1709
+ sum2 = t;
1710
+ denominator += 2;
1711
+ }
1712
+ }
1713
+ function nonFiniteToString(x) {
1714
+ return String(x.s * x.s / 0);
1715
+ }
1716
+ function parseDecimal(x, str) {
1717
+ var e, i, len;
1718
+ if ((e = str.indexOf(".")) > -1) str = str.replace(".", "");
1719
+ if ((i = str.search(/e/i)) > 0) {
1720
+ if (e < 0) e = i;
1721
+ e += +str.slice(i + 1);
1722
+ str = str.substring(0, i);
1723
+ } else if (e < 0) {
1724
+ e = str.length;
1725
+ }
1726
+ for (i = 0; str.charCodeAt(i) === 48; i++) ;
1727
+ for (len = str.length; str.charCodeAt(len - 1) === 48; --len) ;
1728
+ str = str.slice(i, len);
1729
+ if (str) {
1730
+ len -= i;
1731
+ x.e = e = e - i - 1;
1732
+ x.d = [];
1733
+ i = (e + 1) % LOG_BASE;
1734
+ if (e < 0) i += LOG_BASE;
1735
+ if (i < len) {
1736
+ if (i) x.d.push(+str.slice(0, i));
1737
+ for (len -= LOG_BASE; i < len; ) x.d.push(+str.slice(i, i += LOG_BASE));
1738
+ str = str.slice(i);
1739
+ i = LOG_BASE - str.length;
1740
+ } else {
1741
+ i -= len;
1742
+ }
1743
+ for (; i--; ) str += "0";
1744
+ x.d.push(+str);
1745
+ if (external) {
1746
+ if (x.e > x.constructor.maxE) {
1747
+ x.d = null;
1748
+ x.e = NaN;
1749
+ } else if (x.e < x.constructor.minE) {
1750
+ x.e = 0;
1751
+ x.d = [0];
1752
+ }
1753
+ }
1754
+ } else {
1755
+ x.e = 0;
1756
+ x.d = [0];
1757
+ }
1758
+ return x;
1759
+ }
1760
+ function parseOther(x, str) {
1761
+ var base, Ctor, divisor, i, isFloat, len, p, xd, xe;
1762
+ if (str.indexOf("_") > -1) {
1763
+ str = str.replace(/(\d)_(?=\d)/g, "$1");
1764
+ if (isDecimal.test(str)) return parseDecimal(x, str);
1765
+ } else if (str === "Infinity" || str === "NaN") {
1766
+ if (!+str) x.s = NaN;
1767
+ x.e = NaN;
1768
+ x.d = null;
1769
+ return x;
1770
+ }
1771
+ if (isHex.test(str)) {
1772
+ base = 16;
1773
+ str = str.toLowerCase();
1774
+ } else if (isBinary.test(str)) {
1775
+ base = 2;
1776
+ } else if (isOctal.test(str)) {
1777
+ base = 8;
1778
+ } else {
1779
+ throw Error(invalidArgument + str);
1780
+ }
1781
+ i = str.search(/p/i);
1782
+ if (i > 0) {
1783
+ p = +str.slice(i + 1);
1784
+ str = str.substring(2, i);
1785
+ } else {
1786
+ str = str.slice(2);
1787
+ }
1788
+ i = str.indexOf(".");
1789
+ isFloat = i >= 0;
1790
+ Ctor = x.constructor;
1791
+ if (isFloat) {
1792
+ str = str.replace(".", "");
1793
+ len = str.length;
1794
+ i = len - i;
1795
+ divisor = intPow(Ctor, new Ctor(base), i, i * 2);
1796
+ }
1797
+ xd = convertBase(str, base, BASE);
1798
+ xe = xd.length - 1;
1799
+ for (i = xe; xd[i] === 0; --i) xd.pop();
1800
+ if (i < 0) return new Ctor(x.s * 0);
1801
+ x.e = getBase10Exponent(xd, xe);
1802
+ x.d = xd;
1803
+ external = false;
1804
+ if (isFloat) x = divide(x, divisor, len * 4);
1805
+ if (p) x = x.times(Math.abs(p) < 54 ? mathpow(2, p) : Decimal.pow(2, p));
1806
+ external = true;
1807
+ return x;
1808
+ }
1809
+ function sine(Ctor, x) {
1810
+ var k, len = x.d.length;
1811
+ if (len < 3) {
1812
+ return x.isZero() ? x : taylorSeries(Ctor, 2, x, x);
1813
+ }
1814
+ k = 1.4 * Math.sqrt(len);
1815
+ k = k > 16 ? 16 : k | 0;
1816
+ x = x.times(1 / tinyPow(5, k));
1817
+ x = taylorSeries(Ctor, 2, x, x);
1818
+ var sin2_x, d5 = new Ctor(5), d16 = new Ctor(16), d20 = new Ctor(20);
1819
+ for (; k--; ) {
1820
+ sin2_x = x.times(x);
1821
+ x = x.times(d5.plus(sin2_x.times(d16.times(sin2_x).minus(d20))));
1822
+ }
1823
+ return x;
1824
+ }
1825
+ function taylorSeries(Ctor, n, x, y, isHyperbolic) {
1826
+ var j, t, u, x2, i = 1, pr = Ctor.precision, k = Math.ceil(pr / LOG_BASE);
1827
+ external = false;
1828
+ x2 = x.times(x);
1829
+ u = new Ctor(y);
1830
+ for (; ; ) {
1831
+ t = divide(u.times(x2), new Ctor(n++ * n++), pr, 1);
1832
+ u = isHyperbolic ? y.plus(t) : y.minus(t);
1833
+ y = divide(t.times(x2), new Ctor(n++ * n++), pr, 1);
1834
+ t = u.plus(y);
1835
+ if (t.d[k] !== void 0) {
1836
+ for (j = k; t.d[j] === u.d[j] && j--; ) ;
1837
+ if (j == -1) break;
1838
+ }
1839
+ j = u;
1840
+ u = y;
1841
+ y = t;
1842
+ t = j;
1843
+ i++;
1844
+ }
1845
+ external = true;
1846
+ t.d.length = k + 1;
1847
+ return t;
1848
+ }
1849
+ function tinyPow(b, e) {
1850
+ var n = b;
1851
+ while (--e) n *= b;
1852
+ return n;
1853
+ }
1854
+ function toLessThanHalfPi(Ctor, x) {
1855
+ var t, isNeg = x.s < 0, pi = getPi(Ctor, Ctor.precision, 1), halfPi = pi.times(0.5);
1856
+ x = x.abs();
1857
+ if (x.lte(halfPi)) {
1858
+ quadrant = isNeg ? 4 : 1;
1859
+ return x;
1860
+ }
1861
+ t = x.divToInt(pi);
1862
+ if (t.isZero()) {
1863
+ quadrant = isNeg ? 3 : 2;
1864
+ } else {
1865
+ x = x.minus(t.times(pi));
1866
+ if (x.lte(halfPi)) {
1867
+ quadrant = isOdd(t) ? isNeg ? 2 : 3 : isNeg ? 4 : 1;
1868
+ return x;
1869
+ }
1870
+ quadrant = isOdd(t) ? isNeg ? 1 : 4 : isNeg ? 3 : 2;
1871
+ }
1872
+ return x.minus(pi).abs();
1873
+ }
1874
+ function toStringBinary(x, baseOut, sd, rm) {
1875
+ var base, e, i, k, len, roundUp, str, xd, y, Ctor = x.constructor, isExp = sd !== void 0;
1876
+ if (isExp) {
1877
+ checkInt32(sd, 1, MAX_DIGITS);
1878
+ if (rm === void 0) rm = Ctor.rounding;
1879
+ else checkInt32(rm, 0, 8);
1880
+ } else {
1881
+ sd = Ctor.precision;
1882
+ rm = Ctor.rounding;
1883
+ }
1884
+ if (!x.isFinite()) {
1885
+ str = nonFiniteToString(x);
1886
+ } else {
1887
+ str = finiteToString(x);
1888
+ i = str.indexOf(".");
1889
+ if (isExp) {
1890
+ base = 2;
1891
+ if (baseOut == 16) {
1892
+ sd = sd * 4 - 3;
1893
+ } else if (baseOut == 8) {
1894
+ sd = sd * 3 - 2;
1895
+ }
1896
+ } else {
1897
+ base = baseOut;
1898
+ }
1899
+ if (i >= 0) {
1900
+ str = str.replace(".", "");
1901
+ y = new Ctor(1);
1902
+ y.e = str.length - i;
1903
+ y.d = convertBase(finiteToString(y), 10, base);
1904
+ y.e = y.d.length;
1905
+ }
1906
+ xd = convertBase(str, 10, base);
1907
+ e = len = xd.length;
1908
+ for (; xd[--len] == 0; ) xd.pop();
1909
+ if (!xd[0]) {
1910
+ str = isExp ? "0p+0" : "0";
1911
+ } else {
1912
+ if (i < 0) {
1913
+ e--;
1914
+ } else {
1915
+ x = new Ctor(x);
1916
+ x.d = xd;
1917
+ x.e = e;
1918
+ x = divide(x, y, sd, rm, 0, base);
1919
+ xd = x.d;
1920
+ e = x.e;
1921
+ roundUp = inexact;
1922
+ }
1923
+ i = xd[sd];
1924
+ k = base / 2;
1925
+ roundUp = roundUp || xd[sd + 1] !== void 0;
1926
+ roundUp = rm < 4 ? (i !== void 0 || roundUp) && (rm === 0 || rm === (x.s < 0 ? 3 : 2)) : i > k || i === k && (rm === 4 || roundUp || rm === 6 && xd[sd - 1] & 1 || rm === (x.s < 0 ? 8 : 7));
1927
+ xd.length = sd;
1928
+ if (roundUp) {
1929
+ for (; ++xd[--sd] > base - 1; ) {
1930
+ xd[sd] = 0;
1931
+ if (!sd) {
1932
+ ++e;
1933
+ xd.unshift(1);
1934
+ }
1935
+ }
1936
+ }
1937
+ for (len = xd.length; !xd[len - 1]; --len) ;
1938
+ for (i = 0, str = ""; i < len; i++) str += NUMERALS.charAt(xd[i]);
1939
+ if (isExp) {
1940
+ if (len > 1) {
1941
+ if (baseOut == 16 || baseOut == 8) {
1942
+ i = baseOut == 16 ? 4 : 3;
1943
+ for (--len; len % i; len++) str += "0";
1944
+ xd = convertBase(str, base, baseOut);
1945
+ for (len = xd.length; !xd[len - 1]; --len) ;
1946
+ for (i = 1, str = "1."; i < len; i++) str += NUMERALS.charAt(xd[i]);
1947
+ } else {
1948
+ str = str.charAt(0) + "." + str.slice(1);
1949
+ }
1950
+ }
1951
+ str = str + (e < 0 ? "p" : "p+") + e;
1952
+ } else if (e < 0) {
1953
+ for (; ++e; ) str = "0" + str;
1954
+ str = "0." + str;
1955
+ } else {
1956
+ if (++e > len) for (e -= len; e--; ) str += "0";
1957
+ else if (e < len) str = str.slice(0, e) + "." + str.slice(e);
1958
+ }
1959
+ }
1960
+ str = (baseOut == 16 ? "0x" : baseOut == 2 ? "0b" : baseOut == 8 ? "0o" : "") + str;
1961
+ }
1962
+ return x.s < 0 ? "-" + str : str;
1963
+ }
1964
+ function truncate(arr, len) {
1965
+ if (arr.length > len) {
1966
+ arr.length = len;
1967
+ return true;
1968
+ }
1969
+ }
1970
+ function abs(x) {
1971
+ return new this(x).abs();
1972
+ }
1973
+ function acos(x) {
1974
+ return new this(x).acos();
1975
+ }
1976
+ function acosh(x) {
1977
+ return new this(x).acosh();
1978
+ }
1979
+ function add(x, y) {
1980
+ return new this(x).plus(y);
1981
+ }
1982
+ function asin(x) {
1983
+ return new this(x).asin();
1984
+ }
1985
+ function asinh(x) {
1986
+ return new this(x).asinh();
1987
+ }
1988
+ function atan(x) {
1989
+ return new this(x).atan();
1990
+ }
1991
+ function atanh(x) {
1992
+ return new this(x).atanh();
1993
+ }
1994
+ function atan2(y, x) {
1995
+ y = new this(y);
1996
+ x = new this(x);
1997
+ var r, pr = this.precision, rm = this.rounding, wpr = pr + 4;
1998
+ if (!y.s || !x.s) {
1999
+ r = new this(NaN);
2000
+ } else if (!y.d && !x.d) {
2001
+ r = getPi(this, wpr, 1).times(x.s > 0 ? 0.25 : 0.75);
2002
+ r.s = y.s;
2003
+ } else if (!x.d || y.isZero()) {
2004
+ r = x.s < 0 ? getPi(this, pr, rm) : new this(0);
2005
+ r.s = y.s;
2006
+ } else if (!y.d || x.isZero()) {
2007
+ r = getPi(this, wpr, 1).times(0.5);
2008
+ r.s = y.s;
2009
+ } else if (x.s < 0) {
2010
+ this.precision = wpr;
2011
+ this.rounding = 1;
2012
+ r = this.atan(divide(y, x, wpr, 1));
2013
+ x = getPi(this, wpr, 1);
2014
+ this.precision = pr;
2015
+ this.rounding = rm;
2016
+ r = y.s < 0 ? r.minus(x) : r.plus(x);
2017
+ } else {
2018
+ r = this.atan(divide(y, x, wpr, 1));
2019
+ }
2020
+ return r;
2021
+ }
2022
+ function cbrt(x) {
2023
+ return new this(x).cbrt();
2024
+ }
2025
+ function ceil(x) {
2026
+ return finalise(x = new this(x), x.e + 1, 2);
2027
+ }
2028
+ function clamp(x, min2, max2) {
2029
+ return new this(x).clamp(min2, max2);
2030
+ }
2031
+ function config(obj) {
2032
+ if (!obj || typeof obj !== "object") throw Error(decimalError + "Object expected");
2033
+ var i, p, v, useDefaults = obj.defaults === true, ps = [
2034
+ "precision",
2035
+ 1,
2036
+ MAX_DIGITS,
2037
+ "rounding",
2038
+ 0,
2039
+ 8,
2040
+ "toExpNeg",
2041
+ -EXP_LIMIT,
2042
+ 0,
2043
+ "toExpPos",
2044
+ 0,
2045
+ EXP_LIMIT,
2046
+ "maxE",
2047
+ 0,
2048
+ EXP_LIMIT,
2049
+ "minE",
2050
+ -EXP_LIMIT,
2051
+ 0,
2052
+ "modulo",
2053
+ 0,
2054
+ 9
2055
+ ];
2056
+ for (i = 0; i < ps.length; i += 3) {
2057
+ if (p = ps[i], useDefaults) this[p] = DEFAULTS[p];
2058
+ if ((v = obj[p]) !== void 0) {
2059
+ if (mathfloor(v) === v && v >= ps[i + 1] && v <= ps[i + 2]) this[p] = v;
2060
+ else throw Error(invalidArgument + p + ": " + v);
2061
+ }
2062
+ }
2063
+ if (p = "crypto", useDefaults) this[p] = DEFAULTS[p];
2064
+ if ((v = obj[p]) !== void 0) {
2065
+ if (v === true || v === false || v === 0 || v === 1) {
2066
+ if (v) {
2067
+ if (typeof crypto != "undefined" && crypto && (crypto.getRandomValues || crypto.randomBytes)) {
2068
+ this[p] = true;
2069
+ } else {
2070
+ throw Error(cryptoUnavailable);
2071
+ }
2072
+ } else {
2073
+ this[p] = false;
2074
+ }
2075
+ } else {
2076
+ throw Error(invalidArgument + p + ": " + v);
2077
+ }
2078
+ }
2079
+ return this;
2080
+ }
2081
+ function cos(x) {
2082
+ return new this(x).cos();
2083
+ }
2084
+ function cosh(x) {
2085
+ return new this(x).cosh();
2086
+ }
2087
+ function clone(obj) {
2088
+ var i, p, ps;
2089
+ function Decimal2(v) {
2090
+ var e, i2, t, x = this;
2091
+ if (!(x instanceof Decimal2)) return new Decimal2(v);
2092
+ x.constructor = Decimal2;
2093
+ if (isDecimalInstance(v)) {
2094
+ x.s = v.s;
2095
+ if (external) {
2096
+ if (!v.d || v.e > Decimal2.maxE) {
2097
+ x.e = NaN;
2098
+ x.d = null;
2099
+ } else if (v.e < Decimal2.minE) {
2100
+ x.e = 0;
2101
+ x.d = [0];
2102
+ } else {
2103
+ x.e = v.e;
2104
+ x.d = v.d.slice();
2105
+ }
2106
+ } else {
2107
+ x.e = v.e;
2108
+ x.d = v.d ? v.d.slice() : v.d;
2109
+ }
2110
+ return;
2111
+ }
2112
+ t = typeof v;
2113
+ if (t === "number") {
2114
+ if (v === 0) {
2115
+ x.s = 1 / v < 0 ? -1 : 1;
2116
+ x.e = 0;
2117
+ x.d = [0];
2118
+ return;
2119
+ }
2120
+ if (v < 0) {
2121
+ v = -v;
2122
+ x.s = -1;
2123
+ } else {
2124
+ x.s = 1;
2125
+ }
2126
+ if (v === ~~v && v < 1e7) {
2127
+ for (e = 0, i2 = v; i2 >= 10; i2 /= 10) e++;
2128
+ if (external) {
2129
+ if (e > Decimal2.maxE) {
2130
+ x.e = NaN;
2131
+ x.d = null;
2132
+ } else if (e < Decimal2.minE) {
2133
+ x.e = 0;
2134
+ x.d = [0];
2135
+ } else {
2136
+ x.e = e;
2137
+ x.d = [v];
2138
+ }
2139
+ } else {
2140
+ x.e = e;
2141
+ x.d = [v];
2142
+ }
2143
+ return;
2144
+ }
2145
+ if (v * 0 !== 0) {
2146
+ if (!v) x.s = NaN;
2147
+ x.e = NaN;
2148
+ x.d = null;
2149
+ return;
2150
+ }
2151
+ return parseDecimal(x, v.toString());
2152
+ }
2153
+ if (t === "string") {
2154
+ if ((i2 = v.charCodeAt(0)) === 45) {
2155
+ v = v.slice(1);
2156
+ x.s = -1;
2157
+ } else {
2158
+ if (i2 === 43) v = v.slice(1);
2159
+ x.s = 1;
2160
+ }
2161
+ return isDecimal.test(v) ? parseDecimal(x, v) : parseOther(x, v);
2162
+ }
2163
+ if (t === "bigint") {
2164
+ if (v < 0) {
2165
+ v = -v;
2166
+ x.s = -1;
2167
+ } else {
2168
+ x.s = 1;
2169
+ }
2170
+ return parseDecimal(x, v.toString());
2171
+ }
2172
+ throw Error(invalidArgument + v);
2173
+ }
2174
+ Decimal2.prototype = P;
2175
+ Decimal2.ROUND_UP = 0;
2176
+ Decimal2.ROUND_DOWN = 1;
2177
+ Decimal2.ROUND_CEIL = 2;
2178
+ Decimal2.ROUND_FLOOR = 3;
2179
+ Decimal2.ROUND_HALF_UP = 4;
2180
+ Decimal2.ROUND_HALF_DOWN = 5;
2181
+ Decimal2.ROUND_HALF_EVEN = 6;
2182
+ Decimal2.ROUND_HALF_CEIL = 7;
2183
+ Decimal2.ROUND_HALF_FLOOR = 8;
2184
+ Decimal2.EUCLID = 9;
2185
+ Decimal2.config = Decimal2.set = config;
2186
+ Decimal2.clone = clone;
2187
+ Decimal2.isDecimal = isDecimalInstance;
2188
+ Decimal2.abs = abs;
2189
+ Decimal2.acos = acos;
2190
+ Decimal2.acosh = acosh;
2191
+ Decimal2.add = add;
2192
+ Decimal2.asin = asin;
2193
+ Decimal2.asinh = asinh;
2194
+ Decimal2.atan = atan;
2195
+ Decimal2.atanh = atanh;
2196
+ Decimal2.atan2 = atan2;
2197
+ Decimal2.cbrt = cbrt;
2198
+ Decimal2.ceil = ceil;
2199
+ Decimal2.clamp = clamp;
2200
+ Decimal2.cos = cos;
2201
+ Decimal2.cosh = cosh;
2202
+ Decimal2.div = div;
2203
+ Decimal2.exp = exp;
2204
+ Decimal2.floor = floor;
2205
+ Decimal2.hypot = hypot;
2206
+ Decimal2.ln = ln;
2207
+ Decimal2.log = log;
2208
+ Decimal2.log10 = log10;
2209
+ Decimal2.log2 = log2;
2210
+ Decimal2.max = max;
2211
+ Decimal2.min = min;
2212
+ Decimal2.mod = mod;
2213
+ Decimal2.mul = mul;
2214
+ Decimal2.pow = pow;
2215
+ Decimal2.random = random;
2216
+ Decimal2.round = round;
2217
+ Decimal2.sign = sign;
2218
+ Decimal2.sin = sin;
2219
+ Decimal2.sinh = sinh;
2220
+ Decimal2.sqrt = sqrt;
2221
+ Decimal2.sub = sub;
2222
+ Decimal2.sum = sum;
2223
+ Decimal2.tan = tan;
2224
+ Decimal2.tanh = tanh;
2225
+ Decimal2.trunc = trunc;
2226
+ if (obj === void 0) obj = {};
2227
+ if (obj) {
2228
+ if (obj.defaults !== true) {
2229
+ ps = ["precision", "rounding", "toExpNeg", "toExpPos", "maxE", "minE", "modulo", "crypto"];
2230
+ for (i = 0; i < ps.length; ) if (!obj.hasOwnProperty(p = ps[i++])) obj[p] = this[p];
2231
+ }
2232
+ }
2233
+ Decimal2.config(obj);
2234
+ return Decimal2;
2235
+ }
2236
+ function div(x, y) {
2237
+ return new this(x).div(y);
2238
+ }
2239
+ function exp(x) {
2240
+ return new this(x).exp();
2241
+ }
2242
+ function floor(x) {
2243
+ return finalise(x = new this(x), x.e + 1, 3);
2244
+ }
2245
+ function hypot() {
2246
+ var i, n, t = new this(0);
2247
+ external = false;
2248
+ for (i = 0; i < arguments.length; ) {
2249
+ n = new this(arguments[i++]);
2250
+ if (!n.d) {
2251
+ if (n.s) {
2252
+ external = true;
2253
+ return new this(1 / 0);
2254
+ }
2255
+ t = n;
2256
+ } else if (t.d) {
2257
+ t = t.plus(n.times(n));
2258
+ }
2259
+ }
2260
+ external = true;
2261
+ return t.sqrt();
2262
+ }
2263
+ function isDecimalInstance(obj) {
2264
+ return obj instanceof Decimal || obj && obj.toStringTag === tag || false;
2265
+ }
2266
+ function ln(x) {
2267
+ return new this(x).ln();
2268
+ }
2269
+ function log(x, y) {
2270
+ return new this(x).log(y);
2271
+ }
2272
+ function log2(x) {
2273
+ return new this(x).log(2);
2274
+ }
2275
+ function log10(x) {
2276
+ return new this(x).log(10);
2277
+ }
2278
+ function max() {
2279
+ return maxOrMin(this, arguments, -1);
2280
+ }
2281
+ function min() {
2282
+ return maxOrMin(this, arguments, 1);
2283
+ }
2284
+ function mod(x, y) {
2285
+ return new this(x).mod(y);
2286
+ }
2287
+ function mul(x, y) {
2288
+ return new this(x).mul(y);
2289
+ }
2290
+ function pow(x, y) {
2291
+ return new this(x).pow(y);
2292
+ }
2293
+ function random(sd) {
2294
+ var d, e, k, n, i = 0, r = new this(1), rd = [];
2295
+ if (sd === void 0) sd = this.precision;
2296
+ else checkInt32(sd, 1, MAX_DIGITS);
2297
+ k = Math.ceil(sd / LOG_BASE);
2298
+ if (!this.crypto) {
2299
+ for (; i < k; ) rd[i++] = Math.random() * 1e7 | 0;
2300
+ } else if (crypto.getRandomValues) {
2301
+ d = crypto.getRandomValues(new Uint32Array(k));
2302
+ for (; i < k; ) {
2303
+ n = d[i];
2304
+ if (n >= 429e7) {
2305
+ d[i] = crypto.getRandomValues(new Uint32Array(1))[0];
2306
+ } else {
2307
+ rd[i++] = n % 1e7;
2308
+ }
2309
+ }
2310
+ } else if (crypto.randomBytes) {
2311
+ d = crypto.randomBytes(k *= 4);
2312
+ for (; i < k; ) {
2313
+ n = d[i] + (d[i + 1] << 8) + (d[i + 2] << 16) + ((d[i + 3] & 127) << 24);
2314
+ if (n >= 214e7) {
2315
+ crypto.randomBytes(4).copy(d, i);
2316
+ } else {
2317
+ rd.push(n % 1e7);
2318
+ i += 4;
2319
+ }
2320
+ }
2321
+ i = k / 4;
2322
+ } else {
2323
+ throw Error(cryptoUnavailable);
2324
+ }
2325
+ k = rd[--i];
2326
+ sd %= LOG_BASE;
2327
+ if (k && sd) {
2328
+ n = mathpow(10, LOG_BASE - sd);
2329
+ rd[i] = (k / n | 0) * n;
2330
+ }
2331
+ for (; rd[i] === 0; i--) rd.pop();
2332
+ if (i < 0) {
2333
+ e = 0;
2334
+ rd = [0];
2335
+ } else {
2336
+ e = -1;
2337
+ for (; rd[0] === 0; e -= LOG_BASE) rd.shift();
2338
+ for (k = 1, n = rd[0]; n >= 10; n /= 10) k++;
2339
+ if (k < LOG_BASE) e -= LOG_BASE - k;
2340
+ }
2341
+ r.e = e;
2342
+ r.d = rd;
2343
+ return r;
2344
+ }
2345
+ function round(x) {
2346
+ return finalise(x = new this(x), x.e + 1, this.rounding);
2347
+ }
2348
+ function sign(x) {
2349
+ x = new this(x);
2350
+ return x.d ? x.d[0] ? x.s : 0 * x.s : x.s || NaN;
2351
+ }
2352
+ function sin(x) {
2353
+ return new this(x).sin();
2354
+ }
2355
+ function sinh(x) {
2356
+ return new this(x).sinh();
2357
+ }
2358
+ function sqrt(x) {
2359
+ return new this(x).sqrt();
2360
+ }
2361
+ function sub(x, y) {
2362
+ return new this(x).sub(y);
2363
+ }
2364
+ function sum() {
2365
+ var i = 0, args = arguments, x = new this(args[i]);
2366
+ external = false;
2367
+ for (; x.s && ++i < args.length; ) x = x.plus(args[i]);
2368
+ external = true;
2369
+ return finalise(x, this.precision, this.rounding);
2370
+ }
2371
+ function tan(x) {
2372
+ return new this(x).tan();
2373
+ }
2374
+ function tanh(x) {
2375
+ return new this(x).tanh();
2376
+ }
2377
+ function trunc(x) {
2378
+ return finalise(x = new this(x), x.e + 1, 1);
2379
+ }
2380
+ P[Symbol.for("nodejs.util.inspect.custom")] = P.toString;
2381
+ P[Symbol.toStringTag] = "Decimal";
2382
+ var Decimal = P.constructor = clone(DEFAULTS);
2383
+ LN10 = new Decimal(LN10);
2384
+ PI = new Decimal(PI);
2385
+ var decimal_default = Decimal;
2386
+
2387
+ // src/utils.ts
209
2388
  function bytesToHex(bytes2) {
210
2389
  return Array.from(bytes2).map((b) => b.toString(16).padStart(2, "0")).join("");
211
2390
  }
@@ -235,6 +2414,34 @@ function sleep(ms) {
235
2414
  function isNode() {
236
2415
  return !!(typeof process !== "undefined" && process.versions?.node);
237
2416
  }
2417
+ function serialize(value) {
2418
+ if (value == null) {
2419
+ return value;
2420
+ }
2421
+ if (value instanceof decimal_default) {
2422
+ return value.toFixed();
2423
+ }
2424
+ if (typeof value === "bigint") {
2425
+ if (value > BigInt(Number.MAX_SAFE_INTEGER)) {
2426
+ throw new Error(`BigInt value ${value} is too large for safe conversion`);
2427
+ }
2428
+ return Number(value);
2429
+ }
2430
+ if (value instanceof Map) {
2431
+ return new Map(
2432
+ Array.from(value.entries()).map(([k, v]) => [k, serialize(v)])
2433
+ );
2434
+ }
2435
+ if (Array.isArray(value)) {
2436
+ return value.map(serialize);
2437
+ }
2438
+ if (value && typeof value === "object") {
2439
+ return Object.fromEntries(
2440
+ Object.entries(value).map(([k, v]) => [k, serialize(v)])
2441
+ );
2442
+ }
2443
+ return value;
2444
+ }
238
2445
 
239
2446
  // ../node_modules/.pnpm/zod@3.24.2/node_modules/zod/lib/index.mjs
240
2447
  var util;
@@ -397,8 +2604,8 @@ var ZodError = class _ZodError extends Error {
397
2604
  constructor(issues) {
398
2605
  super();
399
2606
  this.issues = [];
400
- this.addIssue = (sub) => {
401
- this.issues = [...this.issues, sub];
2607
+ this.addIssue = (sub2) => {
2608
+ this.issues = [...this.issues, sub2];
402
2609
  };
403
2610
  this.addIssues = (subs = []) => {
404
2611
  this.issues = [...this.issues, ...subs];
@@ -465,12 +2672,12 @@ var ZodError = class _ZodError extends Error {
465
2672
  flatten(mapper = (issue) => issue.message) {
466
2673
  const fieldErrors = {};
467
2674
  const formErrors = [];
468
- for (const sub of this.issues) {
469
- if (sub.path.length > 0) {
470
- fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
471
- fieldErrors[sub.path[0]].push(mapper(sub));
2675
+ for (const sub2 of this.issues) {
2676
+ if (sub2.path.length > 0) {
2677
+ fieldErrors[sub2.path[0]] = fieldErrors[sub2.path[0]] || [];
2678
+ fieldErrors[sub2.path[0]].push(mapper(sub2));
472
2679
  } else {
473
- formErrors.push(mapper(sub));
2680
+ formErrors.push(mapper(sub2));
474
2681
  }
475
2682
  }
476
2683
  return { formErrors, fieldErrors };
@@ -1655,24 +3862,24 @@ var ZodString = class _ZodString extends ZodType {
1655
3862
  return !!this._def.checks.find((ch) => ch.kind === "base64url");
1656
3863
  }
1657
3864
  get minLength() {
1658
- let min = null;
3865
+ let min2 = null;
1659
3866
  for (const ch of this._def.checks) {
1660
3867
  if (ch.kind === "min") {
1661
- if (min === null || ch.value > min)
1662
- min = ch.value;
3868
+ if (min2 === null || ch.value > min2)
3869
+ min2 = ch.value;
1663
3870
  }
1664
3871
  }
1665
- return min;
3872
+ return min2;
1666
3873
  }
1667
3874
  get maxLength() {
1668
- let max = null;
3875
+ let max2 = null;
1669
3876
  for (const ch of this._def.checks) {
1670
3877
  if (ch.kind === "max") {
1671
- if (max === null || ch.value < max)
1672
- max = ch.value;
3878
+ if (max2 === null || ch.value < max2)
3879
+ max2 = ch.value;
1673
3880
  }
1674
3881
  }
1675
- return max;
3882
+ return max2;
1676
3883
  }
1677
3884
  };
1678
3885
  ZodString.create = (params) => {
@@ -1877,42 +4084,42 @@ var ZodNumber = class _ZodNumber extends ZodType {
1877
4084
  });
1878
4085
  }
1879
4086
  get minValue() {
1880
- let min = null;
4087
+ let min2 = null;
1881
4088
  for (const ch of this._def.checks) {
1882
4089
  if (ch.kind === "min") {
1883
- if (min === null || ch.value > min)
1884
- min = ch.value;
4090
+ if (min2 === null || ch.value > min2)
4091
+ min2 = ch.value;
1885
4092
  }
1886
4093
  }
1887
- return min;
4094
+ return min2;
1888
4095
  }
1889
4096
  get maxValue() {
1890
- let max = null;
4097
+ let max2 = null;
1891
4098
  for (const ch of this._def.checks) {
1892
4099
  if (ch.kind === "max") {
1893
- if (max === null || ch.value < max)
1894
- max = ch.value;
4100
+ if (max2 === null || ch.value < max2)
4101
+ max2 = ch.value;
1895
4102
  }
1896
4103
  }
1897
- return max;
4104
+ return max2;
1898
4105
  }
1899
4106
  get isInt() {
1900
4107
  return !!this._def.checks.find((ch) => ch.kind === "int" || ch.kind === "multipleOf" && util.isInteger(ch.value));
1901
4108
  }
1902
4109
  get isFinite() {
1903
- let max = null, min = null;
4110
+ let max2 = null, min2 = null;
1904
4111
  for (const ch of this._def.checks) {
1905
4112
  if (ch.kind === "finite" || ch.kind === "int" || ch.kind === "multipleOf") {
1906
4113
  return true;
1907
4114
  } else if (ch.kind === "min") {
1908
- if (min === null || ch.value > min)
1909
- min = ch.value;
4115
+ if (min2 === null || ch.value > min2)
4116
+ min2 = ch.value;
1910
4117
  } else if (ch.kind === "max") {
1911
- if (max === null || ch.value < max)
1912
- max = ch.value;
4118
+ if (max2 === null || ch.value < max2)
4119
+ max2 = ch.value;
1913
4120
  }
1914
4121
  }
1915
- return Number.isFinite(min) && Number.isFinite(max);
4122
+ return Number.isFinite(min2) && Number.isFinite(max2);
1916
4123
  }
1917
4124
  };
1918
4125
  ZodNumber.create = (params) => {
@@ -2067,24 +4274,24 @@ var ZodBigInt = class _ZodBigInt extends ZodType {
2067
4274
  });
2068
4275
  }
2069
4276
  get minValue() {
2070
- let min = null;
4277
+ let min2 = null;
2071
4278
  for (const ch of this._def.checks) {
2072
4279
  if (ch.kind === "min") {
2073
- if (min === null || ch.value > min)
2074
- min = ch.value;
4280
+ if (min2 === null || ch.value > min2)
4281
+ min2 = ch.value;
2075
4282
  }
2076
4283
  }
2077
- return min;
4284
+ return min2;
2078
4285
  }
2079
4286
  get maxValue() {
2080
- let max = null;
4287
+ let max2 = null;
2081
4288
  for (const ch of this._def.checks) {
2082
4289
  if (ch.kind === "max") {
2083
- if (max === null || ch.value < max)
2084
- max = ch.value;
4290
+ if (max2 === null || ch.value < max2)
4291
+ max2 = ch.value;
2085
4292
  }
2086
4293
  }
2087
- return max;
4294
+ return max2;
2088
4295
  }
2089
4296
  };
2090
4297
  ZodBigInt.create = (params) => {
@@ -2202,24 +4409,24 @@ var ZodDate = class _ZodDate extends ZodType {
2202
4409
  });
2203
4410
  }
2204
4411
  get minDate() {
2205
- let min = null;
4412
+ let min2 = null;
2206
4413
  for (const ch of this._def.checks) {
2207
4414
  if (ch.kind === "min") {
2208
- if (min === null || ch.value > min)
2209
- min = ch.value;
4415
+ if (min2 === null || ch.value > min2)
4416
+ min2 = ch.value;
2210
4417
  }
2211
4418
  }
2212
- return min != null ? new Date(min) : null;
4419
+ return min2 != null ? new Date(min2) : null;
2213
4420
  }
2214
4421
  get maxDate() {
2215
- let max = null;
4422
+ let max2 = null;
2216
4423
  for (const ch of this._def.checks) {
2217
4424
  if (ch.kind === "max") {
2218
- if (max === null || ch.value < max)
2219
- max = ch.value;
4425
+ if (max2 === null || ch.value < max2)
4426
+ max2 = ch.value;
2220
4427
  }
2221
4428
  }
2222
- return max != null ? new Date(max) : null;
4429
+ return max2 != null ? new Date(max2) : null;
2223
4430
  }
2224
4431
  };
2225
4432
  ZodDate.create = (params) => {
@@ -4297,12 +6504,33 @@ var z = /* @__PURE__ */ Object.freeze({
4297
6504
  });
4298
6505
 
4299
6506
  // src/zod-types/index.ts
4300
- var Decimal = z.coerce.number();
4301
- var AssetId = z.number().int().nonnegative();
4302
- var Amount = z.coerce.bigint().refine((val) => val >= 0n && val <= 2n ** 128n - 1n);
4303
- var jsonToMap = (obj) => new Map(Object.entries(obj).map(([k, v]) => [Number(k), v]));
4304
- var createJsonMap = (valueSchema) => {
4305
- return z.record(z.string(), valueSchema).transform((obj) => jsonToMap(obj));
6507
+ var I64_MAX = BigInt("9223372036854775807");
6508
+ var I64_MIN = BigInt("-9223372036854775808");
6509
+ var U16_MAX = 65535;
6510
+ var U32_MAX = 4294967295;
6511
+ var U64_MAX = BigInt("18446744073709551615");
6512
+ var U128_MAX = BigInt("340282366920938463463374607431768211455");
6513
+ var I64Schema = z.coerce.bigint().min(I64_MIN).max(I64_MAX);
6514
+ var U16Schema = z.coerce.number().int().min(0).max(U16_MAX);
6515
+ var U32Schema = z.coerce.number().int().min(0).max(U32_MAX);
6516
+ var U64Schema = z.coerce.bigint().min(0n).max(U64_MAX);
6517
+ var U128Schema = z.coerce.bigint().min(0n).max(U128_MAX);
6518
+ var DecimalSchema = z.coerce.string().or(z.coerce.number()).transform((val) => {
6519
+ try {
6520
+ return new decimal_default(val);
6521
+ } catch (e) {
6522
+ throw new Error(`Invalid decimal: ${val}`);
6523
+ }
6524
+ });
6525
+ var Base58Address = z.string().regex(/^[1-9A-HJ-NP-Za-km-z]{32}$/);
6526
+ var AssetId = U16Schema;
6527
+ var OrderId = U64Schema;
6528
+ var Amount = U128Schema;
6529
+ var UnixTimestampMillis = I64Schema;
6530
+ var createJsonMap = (keySchema, valueSchema) => {
6531
+ return z.record(z.string(), valueSchema).transform(
6532
+ (obj) => new Map(Object.entries(obj).map(([k, v]) => [keySchema.parse(k), v]))
6533
+ );
4306
6534
  };
4307
6535
 
4308
6536
  // src/zod-types/rest.ts
@@ -4319,7 +6547,7 @@ var ORDER_TYPES = [
4319
6547
  ];
4320
6548
  var createBaseResponse = (schema) => z.object({
4321
6549
  data: schema,
4322
- meta: createJsonMap(z.unknown()).optional()
6550
+ meta: createJsonMap(z.unknown(), z.unknown()).optional()
4323
6551
  });
4324
6552
  var StateResponseSchemas = {
4325
6553
  StateValue: (valueSchema) => createBaseResponse(z.object({ value: valueSchema.nullable() })),
@@ -4359,198 +6587,207 @@ var BaseResponseSchemas = {
4359
6587
  NonceResponse: createBaseResponse(z.object({ nonce: z.number() }))
4360
6588
  };
4361
6589
  var OrderbookLevel = z.object({
4362
- order_ids: z.array(z.number()),
4363
- total_size: Decimal
6590
+ order_ids: z.array(OrderId),
6591
+ total_size: DecimalSchema
4364
6592
  });
4365
6593
  var Position = z.object({
4366
- size: Decimal,
4367
- cost_of_trades: Decimal,
4368
- realized_pnl: Decimal,
4369
- cached_mark_price: Decimal,
4370
- funding_payments_on_side: Decimal,
4371
- funding_payments_on_position: Decimal,
4372
- last_accum_funding_applied_per_lot: z.tuple([Decimal, z.number()])
6594
+ size: DecimalSchema,
6595
+ cost_of_trades: DecimalSchema,
6596
+ realized_pnl: DecimalSchema,
6597
+ cached_mark_price: DecimalSchema,
6598
+ last_accum_funding_applied_per_lot: z.tuple([
6599
+ DecimalSchema,
6600
+ UnixTimestampMillis
6601
+ ]),
6602
+ funding_payments_on_side: DecimalSchema,
6603
+ funding_payments_on_position: DecimalSchema
4373
6604
  });
4374
6605
  var BorrowLendRateParameters = z.object({
4375
- optimal_utilisation_rate: Decimal,
4376
- min_borrow_rate: Decimal,
4377
- optimal_borrow_rate: Decimal,
4378
- max_borrow_rate: Decimal
6606
+ optimal_utilisation_rate: DecimalSchema,
6607
+ min_borrow_rate: DecimalSchema,
6608
+ optimal_borrow_rate: DecimalSchema,
6609
+ max_borrow_rate: DecimalSchema
6610
+ });
6611
+ var BorrowLendRiskWeight = z.object({
6612
+ asset_weight: DecimalSchema,
6613
+ initial_liability_weight: DecimalSchema,
6614
+ maintenance_liability_weight: DecimalSchema
4379
6615
  });
4380
6616
  var BorrowLendRiskParameters = z.object({
4381
- weight: Decimal,
4382
- initial_liability_weight: Decimal,
4383
- maintenance_liability_weight: Decimal,
4384
- deposit_limit: Decimal,
4385
- borrow_limit: Decimal,
4386
- liquidation_reward_ratio: Decimal,
4387
- liability_liquidation_limit_ratio: Decimal
6617
+ weights: BorrowLendRiskWeight,
6618
+ deposit_limit: DecimalSchema,
6619
+ borrow_limit: DecimalSchema,
6620
+ liquidation_reward_ratio: DecimalSchema,
6621
+ liability_liquidation_limit_ratio: DecimalSchema
4388
6622
  });
4389
6623
  var BorrowLendMarket = z.object({
4390
6624
  is_active: z.boolean(),
4391
- last_update_timestamp: z.number(),
6625
+ last_update_timestamp: UnixTimestampMillis,
4392
6626
  decimals: z.number().nonnegative(),
4393
6627
  risk_params: BorrowLendRiskParameters,
4394
6628
  rate_params: BorrowLendRateParameters,
4395
- available_amount: Decimal,
4396
- borrowed_amount: Decimal,
4397
- cumulative_deposit_rate: Decimal,
4398
- cumulative_borrow_rate: Decimal,
4399
- accumulated_protocol_fees: Decimal
6629
+ available_amount: DecimalSchema,
6630
+ borrowed_amount: DecimalSchema,
6631
+ cumulative_deposit_rate: DecimalSchema,
6632
+ cumulative_borrow_rate: DecimalSchema,
6633
+ accumulated_protocol_fees: DecimalSchema
4400
6634
  });
4401
6635
  var BorrowLendRiskWeights = z.object({
4402
- asset_weight: Decimal,
4403
- initial_liability_weight: Decimal,
4404
- maintenance_liability_weight: Decimal
6636
+ asset_weight: DecimalSchema,
6637
+ initial_liability_weight: DecimalSchema,
6638
+ maintenance_liability_weight: DecimalSchema
4405
6639
  });
4406
6640
  var SpotLedger = z.object({
4407
6641
  weights: BorrowLendRiskWeights,
4408
- asset: Decimal,
4409
- liability: Decimal
6642
+ asset: DecimalSchema,
6643
+ liability: DecimalSchema
4410
6644
  });
4411
6645
  var UsdcLedger = z.object({
4412
6646
  ledger: SpotLedger,
4413
- unrealized_loss_borrow: Decimal,
4414
- unsettled_perp_profit: Decimal
6647
+ unrealized_loss_borrow: DecimalSchema,
6648
+ unsettled_perp_profit: DecimalSchema
4415
6649
  });
4416
6650
  var StrippedOrder = z.object({
4417
- size: Decimal,
4418
- price: Decimal,
6651
+ size: DecimalSchema,
6652
+ price: DecimalSchema,
4419
6653
  side: z.enum(SIDES),
4420
- order_id: z.number(),
6654
+ order_id: OrderId,
4421
6655
  reduce_only: z.boolean()
4422
6656
  });
4423
6657
  var StrippedTpsl = z.object({
4424
6658
  side: z.enum(SIDES),
4425
- tpsl_order_id: z.number(),
4426
- order_price: Decimal,
4427
- trigger_price: Decimal,
6659
+ tpsl_order_id: OrderId,
6660
+ order_price: DecimalSchema,
6661
+ trigger_price: DecimalSchema,
4428
6662
  trigger_direction: z.enum(TRIGGER_DIRECTIONS),
4429
6663
  tpsl_price_condition: z.enum(TPSL_PRICE_CONDITIONS),
4430
- active_size: Decimal,
4431
- full_size: Decimal,
6664
+ active_size: DecimalSchema,
6665
+ full_size: DecimalSchema,
4432
6666
  order_type: z.enum(ORDER_TYPES),
4433
- parent_order_id: z.number().optional(),
4434
- linked_tpsl_order_ids: z.array(z.number())
6667
+ parent_order_id: OrderId.optional(),
6668
+ linked_tpsl_order_ids: z.array(OrderId)
4435
6669
  });
4436
6670
  var PerpLedger = z.object({
4437
6671
  orders: z.array(StrippedOrder),
4438
6672
  position: Position,
4439
- user_selected_max_leverage: z.number().nonnegative(),
6673
+ user_selected_max_leverage: U16Schema,
4440
6674
  tpsls: z.array(StrippedTpsl)
4441
6675
  });
4442
6676
  var PriceInfo = z.object({
4443
- mark_price: Decimal,
4444
- oracle_price: Decimal,
4445
- publish_timestamp: z.number()
6677
+ mark_price: DecimalSchema,
6678
+ oracle_price: DecimalSchema,
6679
+ publish_timestamp: UnixTimestampMillis
4446
6680
  });
4447
6681
  var Schemas = {
4448
6682
  DummyValue: z.number(),
4449
6683
  Order: z.object({
4450
6684
  side: z.enum(SIDES),
4451
- asset_id: z.number(),
4452
- order_id: z.number(),
4453
- price: Decimal,
4454
- size: Decimal,
6685
+ asset_id: AssetId,
6686
+ order_id: OrderId,
6687
+ price: DecimalSchema,
6688
+ size: DecimalSchema,
4455
6689
  reduce_only: z.boolean(),
4456
- filled_size: Decimal,
4457
- average_filled_price: Decimal,
4458
- owner: z.string(),
4459
- tpsl_order_ids: z.array(z.number())
6690
+ filled_size: DecimalSchema,
6691
+ average_filled_price: DecimalSchema,
6692
+ owner: Base58Address,
6693
+ tpsl_order_ids: z.array(OrderId)
4460
6694
  }),
4461
6695
  Orderbook: z.object({
4462
- asset_id: z.number(),
4463
- bids: createJsonMap(OrderbookLevel),
4464
- asks: createJsonMap(OrderbookLevel)
6696
+ asset_id: AssetId,
6697
+ bids: createJsonMap(z.string(), OrderbookLevel),
6698
+ asks: createJsonMap(z.string(), OrderbookLevel)
4465
6699
  }),
4466
6700
  UsdcPnlPool: z.object({
4467
- usdc_token_amount: Decimal,
4468
- external_deposits: z.record(z.string(), Decimal)
6701
+ usdc_token_amount: DecimalSchema,
6702
+ external_deposits: createJsonMap(Base58Address, DecimalSchema)
4469
6703
  }),
4470
6704
  UsdcInsuranceFund: z.object({
4471
- usdc_token_amount: Decimal,
4472
- external_deposits: z.record(z.string(), Decimal),
4473
- minimum_usdc_token_amount: Decimal
6705
+ usdc_token_amount: DecimalSchema,
6706
+ external_deposits: createJsonMap(Base58Address, DecimalSchema),
6707
+ minimum_usdc_token_amount: DecimalSchema
4474
6708
  }),
4475
6709
  UserAccount: z.object({
4476
6710
  usdc_ledger: UsdcLedger,
4477
- spot_ledgers: createJsonMap(SpotLedger),
4478
- perp_ledgers: createJsonMap(PerpLedger)
6711
+ spot_ledgers: createJsonMap(AssetId, SpotLedger),
6712
+ perp_ledgers: createJsonMap(AssetId, PerpLedger)
4479
6713
  }),
4480
6714
  Pricing: z.object({
4481
- pricing_frequency_seconds: z.number(),
4482
- prices_info: createJsonMap(PriceInfo),
6715
+ pricing_frequency_seconds: U32Schema,
6716
+ prices_info: createJsonMap(AssetId, PriceInfo),
4483
6717
  premium_index: createJsonMap(
6718
+ AssetId,
4484
6719
  z.object({
4485
- weighted_premium_sum: Decimal,
4486
- duration_sum: z.number(),
4487
- last_update_timestamp: z.number().nullable()
6720
+ weighted_premium_sum: DecimalSchema,
6721
+ duration_sum: U64Schema,
6722
+ last_update_timestamp: UnixTimestampMillis.nullable()
4488
6723
  })
4489
6724
  ),
4490
- funding_rates: createJsonMap(z.tuple([Decimal, z.number()])),
4491
- accum_funding_per_lot: createJsonMap(z.tuple([Decimal, z.number()]))
6725
+ funding_rates: createJsonMap(
6726
+ AssetId,
6727
+ z.tuple([DecimalSchema, UnixTimestampMillis])
6728
+ ),
6729
+ accum_funding_per_lot: createJsonMap(
6730
+ AssetId,
6731
+ z.tuple([DecimalSchema, UnixTimestampMillis])
6732
+ )
4492
6733
  }),
4493
6734
  EnrichedBorrowLendMarket: z.object({
4494
6735
  market: BorrowLendMarket,
4495
- utilisation_rate: Decimal,
4496
- current_deposit_rate: Decimal,
4497
- current_borrow_rate: Decimal
6736
+ utilisation_rate: DecimalSchema,
6737
+ current_deposit_rate: DecimalSchema,
6738
+ current_borrow_rate: DecimalSchema
4498
6739
  }),
4499
6740
  PerpMarket: z.object({
4500
6741
  is_active: z.boolean(),
4501
- min_tick_size: Decimal,
4502
- min_lot_size: Decimal,
4503
- min_interest_rate_clamp: Decimal,
4504
- max_interest_rate_clamp: Decimal,
4505
- min_funding_rate_clamp: Decimal,
4506
- max_funding_rate_clamp: Decimal,
4507
- impact_margin: Decimal,
4508
- interest_rate: Decimal
6742
+ min_tick_size: DecimalSchema,
6743
+ min_lot_size: DecimalSchema,
6744
+ min_interest_rate_clamp: DecimalSchema,
6745
+ max_interest_rate_clamp: DecimalSchema,
6746
+ min_funding_rate_clamp: DecimalSchema,
6747
+ max_funding_rate_clamp: DecimalSchema,
6748
+ impact_margin: DecimalSchema,
6749
+ interest_rate: DecimalSchema
4509
6750
  }),
4510
6751
  MarginConfig: z.object({
4511
6752
  perp_leverage_tables: createJsonMap(
6753
+ AssetId,
4512
6754
  z.object({
4513
6755
  tiers: createJsonMap(
6756
+ z.string(),
4514
6757
  z.object({
4515
- max_leverage: z.number(),
4516
- mmr: Decimal,
4517
- maintenance_amount: Decimal
6758
+ max_leverage: U16Schema,
6759
+ mmr: DecimalSchema,
6760
+ maintenance_amount: DecimalSchema
4518
6761
  })
4519
6762
  )
4520
6763
  })
4521
6764
  ),
4522
- liquidation_ioc_buffer: Decimal,
4523
- backstop_liquidation_threshold: Decimal,
4524
- liquidation_fee: Decimal
6765
+ liquidation_ioc_buffer: DecimalSchema,
6766
+ backstop_liquidation_threshold: DecimalSchema,
6767
+ liquidation_fee: DecimalSchema
4525
6768
  }),
4526
6769
  Assets: z.object({
4527
- assets: createJsonMap(z.string())
6770
+ assets: createJsonMap(AssetId, z.string())
4528
6771
  })
4529
6772
  };
4530
6773
  var ResponseSchemas = {
4531
6774
  DummyValue: StateResponseSchemas.StateValue(Schemas.DummyValue),
4532
- Order: StateResponseSchemas.StateMapElement(z.number(), Schemas.Order),
6775
+ Order: StateResponseSchemas.StateMapElement(OrderId, Schemas.Order),
4533
6776
  UserAccount: createBaseResponse(
4534
6777
  z.object({ user_accounts: z.array(Schemas.UserAccount) })
4535
6778
  ),
4536
- Orderbook: StateResponseSchemas.StateMapElement(
4537
- z.number(),
4538
- Schemas.Orderbook
4539
- ),
6779
+ Orderbook: StateResponseSchemas.StateMapElement(AssetId, Schemas.Orderbook),
4540
6780
  Pricing: StateResponseSchemas.StateValue(Schemas.Pricing),
4541
6781
  UsdcPnlPool: StateResponseSchemas.StateValue(Schemas.UsdcPnlPool),
4542
6782
  UsdcInsuranceFund: StateResponseSchemas.StateValue(Schemas.UsdcInsuranceFund),
4543
6783
  EnrichedBorrowLendMarket: createBaseResponse(
4544
6784
  z.object({ market: Schemas.EnrichedBorrowLendMarket })
4545
6785
  ),
4546
- PerpMarket: StateResponseSchemas.StateMapElement(
4547
- z.number(),
4548
- Schemas.PerpMarket
4549
- ),
6786
+ PerpMarket: StateResponseSchemas.StateMapElement(AssetId, Schemas.PerpMarket),
4550
6787
  MarginConfig: StateResponseSchemas.StateValue(Schemas.MarginConfig),
4551
6788
  AccountAddresses: createBaseResponse(
4552
6789
  z.object({
4553
- accounts: z.array(z.string())
6790
+ accounts: z.array(Base58Address)
4554
6791
  })
4555
6792
  ),
4556
6793
  Assets: StateResponseSchemas.CustomRouteValue(Schemas.Assets)
@@ -4698,7 +6935,7 @@ var WebSocketResponse = z.object({
4698
6935
  conn_id: z.string(),
4699
6936
  op: z.string()
4700
6937
  });
4701
- var OrderbookDelta = z.array(z.tuple([Decimal, Decimal]));
6938
+ var OrderbookDelta = z.array(z.tuple([DecimalSchema, DecimalSchema]));
4702
6939
  var OrderbookUpdate = z.object({
4703
6940
  s: AssetId,
4704
6941
  b: OrderbookDelta,
@@ -6638,7 +8875,7 @@ var Client = class _Client {
6638
8875
  exchange: {
6639
8876
  deposit: {
6640
8877
  asset_id: asset,
6641
- amount: BulletWasm.convert_rust_decimal_to_json(amount.toString())
8878
+ amount: BulletWasm.convert_rust_decimal_to_json(amount.toFixed())
6642
8879
  }
6643
8880
  }
6644
8881
  });
@@ -6648,7 +8885,7 @@ var Client = class _Client {
6648
8885
  exchange: {
6649
8886
  withdraw: {
6650
8887
  asset_id: asset,
6651
- amount: BulletWasm.convert_rust_decimal_to_json(amount.toString())
8888
+ amount: BulletWasm.convert_rust_decimal_to_json(amount.toFixed())
6652
8889
  }
6653
8890
  }
6654
8891
  });
@@ -6658,7 +8895,7 @@ var Client = class _Client {
6658
8895
  exchange: {
6659
8896
  borrow_spot: {
6660
8897
  asset_id: asset,
6661
- amount: BulletWasm.convert_rust_decimal_to_json(amount.toString())
8898
+ amount: BulletWasm.convert_rust_decimal_to_json(amount.toFixed())
6662
8899
  }
6663
8900
  }
6664
8901
  });
@@ -6669,8 +8906,8 @@ var Client = class _Client {
6669
8906
  place_order: {
6670
8907
  order_args: {
6671
8908
  asset_id: asset,
6672
- price: BulletWasm.convert_rust_decimal_to_json(price.toString()),
6673
- size: BulletWasm.convert_rust_decimal_to_json(size.toString()),
8909
+ price: BulletWasm.convert_rust_decimal_to_json(price.toFixed()),
8910
+ size: BulletWasm.convert_rust_decimal_to_json(size.toFixed()),
6674
8911
  side,
6675
8912
  order_type: orderType,
6676
8913
  reduce_only: reduceOnly
@@ -6685,14 +8922,14 @@ var Client = class _Client {
6685
8922
  orderType: orderType2
6686
8923
  }) => ({
6687
8924
  order_price: BulletWasm.convert_rust_decimal_to_json(
6688
- orderPrice.toString()
8925
+ orderPrice.toFixed()
6689
8926
  ),
6690
8927
  trigger_price: BulletWasm.convert_rust_decimal_to_json(
6691
- triggerPrice.toString()
8928
+ triggerPrice.toFixed()
6692
8929
  ),
6693
8930
  trigger_direction: triggerDirection,
6694
8931
  tpsl_price_condition: tpslPriceCondition,
6695
- size: BulletWasm.convert_rust_decimal_to_json(size2.toString()),
8932
+ size: BulletWasm.convert_rust_decimal_to_json(size2.toFixed()),
6696
8933
  order_type: orderType2
6697
8934
  })
6698
8935
  )
@@ -6715,14 +8952,14 @@ var Client = class _Client {
6715
8952
  orderType
6716
8953
  }) => ({
6717
8954
  order_price: BulletWasm.convert_rust_decimal_to_json(
6718
- orderPrice.toString()
8955
+ orderPrice.toFixed()
6719
8956
  ),
6720
8957
  trigger_price: BulletWasm.convert_rust_decimal_to_json(
6721
- triggerPrice.toString()
8958
+ triggerPrice.toFixed()
6722
8959
  ),
6723
8960
  trigger_direction: triggerDirection,
6724
8961
  tpsl_price_condition: tpslPriceCondition,
6725
- size: BulletWasm.convert_rust_decimal_to_json(size.toString()),
8962
+ size: BulletWasm.convert_rust_decimal_to_json(size.toFixed()),
6726
8963
  order_type: orderType
6727
8964
  })
6728
8965
  )
@@ -6730,17 +8967,19 @@ var Client = class _Client {
6730
8967
  }
6731
8968
  });
6732
8969
  }
6733
- async replaceOrder(asset, existingOrderId, existingSide, price, size, side, orderType = "Limit", reduceOnly = false, tpslOrders = []) {
8970
+ async replaceOrder(existingOrderId, asset, price, size, side, orderType = "Limit", reduceOnly = false, tpslOrders = []) {
6734
8971
  return await this.submitTransaction({
6735
8972
  exchange: {
6736
8973
  replace_order: {
6737
8974
  existing_order_id: existingOrderId,
6738
- asset_id: asset,
6739
- price: BulletWasm.convert_rust_decimal_to_json(price.toString()),
6740
- size: BulletWasm.convert_rust_decimal_to_json(size.toString()),
6741
- side,
6742
- order_type: orderType,
6743
- reduce_only: reduceOnly,
8975
+ order_args: {
8976
+ asset_id: asset,
8977
+ price: BulletWasm.convert_rust_decimal_to_json(price.toFixed()),
8978
+ size: BulletWasm.convert_rust_decimal_to_json(size.toFixed()),
8979
+ side,
8980
+ order_type: orderType,
8981
+ reduce_only: reduceOnly
8982
+ },
6744
8983
  tpsls: tpslOrders.map(
6745
8984
  ({
6746
8985
  orderPrice,
@@ -6751,14 +8990,14 @@ var Client = class _Client {
6751
8990
  orderType: orderType2
6752
8991
  }) => ({
6753
8992
  order_price: BulletWasm.convert_rust_decimal_to_json(
6754
- orderPrice.toString()
8993
+ orderPrice.toFixed()
6755
8994
  ),
6756
8995
  trigger_price: BulletWasm.convert_rust_decimal_to_json(
6757
- triggerPrice.toString()
8996
+ triggerPrice.toFixed()
6758
8997
  ),
6759
8998
  trigger_direction: triggerDirection,
6760
8999
  tpsl_price_condition: tpslPriceCondition,
6761
- size: BulletWasm.convert_rust_decimal_to_json(size2.toString()),
9000
+ size: BulletWasm.convert_rust_decimal_to_json(size2.toFixed()),
6762
9001
  order_type: orderType2
6763
9002
  })
6764
9003
  )
@@ -6775,11 +9014,10 @@ var Client = class _Client {
6775
9014
  }
6776
9015
  });
6777
9016
  }
6778
- async cancelOrder(asset, orderId) {
9017
+ async cancelOrder(orderId) {
6779
9018
  return await this.submitTransaction({
6780
9019
  exchange: {
6781
9020
  cancel_order: {
6782
- asset_id: asset,
6783
9021
  order_id: orderId
6784
9022
  }
6785
9023
  }
@@ -6805,12 +9043,14 @@ var Client = class _Client {
6805
9043
  ({ asset, medianCexPrice, oraclePrice, diffEma }) => ({
6806
9044
  asset_id: asset,
6807
9045
  median_cex_price: BulletWasm.convert_rust_decimal_to_json(
6808
- medianCexPrice.toString()
9046
+ medianCexPrice.toFixed()
6809
9047
  ),
6810
9048
  oracle_price: BulletWasm.convert_rust_decimal_to_json(
6811
- oraclePrice.toString()
9049
+ oraclePrice.toFixed()
6812
9050
  ),
6813
- diff_ema: BulletWasm.convert_rust_decimal_to_json(diffEma.toString())
9051
+ diff_ema: BulletWasm.convert_rust_decimal_to_json(
9052
+ diffEma.toFixed()
9053
+ )
6814
9054
  })
6815
9055
  ),
6816
9056
  publish_timestamp: publishTimestamp
@@ -6873,7 +9113,7 @@ var Client = class _Client {
6873
9113
  address: base58ToBytes(address),
6874
9114
  positions: positions?.map(({ asset, size }) => ({
6875
9115
  asset_id: asset,
6876
- size: BulletWasm.convert_rust_decimal_to_json(size.toString())
9116
+ size: BulletWasm.convert_rust_decimal_to_json(size.toFixed())
6877
9117
  })) ?? null
6878
9118
  }
6879
9119
  }
@@ -6887,7 +9127,7 @@ var Client = class _Client {
6887
9127
  liability_asset_id: liabilityAssetId,
6888
9128
  collateral_asset_id: collateralAssetId,
6889
9129
  liability_amount: BulletWasm.convert_rust_decimal_to_json(
6890
- liabilityAmount.toString()
9130
+ liabilityAmount.toFixed()
6891
9131
  )
6892
9132
  }
6893
9133
  }
@@ -6898,7 +9138,7 @@ var Client = class _Client {
6898
9138
  exchange: {
6899
9139
  deposit_to_usdc_pnl_pool: {
6900
9140
  usdc_amount: BulletWasm.convert_rust_decimal_to_json(
6901
- amount.toString()
9141
+ amount.toFixed()
6902
9142
  )
6903
9143
  }
6904
9144
  }
@@ -6909,7 +9149,7 @@ var Client = class _Client {
6909
9149
  exchange: {
6910
9150
  deposit_to_insurance_fund: {
6911
9151
  usdc_amount: BulletWasm.convert_rust_decimal_to_json(
6912
- amount.toString()
9152
+ amount.toFixed()
6913
9153
  )
6914
9154
  }
6915
9155
  }
@@ -6920,8 +9160,8 @@ var Client = class _Client {
6920
9160
  // src/zod-types/wasm.ts
6921
9161
  var WasmResponseSchemas = {
6922
9162
  SimulationResultSchema: z.object({
6923
- current: Decimal,
6924
- new: Decimal
9163
+ current: DecimalSchema,
9164
+ new: DecimalSchema
6925
9165
  })
6926
9166
  };
6927
9167
 
@@ -6930,109 +9170,137 @@ function calculateOrderbookMidpoint(orderbook) {
6930
9170
  if (orderbook.bids.size === 0 || orderbook.asks.size === 0) {
6931
9171
  return void 0;
6932
9172
  }
6933
- const bestBid = Math.max(...orderbook.bids.keys());
6934
- const bestAsk = Math.min(...orderbook.asks.keys());
6935
- return (bestBid + bestAsk) / 2;
9173
+ const bestBid = Array.from(orderbook.bids.keys()).reduce((max2, key) => {
9174
+ const keyDecimal = new decimal_default(key);
9175
+ return max2.greaterThan(keyDecimal) ? max2 : keyDecimal;
9176
+ }, new decimal_default(-Infinity));
9177
+ const bestAsk = Array.from(orderbook.asks.keys()).reduce((min2, key) => {
9178
+ const keyDecimal = new decimal_default(key);
9179
+ return min2.lessThan(keyDecimal) ? min2 : keyDecimal;
9180
+ }, new decimal_default(Infinity));
9181
+ return bestBid.plus(bestAsk).div(2);
6936
9182
  }
6937
9183
  function calculateEntryPrice(asset, userAccount) {
6938
9184
  const position = userAccount.perp_ledgers.get(asset)?.position;
6939
9185
  if (!position) {
6940
9186
  throw new Error("No position found for asset");
6941
9187
  }
6942
- return position.cost_of_trades / Math.abs(position.size);
9188
+ return position.cost_of_trades.div(position.size.abs());
6943
9189
  }
6944
9190
  function calculateTotalPerpUnrealizedPnl(userAccount, marginContext) {
9191
+ const userAccountObject = serialize(userAccount);
9192
+ const marginContextObject = serialize(marginContext);
6945
9193
  const result = BulletWasm.calculate_total_perp_unrealized_pnl(
6946
- userAccount,
6947
- marginContext
9194
+ userAccountObject,
9195
+ marginContextObject
6948
9196
  );
6949
- return Decimal.parse(result);
9197
+ return new decimal_default(result);
6950
9198
  }
6951
9199
  function calculateAccountEquity(equityType, conservative, userAccount, marginContext) {
9200
+ const userAccountObject = serialize(userAccount);
9201
+ const marginContextObject = serialize(marginContext);
6952
9202
  const result = BulletWasm.calculate_account_equity(
6953
- userAccount,
9203
+ userAccountObject,
6954
9204
  conservative,
6955
9205
  equityType,
6956
- marginContext
9206
+ marginContextObject
6957
9207
  );
6958
- return Decimal.parse(result);
9208
+ return new decimal_default(result);
6959
9209
  }
6960
9210
  function calculateAccountLeverage(userAccount, marginContext) {
9211
+ const userAccountObject = serialize(userAccount);
9212
+ const marginContextObject = serialize(marginContext);
6961
9213
  const result = BulletWasm.calculate_account_leverage(
6962
- userAccount,
6963
- marginContext
9214
+ userAccountObject,
9215
+ marginContextObject
6964
9216
  );
6965
- return Decimal.parse(result);
9217
+ return new decimal_default(result);
6966
9218
  }
6967
9219
  function calculateAvailableMargin(marginType, conservative, userAccount, marginContext) {
9220
+ const userAccountObject = serialize(userAccount);
9221
+ const marginContextObject = serialize(marginContext);
6968
9222
  const result = BulletWasm.calculate_available_margin(
6969
- userAccount,
9223
+ userAccountObject,
6970
9224
  conservative,
6971
9225
  marginType,
6972
- marginContext
9226
+ marginContextObject
6973
9227
  );
6974
- return Decimal.parse(result);
9228
+ return new decimal_default(result);
6975
9229
  }
6976
9230
  function calculateUsedMargin(marginType, userAccount, marginContext) {
9231
+ const userAccountObject = serialize(userAccount);
9232
+ const marginContextObject = serialize(marginContext);
6977
9233
  const result = BulletWasm.calculate_used_margin(
6978
- userAccount,
9234
+ userAccountObject,
6979
9235
  marginType,
6980
- marginContext
9236
+ marginContextObject
6981
9237
  );
6982
- return Decimal.parse(result);
9238
+ return new decimal_default(result);
6983
9239
  }
6984
9240
  function calculateWithdrawableAmountOfAsset(assetId, userAccount, marginContext) {
9241
+ const userAccountObject = serialize(userAccount);
9242
+ const marginContextObject = serialize(marginContext);
6985
9243
  const result = BulletWasm.calculate_withdrawable_amount_of_asset(
6986
- userAccount,
9244
+ userAccountObject,
6987
9245
  assetId,
6988
- marginContext
9246
+ marginContextObject
6989
9247
  );
6990
- return Decimal.parse(result);
9248
+ return new decimal_default(result);
6991
9249
  }
6992
9250
  function calculateEstimatedLiquidationPrice(assetId, userAccount, marginContext) {
9251
+ const userAccountObject = serialize(userAccount);
9252
+ const marginContextObject = serialize(marginContext);
6993
9253
  const result = BulletWasm.calculate_estimated_liquidation_price(
6994
- userAccount,
9254
+ userAccountObject,
6995
9255
  assetId,
6996
- marginContext
9256
+ marginContextObject
6997
9257
  );
6998
- return Decimal.parse(result);
9258
+ return new decimal_default(result);
6999
9259
  }
7000
9260
  function calculateLiquidationRiskPercentage(userAccount, marginContext) {
9261
+ const userAccountObject = serialize(userAccount);
9262
+ const marginContextObject = serialize(marginContext);
7001
9263
  const result = BulletWasm.calculate_liquidation_risk_percentage(
7002
- userAccount,
7003
- marginContext
9264
+ userAccountObject,
9265
+ marginContextObject
7004
9266
  );
7005
- return Decimal.parse(result);
9267
+ return new decimal_default(result);
7006
9268
  }
7007
9269
  function calculateMaxBorrowAmount(assetId, userAccount, marginContext) {
9270
+ const userAccountObject = serialize(userAccount);
9271
+ const marginContextObject = serialize(marginContext);
7008
9272
  const result = BulletWasm.calculate_max_borrow_amount(
7009
9273
  assetId,
7010
- userAccount,
7011
- marginContext
9274
+ userAccountObject,
9275
+ marginContextObject
7012
9276
  );
7013
- return Decimal.parse(result);
9277
+ return new decimal_default(result);
7014
9278
  }
7015
- function calculateMaxOrderSize(assetId, side, price, reduceOnly, userAccount, marginContext, makerBook, n_iterations = 100, error_tolerance = 1e-4) {
9279
+ function calculateMaxOrderSize(assetId, side, price, reduceOnly, userAccount, marginContext, makerBook, n_iterations = 100, error_tolerance = new decimal_default(1e-4)) {
9280
+ const userAccountObject = serialize(userAccount);
9281
+ const marginContextObject = serialize(marginContext);
7016
9282
  const result = BulletWasm.calculate_max_order_size(
7017
- userAccount,
9283
+ userAccountObject,
7018
9284
  assetId,
7019
9285
  side === "Bid" ? Side.Bid : Side.Ask,
7020
- price.toString(),
9286
+ price.toFixed(),
7021
9287
  reduceOnly,
7022
- marginContext,
9288
+ marginContextObject,
7023
9289
  side == "Bid" ? makerBook.asks : makerBook.bids,
7024
9290
  n_iterations,
7025
- error_tolerance.toString()
9291
+ error_tolerance.toFixed()
7026
9292
  );
7027
- return Decimal.parse(result);
9293
+ return new decimal_default(result);
7028
9294
  }
7029
9295
  function simulateUsedMarginOnBorrow(assetId, borrowAmount, userAccount, marginType, marginContext) {
9296
+ const userAccountObject = serialize(userAccount);
9297
+ const marginContextObject = serialize(marginContext);
7030
9298
  const result = BulletWasm.simulate_used_margin_on_borrow(
7031
9299
  assetId,
7032
- borrowAmount.toString(),
7033
- userAccount,
9300
+ borrowAmount.toFixed(),
9301
+ userAccountObject,
7034
9302
  marginType,
7035
- marginContext
9303
+ marginContextObject
7036
9304
  );
7037
9305
  return WasmResponseSchemas.SimulationResultSchema.parse(result);
7038
9306
  }
@@ -7087,7 +9355,19 @@ export {
7087
9355
  hexToBase64,
7088
9356
  hexToBytes,
7089
9357
  isNode,
9358
+ serialize,
7090
9359
  simulateUsedMarginOnBorrow,
7091
9360
  sleep
7092
9361
  };
9362
+ /*! Bundled license information:
9363
+
9364
+ decimal.js/decimal.mjs:
9365
+ (*!
9366
+ * decimal.js v10.5.0
9367
+ * An arbitrary-precision Decimal type for JavaScript.
9368
+ * https://github.com/MikeMcl/decimal.js
9369
+ * Copyright (c) 2025 Michael Mclaughlin <M8ch88l@gmail.com>
9370
+ * MIT Licence
9371
+ *)
9372
+ */
7093
9373
  //# sourceMappingURL=index.js.map