@pyxisjs/core 0.1.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.
- package/LICENSE +21 -0
- package/dist/index.d.mts +821 -0
- package/dist/index.d.ts +821 -0
- package/dist/index.js +3133 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +3104 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +41 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,3133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __commonJS = (cb, mod2) => function __require() {
|
|
9
|
+
return mod2 || (0, cb[__getOwnPropNames(cb)[0]])((mod2 = { exports: {} }).exports, mod2), mod2.exports;
|
|
10
|
+
};
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from))
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toESM = (mod2, isNodeMode, target) => (target = mod2 != null ? __create(__getProtoOf(mod2)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod2 || !mod2.__esModule ? __defProp(target, "default", { value: mod2, enumerable: true }) : target,
|
|
29
|
+
mod2
|
|
30
|
+
));
|
|
31
|
+
var __toCommonJS = (mod2) => __copyProps(__defProp({}, "__esModule", { value: true }), mod2);
|
|
32
|
+
|
|
33
|
+
// ../../node_modules/.pnpm/dayjs@1.11.19/node_modules/dayjs/dayjs.min.js
|
|
34
|
+
var require_dayjs_min = __commonJS({
|
|
35
|
+
"../../node_modules/.pnpm/dayjs@1.11.19/node_modules/dayjs/dayjs.min.js"(exports2, module2) {
|
|
36
|
+
"use strict";
|
|
37
|
+
!(function(t, e) {
|
|
38
|
+
"object" == typeof exports2 && "undefined" != typeof module2 ? module2.exports = e() : "function" == typeof define && define.amd ? define(e) : (t = "undefined" != typeof globalThis ? globalThis : t || self).dayjs = e();
|
|
39
|
+
})(exports2, (function() {
|
|
40
|
+
"use strict";
|
|
41
|
+
var t = 1e3, e = 6e4, n = 36e5, r = "millisecond", i = "second", s = "minute", u = "hour", a = "day", o = "week", c = "month", f = "quarter", h = "year", d = "date", l = "Invalid Date", $ = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/, y = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g, M = { name: "en", weekdays: "Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"), months: "January_February_March_April_May_June_July_August_September_October_November_December".split("_"), ordinal: function(t2) {
|
|
42
|
+
var e2 = ["th", "st", "nd", "rd"], n2 = t2 % 100;
|
|
43
|
+
return "[" + t2 + (e2[(n2 - 20) % 10] || e2[n2] || e2[0]) + "]";
|
|
44
|
+
} }, m = function(t2, e2, n2) {
|
|
45
|
+
var r2 = String(t2);
|
|
46
|
+
return !r2 || r2.length >= e2 ? t2 : "" + Array(e2 + 1 - r2.length).join(n2) + t2;
|
|
47
|
+
}, v = { s: m, z: function(t2) {
|
|
48
|
+
var e2 = -t2.utcOffset(), n2 = Math.abs(e2), r2 = Math.floor(n2 / 60), i2 = n2 % 60;
|
|
49
|
+
return (e2 <= 0 ? "+" : "-") + m(r2, 2, "0") + ":" + m(i2, 2, "0");
|
|
50
|
+
}, m: function t2(e2, n2) {
|
|
51
|
+
if (e2.date() < n2.date()) return -t2(n2, e2);
|
|
52
|
+
var r2 = 12 * (n2.year() - e2.year()) + (n2.month() - e2.month()), i2 = e2.clone().add(r2, c), s2 = n2 - i2 < 0, u2 = e2.clone().add(r2 + (s2 ? -1 : 1), c);
|
|
53
|
+
return +(-(r2 + (n2 - i2) / (s2 ? i2 - u2 : u2 - i2)) || 0);
|
|
54
|
+
}, a: function(t2) {
|
|
55
|
+
return t2 < 0 ? Math.ceil(t2) || 0 : Math.floor(t2);
|
|
56
|
+
}, p: function(t2) {
|
|
57
|
+
return { M: c, y: h, w: o, d: a, D: d, h: u, m: s, s: i, ms: r, Q: f }[t2] || String(t2 || "").toLowerCase().replace(/s$/, "");
|
|
58
|
+
}, u: function(t2) {
|
|
59
|
+
return void 0 === t2;
|
|
60
|
+
} }, g = "en", D = {};
|
|
61
|
+
D[g] = M;
|
|
62
|
+
var p = "$isDayjsObject", S = function(t2) {
|
|
63
|
+
return t2 instanceof _ || !(!t2 || !t2[p]);
|
|
64
|
+
}, w = function t2(e2, n2, r2) {
|
|
65
|
+
var i2;
|
|
66
|
+
if (!e2) return g;
|
|
67
|
+
if ("string" == typeof e2) {
|
|
68
|
+
var s2 = e2.toLowerCase();
|
|
69
|
+
D[s2] && (i2 = s2), n2 && (D[s2] = n2, i2 = s2);
|
|
70
|
+
var u2 = e2.split("-");
|
|
71
|
+
if (!i2 && u2.length > 1) return t2(u2[0]);
|
|
72
|
+
} else {
|
|
73
|
+
var a2 = e2.name;
|
|
74
|
+
D[a2] = e2, i2 = a2;
|
|
75
|
+
}
|
|
76
|
+
return !r2 && i2 && (g = i2), i2 || !r2 && g;
|
|
77
|
+
}, O = function(t2, e2) {
|
|
78
|
+
if (S(t2)) return t2.clone();
|
|
79
|
+
var n2 = "object" == typeof e2 ? e2 : {};
|
|
80
|
+
return n2.date = t2, n2.args = arguments, new _(n2);
|
|
81
|
+
}, b = v;
|
|
82
|
+
b.l = w, b.i = S, b.w = function(t2, e2) {
|
|
83
|
+
return O(t2, { locale: e2.$L, utc: e2.$u, x: e2.$x, $offset: e2.$offset });
|
|
84
|
+
};
|
|
85
|
+
var _ = (function() {
|
|
86
|
+
function M2(t2) {
|
|
87
|
+
this.$L = w(t2.locale, null, true), this.parse(t2), this.$x = this.$x || t2.x || {}, this[p] = true;
|
|
88
|
+
}
|
|
89
|
+
var m2 = M2.prototype;
|
|
90
|
+
return m2.parse = function(t2) {
|
|
91
|
+
this.$d = (function(t3) {
|
|
92
|
+
var e2 = t3.date, n2 = t3.utc;
|
|
93
|
+
if (null === e2) return /* @__PURE__ */ new Date(NaN);
|
|
94
|
+
if (b.u(e2)) return /* @__PURE__ */ new Date();
|
|
95
|
+
if (e2 instanceof Date) return new Date(e2);
|
|
96
|
+
if ("string" == typeof e2 && !/Z$/i.test(e2)) {
|
|
97
|
+
var r2 = e2.match($);
|
|
98
|
+
if (r2) {
|
|
99
|
+
var i2 = r2[2] - 1 || 0, s2 = (r2[7] || "0").substring(0, 3);
|
|
100
|
+
return n2 ? new Date(Date.UTC(r2[1], i2, r2[3] || 1, r2[4] || 0, r2[5] || 0, r2[6] || 0, s2)) : new Date(r2[1], i2, r2[3] || 1, r2[4] || 0, r2[5] || 0, r2[6] || 0, s2);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return new Date(e2);
|
|
104
|
+
})(t2), this.init();
|
|
105
|
+
}, m2.init = function() {
|
|
106
|
+
var t2 = this.$d;
|
|
107
|
+
this.$y = t2.getFullYear(), this.$M = t2.getMonth(), this.$D = t2.getDate(), this.$W = t2.getDay(), this.$H = t2.getHours(), this.$m = t2.getMinutes(), this.$s = t2.getSeconds(), this.$ms = t2.getMilliseconds();
|
|
108
|
+
}, m2.$utils = function() {
|
|
109
|
+
return b;
|
|
110
|
+
}, m2.isValid = function() {
|
|
111
|
+
return !(this.$d.toString() === l);
|
|
112
|
+
}, m2.isSame = function(t2, e2) {
|
|
113
|
+
var n2 = O(t2);
|
|
114
|
+
return this.startOf(e2) <= n2 && n2 <= this.endOf(e2);
|
|
115
|
+
}, m2.isAfter = function(t2, e2) {
|
|
116
|
+
return O(t2) < this.startOf(e2);
|
|
117
|
+
}, m2.isBefore = function(t2, e2) {
|
|
118
|
+
return this.endOf(e2) < O(t2);
|
|
119
|
+
}, m2.$g = function(t2, e2, n2) {
|
|
120
|
+
return b.u(t2) ? this[e2] : this.set(n2, t2);
|
|
121
|
+
}, m2.unix = function() {
|
|
122
|
+
return Math.floor(this.valueOf() / 1e3);
|
|
123
|
+
}, m2.valueOf = function() {
|
|
124
|
+
return this.$d.getTime();
|
|
125
|
+
}, m2.startOf = function(t2, e2) {
|
|
126
|
+
var n2 = this, r2 = !!b.u(e2) || e2, f2 = b.p(t2), l2 = function(t3, e3) {
|
|
127
|
+
var i2 = b.w(n2.$u ? Date.UTC(n2.$y, e3, t3) : new Date(n2.$y, e3, t3), n2);
|
|
128
|
+
return r2 ? i2 : i2.endOf(a);
|
|
129
|
+
}, $2 = function(t3, e3) {
|
|
130
|
+
return b.w(n2.toDate()[t3].apply(n2.toDate("s"), (r2 ? [0, 0, 0, 0] : [23, 59, 59, 999]).slice(e3)), n2);
|
|
131
|
+
}, y2 = this.$W, M3 = this.$M, m3 = this.$D, v2 = "set" + (this.$u ? "UTC" : "");
|
|
132
|
+
switch (f2) {
|
|
133
|
+
case h:
|
|
134
|
+
return r2 ? l2(1, 0) : l2(31, 11);
|
|
135
|
+
case c:
|
|
136
|
+
return r2 ? l2(1, M3) : l2(0, M3 + 1);
|
|
137
|
+
case o:
|
|
138
|
+
var g2 = this.$locale().weekStart || 0, D2 = (y2 < g2 ? y2 + 7 : y2) - g2;
|
|
139
|
+
return l2(r2 ? m3 - D2 : m3 + (6 - D2), M3);
|
|
140
|
+
case a:
|
|
141
|
+
case d:
|
|
142
|
+
return $2(v2 + "Hours", 0);
|
|
143
|
+
case u:
|
|
144
|
+
return $2(v2 + "Minutes", 1);
|
|
145
|
+
case s:
|
|
146
|
+
return $2(v2 + "Seconds", 2);
|
|
147
|
+
case i:
|
|
148
|
+
return $2(v2 + "Milliseconds", 3);
|
|
149
|
+
default:
|
|
150
|
+
return this.clone();
|
|
151
|
+
}
|
|
152
|
+
}, m2.endOf = function(t2) {
|
|
153
|
+
return this.startOf(t2, false);
|
|
154
|
+
}, m2.$set = function(t2, e2) {
|
|
155
|
+
var n2, o2 = b.p(t2), f2 = "set" + (this.$u ? "UTC" : ""), l2 = (n2 = {}, n2[a] = f2 + "Date", n2[d] = f2 + "Date", n2[c] = f2 + "Month", n2[h] = f2 + "FullYear", n2[u] = f2 + "Hours", n2[s] = f2 + "Minutes", n2[i] = f2 + "Seconds", n2[r] = f2 + "Milliseconds", n2)[o2], $2 = o2 === a ? this.$D + (e2 - this.$W) : e2;
|
|
156
|
+
if (o2 === c || o2 === h) {
|
|
157
|
+
var y2 = this.clone().set(d, 1);
|
|
158
|
+
y2.$d[l2]($2), y2.init(), this.$d = y2.set(d, Math.min(this.$D, y2.daysInMonth())).$d;
|
|
159
|
+
} else l2 && this.$d[l2]($2);
|
|
160
|
+
return this.init(), this;
|
|
161
|
+
}, m2.set = function(t2, e2) {
|
|
162
|
+
return this.clone().$set(t2, e2);
|
|
163
|
+
}, m2.get = function(t2) {
|
|
164
|
+
return this[b.p(t2)]();
|
|
165
|
+
}, m2.add = function(r2, f2) {
|
|
166
|
+
var d2, l2 = this;
|
|
167
|
+
r2 = Number(r2);
|
|
168
|
+
var $2 = b.p(f2), y2 = function(t2) {
|
|
169
|
+
var e2 = O(l2);
|
|
170
|
+
return b.w(e2.date(e2.date() + Math.round(t2 * r2)), l2);
|
|
171
|
+
};
|
|
172
|
+
if ($2 === c) return this.set(c, this.$M + r2);
|
|
173
|
+
if ($2 === h) return this.set(h, this.$y + r2);
|
|
174
|
+
if ($2 === a) return y2(1);
|
|
175
|
+
if ($2 === o) return y2(7);
|
|
176
|
+
var M3 = (d2 = {}, d2[s] = e, d2[u] = n, d2[i] = t, d2)[$2] || 1, m3 = this.$d.getTime() + r2 * M3;
|
|
177
|
+
return b.w(m3, this);
|
|
178
|
+
}, m2.subtract = function(t2, e2) {
|
|
179
|
+
return this.add(-1 * t2, e2);
|
|
180
|
+
}, m2.format = function(t2) {
|
|
181
|
+
var e2 = this, n2 = this.$locale();
|
|
182
|
+
if (!this.isValid()) return n2.invalidDate || l;
|
|
183
|
+
var r2 = t2 || "YYYY-MM-DDTHH:mm:ssZ", i2 = b.z(this), s2 = this.$H, u2 = this.$m, a2 = this.$M, o2 = n2.weekdays, c2 = n2.months, f2 = n2.meridiem, h2 = function(t3, n3, i3, s3) {
|
|
184
|
+
return t3 && (t3[n3] || t3(e2, r2)) || i3[n3].slice(0, s3);
|
|
185
|
+
}, d2 = function(t3) {
|
|
186
|
+
return b.s(s2 % 12 || 12, t3, "0");
|
|
187
|
+
}, $2 = f2 || function(t3, e3, n3) {
|
|
188
|
+
var r3 = t3 < 12 ? "AM" : "PM";
|
|
189
|
+
return n3 ? r3.toLowerCase() : r3;
|
|
190
|
+
};
|
|
191
|
+
return r2.replace(y, (function(t3, r3) {
|
|
192
|
+
return r3 || (function(t4) {
|
|
193
|
+
switch (t4) {
|
|
194
|
+
case "YY":
|
|
195
|
+
return String(e2.$y).slice(-2);
|
|
196
|
+
case "YYYY":
|
|
197
|
+
return b.s(e2.$y, 4, "0");
|
|
198
|
+
case "M":
|
|
199
|
+
return a2 + 1;
|
|
200
|
+
case "MM":
|
|
201
|
+
return b.s(a2 + 1, 2, "0");
|
|
202
|
+
case "MMM":
|
|
203
|
+
return h2(n2.monthsShort, a2, c2, 3);
|
|
204
|
+
case "MMMM":
|
|
205
|
+
return h2(c2, a2);
|
|
206
|
+
case "D":
|
|
207
|
+
return e2.$D;
|
|
208
|
+
case "DD":
|
|
209
|
+
return b.s(e2.$D, 2, "0");
|
|
210
|
+
case "d":
|
|
211
|
+
return String(e2.$W);
|
|
212
|
+
case "dd":
|
|
213
|
+
return h2(n2.weekdaysMin, e2.$W, o2, 2);
|
|
214
|
+
case "ddd":
|
|
215
|
+
return h2(n2.weekdaysShort, e2.$W, o2, 3);
|
|
216
|
+
case "dddd":
|
|
217
|
+
return o2[e2.$W];
|
|
218
|
+
case "H":
|
|
219
|
+
return String(s2);
|
|
220
|
+
case "HH":
|
|
221
|
+
return b.s(s2, 2, "0");
|
|
222
|
+
case "h":
|
|
223
|
+
return d2(1);
|
|
224
|
+
case "hh":
|
|
225
|
+
return d2(2);
|
|
226
|
+
case "a":
|
|
227
|
+
return $2(s2, u2, true);
|
|
228
|
+
case "A":
|
|
229
|
+
return $2(s2, u2, false);
|
|
230
|
+
case "m":
|
|
231
|
+
return String(u2);
|
|
232
|
+
case "mm":
|
|
233
|
+
return b.s(u2, 2, "0");
|
|
234
|
+
case "s":
|
|
235
|
+
return String(e2.$s);
|
|
236
|
+
case "ss":
|
|
237
|
+
return b.s(e2.$s, 2, "0");
|
|
238
|
+
case "SSS":
|
|
239
|
+
return b.s(e2.$ms, 3, "0");
|
|
240
|
+
case "Z":
|
|
241
|
+
return i2;
|
|
242
|
+
}
|
|
243
|
+
return null;
|
|
244
|
+
})(t3) || i2.replace(":", "");
|
|
245
|
+
}));
|
|
246
|
+
}, m2.utcOffset = function() {
|
|
247
|
+
return 15 * -Math.round(this.$d.getTimezoneOffset() / 15);
|
|
248
|
+
}, m2.diff = function(r2, d2, l2) {
|
|
249
|
+
var $2, y2 = this, M3 = b.p(d2), m3 = O(r2), v2 = (m3.utcOffset() - this.utcOffset()) * e, g2 = this - m3, D2 = function() {
|
|
250
|
+
return b.m(y2, m3);
|
|
251
|
+
};
|
|
252
|
+
switch (M3) {
|
|
253
|
+
case h:
|
|
254
|
+
$2 = D2() / 12;
|
|
255
|
+
break;
|
|
256
|
+
case c:
|
|
257
|
+
$2 = D2();
|
|
258
|
+
break;
|
|
259
|
+
case f:
|
|
260
|
+
$2 = D2() / 3;
|
|
261
|
+
break;
|
|
262
|
+
case o:
|
|
263
|
+
$2 = (g2 - v2) / 6048e5;
|
|
264
|
+
break;
|
|
265
|
+
case a:
|
|
266
|
+
$2 = (g2 - v2) / 864e5;
|
|
267
|
+
break;
|
|
268
|
+
case u:
|
|
269
|
+
$2 = g2 / n;
|
|
270
|
+
break;
|
|
271
|
+
case s:
|
|
272
|
+
$2 = g2 / e;
|
|
273
|
+
break;
|
|
274
|
+
case i:
|
|
275
|
+
$2 = g2 / t;
|
|
276
|
+
break;
|
|
277
|
+
default:
|
|
278
|
+
$2 = g2;
|
|
279
|
+
}
|
|
280
|
+
return l2 ? $2 : b.a($2);
|
|
281
|
+
}, m2.daysInMonth = function() {
|
|
282
|
+
return this.endOf(c).$D;
|
|
283
|
+
}, m2.$locale = function() {
|
|
284
|
+
return D[this.$L];
|
|
285
|
+
}, m2.locale = function(t2, e2) {
|
|
286
|
+
if (!t2) return this.$L;
|
|
287
|
+
var n2 = this.clone(), r2 = w(t2, e2, true);
|
|
288
|
+
return r2 && (n2.$L = r2), n2;
|
|
289
|
+
}, m2.clone = function() {
|
|
290
|
+
return b.w(this.$d, this);
|
|
291
|
+
}, m2.toDate = function() {
|
|
292
|
+
return new Date(this.valueOf());
|
|
293
|
+
}, m2.toJSON = function() {
|
|
294
|
+
return this.isValid() ? this.toISOString() : null;
|
|
295
|
+
}, m2.toISOString = function() {
|
|
296
|
+
return this.$d.toISOString();
|
|
297
|
+
}, m2.toString = function() {
|
|
298
|
+
return this.$d.toUTCString();
|
|
299
|
+
}, M2;
|
|
300
|
+
})(), k = _.prototype;
|
|
301
|
+
return O.prototype = k, [["$ms", r], ["$s", i], ["$m", s], ["$H", u], ["$W", a], ["$M", c], ["$y", h], ["$D", d]].forEach((function(t2) {
|
|
302
|
+
k[t2[1]] = function(e2) {
|
|
303
|
+
return this.$g(e2, t2[0], t2[1]);
|
|
304
|
+
};
|
|
305
|
+
})), O.extend = function(t2, e2) {
|
|
306
|
+
return t2.$i || (t2(e2, _, O), t2.$i = true), O;
|
|
307
|
+
}, O.locale = w, O.isDayjs = S, O.unix = function(t2) {
|
|
308
|
+
return O(1e3 * t2);
|
|
309
|
+
}, O.en = D[g], O.Ls = D, O.p = {}, O;
|
|
310
|
+
}));
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
// ../../node_modules/.pnpm/dayjs@1.11.19/node_modules/dayjs/plugin/utc.js
|
|
315
|
+
var require_utc = __commonJS({
|
|
316
|
+
"../../node_modules/.pnpm/dayjs@1.11.19/node_modules/dayjs/plugin/utc.js"(exports2, module2) {
|
|
317
|
+
"use strict";
|
|
318
|
+
!(function(t, i) {
|
|
319
|
+
"object" == typeof exports2 && "undefined" != typeof module2 ? module2.exports = i() : "function" == typeof define && define.amd ? define(i) : (t = "undefined" != typeof globalThis ? globalThis : t || self).dayjs_plugin_utc = i();
|
|
320
|
+
})(exports2, (function() {
|
|
321
|
+
"use strict";
|
|
322
|
+
var t = "minute", i = /[+-]\d\d(?::?\d\d)?/g, e = /([+-]|\d\d)/g;
|
|
323
|
+
return function(s, f, n) {
|
|
324
|
+
var u = f.prototype;
|
|
325
|
+
n.utc = function(t2) {
|
|
326
|
+
var i2 = { date: t2, utc: true, args: arguments };
|
|
327
|
+
return new f(i2);
|
|
328
|
+
}, u.utc = function(i2) {
|
|
329
|
+
var e2 = n(this.toDate(), { locale: this.$L, utc: true });
|
|
330
|
+
return i2 ? e2.add(this.utcOffset(), t) : e2;
|
|
331
|
+
}, u.local = function() {
|
|
332
|
+
return n(this.toDate(), { locale: this.$L, utc: false });
|
|
333
|
+
};
|
|
334
|
+
var r = u.parse;
|
|
335
|
+
u.parse = function(t2) {
|
|
336
|
+
t2.utc && (this.$u = true), this.$utils().u(t2.$offset) || (this.$offset = t2.$offset), r.call(this, t2);
|
|
337
|
+
};
|
|
338
|
+
var o = u.init;
|
|
339
|
+
u.init = function() {
|
|
340
|
+
if (this.$u) {
|
|
341
|
+
var t2 = this.$d;
|
|
342
|
+
this.$y = t2.getUTCFullYear(), this.$M = t2.getUTCMonth(), this.$D = t2.getUTCDate(), this.$W = t2.getUTCDay(), this.$H = t2.getUTCHours(), this.$m = t2.getUTCMinutes(), this.$s = t2.getUTCSeconds(), this.$ms = t2.getUTCMilliseconds();
|
|
343
|
+
} else o.call(this);
|
|
344
|
+
};
|
|
345
|
+
var a = u.utcOffset;
|
|
346
|
+
u.utcOffset = function(s2, f2) {
|
|
347
|
+
var n2 = this.$utils().u;
|
|
348
|
+
if (n2(s2)) return this.$u ? 0 : n2(this.$offset) ? a.call(this) : this.$offset;
|
|
349
|
+
if ("string" == typeof s2 && (s2 = (function(t2) {
|
|
350
|
+
void 0 === t2 && (t2 = "");
|
|
351
|
+
var s3 = t2.match(i);
|
|
352
|
+
if (!s3) return null;
|
|
353
|
+
var f3 = ("" + s3[0]).match(e) || ["-", 0, 0], n3 = f3[0], u3 = 60 * +f3[1] + +f3[2];
|
|
354
|
+
return 0 === u3 ? 0 : "+" === n3 ? u3 : -u3;
|
|
355
|
+
})(s2), null === s2)) return this;
|
|
356
|
+
var u2 = Math.abs(s2) <= 16 ? 60 * s2 : s2;
|
|
357
|
+
if (0 === u2) return this.utc(f2);
|
|
358
|
+
var r2 = this.clone();
|
|
359
|
+
if (f2) return r2.$offset = u2, r2.$u = false, r2;
|
|
360
|
+
var o2 = this.$u ? this.toDate().getTimezoneOffset() : -1 * this.utcOffset();
|
|
361
|
+
return (r2 = this.local().add(u2 + o2, t)).$offset = u2, r2.$x.$localOffset = o2, r2;
|
|
362
|
+
};
|
|
363
|
+
var h = u.format;
|
|
364
|
+
u.format = function(t2) {
|
|
365
|
+
var i2 = t2 || (this.$u ? "YYYY-MM-DDTHH:mm:ss[Z]" : "");
|
|
366
|
+
return h.call(this, i2);
|
|
367
|
+
}, u.valueOf = function() {
|
|
368
|
+
var t2 = this.$utils().u(this.$offset) ? 0 : this.$offset + (this.$x.$localOffset || this.$d.getTimezoneOffset());
|
|
369
|
+
return this.$d.valueOf() - 6e4 * t2;
|
|
370
|
+
}, u.isUTC = function() {
|
|
371
|
+
return !!this.$u;
|
|
372
|
+
}, u.toISOString = function() {
|
|
373
|
+
return this.toDate().toISOString();
|
|
374
|
+
}, u.toString = function() {
|
|
375
|
+
return this.toDate().toUTCString();
|
|
376
|
+
};
|
|
377
|
+
var l = u.toDate;
|
|
378
|
+
u.toDate = function(t2) {
|
|
379
|
+
return "s" === t2 && this.$offset ? n(this.format("YYYY-MM-DD HH:mm:ss:SSS")).toDate() : l.call(this);
|
|
380
|
+
};
|
|
381
|
+
var c = u.diff;
|
|
382
|
+
u.diff = function(t2, i2, e2) {
|
|
383
|
+
if (t2 && this.$u === t2.$u) return c.call(this, t2, i2, e2);
|
|
384
|
+
var s2 = this.local(), f2 = n(t2).local();
|
|
385
|
+
return c.call(s2, f2, i2, e2);
|
|
386
|
+
};
|
|
387
|
+
};
|
|
388
|
+
}));
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
// src/index.ts
|
|
393
|
+
var index_exports = {};
|
|
394
|
+
__export(index_exports, {
|
|
395
|
+
BaseChainAdapter: () => BaseChainAdapter,
|
|
396
|
+
CHAIN_CONFIGS: () => CHAIN_CONFIGS,
|
|
397
|
+
ChainId: () => ChainId,
|
|
398
|
+
ChainType: () => ChainType,
|
|
399
|
+
ConfigError: () => ConfigError,
|
|
400
|
+
ContractError: () => ContractError,
|
|
401
|
+
ErrorCodes: () => ErrorCodes,
|
|
402
|
+
InsufficientFundsError: () => InsufficientFundsError,
|
|
403
|
+
NetworkError: () => NetworkError,
|
|
404
|
+
NetworkType: () => NetworkType,
|
|
405
|
+
PyxisError: () => PyxisError,
|
|
406
|
+
PyxisUtils: () => utils_exports,
|
|
407
|
+
RateLimitError: () => RateLimitError,
|
|
408
|
+
StrategyError: () => StrategyError,
|
|
409
|
+
TransactionError: () => TransactionError,
|
|
410
|
+
ValidationError: () => ValidationError,
|
|
411
|
+
VaultError: () => VaultError,
|
|
412
|
+
WalletError: () => WalletError,
|
|
413
|
+
createError: () => createError,
|
|
414
|
+
getChainConfig: () => getChainConfig
|
|
415
|
+
});
|
|
416
|
+
module.exports = __toCommonJS(index_exports);
|
|
417
|
+
|
|
418
|
+
// src/types/chain.ts
|
|
419
|
+
var NetworkType = /* @__PURE__ */ ((NetworkType2) => {
|
|
420
|
+
NetworkType2["MAINNET"] = "mainnet";
|
|
421
|
+
NetworkType2["TESTNET"] = "testnet";
|
|
422
|
+
return NetworkType2;
|
|
423
|
+
})(NetworkType || {});
|
|
424
|
+
var ChainId = /* @__PURE__ */ ((ChainId2) => {
|
|
425
|
+
ChainId2["APTOS_MAINNET"] = "aptos-mainnet";
|
|
426
|
+
ChainId2["APTOS_TESTNET"] = "aptos-testnet";
|
|
427
|
+
ChainId2["ETHEREUM_MAINNET"] = "ethereum-mainnet";
|
|
428
|
+
ChainId2["ETHEREUM_SEPOLIA"] = "ethereum-sepolia";
|
|
429
|
+
ChainId2["ARBITRUM_ONE"] = "arbitrum-one";
|
|
430
|
+
return ChainId2;
|
|
431
|
+
})(ChainId || {});
|
|
432
|
+
var ChainType = /* @__PURE__ */ ((ChainType3) => {
|
|
433
|
+
ChainType3["EVM"] = "evm";
|
|
434
|
+
ChainType3["MoveVM"] = "move_vm";
|
|
435
|
+
return ChainType3;
|
|
436
|
+
})(ChainType || {});
|
|
437
|
+
var CHAIN_CONFIGS = {
|
|
438
|
+
["aptos-mainnet" /* APTOS_MAINNET */]: {
|
|
439
|
+
type: "move_vm" /* MoveVM */,
|
|
440
|
+
networkType: "mainnet" /* MAINNET */,
|
|
441
|
+
name: "Aptos Mainnet",
|
|
442
|
+
nativeCurrency: { symbol: "APT", decimals: 8, name: "Aptos" },
|
|
443
|
+
blockExplorerUrls: ["https://explorer.aptoslabs.com"],
|
|
444
|
+
isTestnet: false
|
|
445
|
+
},
|
|
446
|
+
["aptos-testnet" /* APTOS_TESTNET */]: {
|
|
447
|
+
type: "move_vm" /* MoveVM */,
|
|
448
|
+
networkType: "testnet" /* TESTNET */,
|
|
449
|
+
name: "Aptos Testnet",
|
|
450
|
+
nativeCurrency: { symbol: "APT", decimals: 8, name: "Aptos" },
|
|
451
|
+
blockExplorerUrls: ["https://explorer.aptoslabs.com/?network=testnet"],
|
|
452
|
+
isTestnet: true
|
|
453
|
+
},
|
|
454
|
+
["ethereum-mainnet" /* ETHEREUM_MAINNET */]: {
|
|
455
|
+
type: "evm" /* EVM */,
|
|
456
|
+
networkType: "mainnet" /* MAINNET */,
|
|
457
|
+
name: "Ethereum Mainnet",
|
|
458
|
+
chainId: 1,
|
|
459
|
+
nativeCurrency: { symbol: "ETH", decimals: 18, name: "Ether" },
|
|
460
|
+
blockExplorerUrls: ["https://etherscan.io"],
|
|
461
|
+
isTestnet: false
|
|
462
|
+
},
|
|
463
|
+
["ethereum-sepolia" /* ETHEREUM_SEPOLIA */]: {
|
|
464
|
+
type: "evm" /* EVM */,
|
|
465
|
+
networkType: "testnet" /* TESTNET */,
|
|
466
|
+
name: "Ethereum Sepolia",
|
|
467
|
+
chainId: 11155111,
|
|
468
|
+
nativeCurrency: { symbol: "ETH", decimals: 18, name: "Ether" },
|
|
469
|
+
blockExplorerUrls: ["https://sepolia.etherscan.io"],
|
|
470
|
+
isTestnet: true
|
|
471
|
+
},
|
|
472
|
+
["arbitrum-one" /* ARBITRUM_ONE */]: {
|
|
473
|
+
type: "evm" /* EVM */,
|
|
474
|
+
networkType: "mainnet" /* MAINNET */,
|
|
475
|
+
name: "Arbitrum One",
|
|
476
|
+
chainId: 42161,
|
|
477
|
+
nativeCurrency: { symbol: "ETH", decimals: 18, name: "Ether" },
|
|
478
|
+
blockExplorerUrls: ["https://arbiscan.io"],
|
|
479
|
+
isTestnet: false
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
function getChainConfig(chainId) {
|
|
483
|
+
const config2 = CHAIN_CONFIGS[chainId];
|
|
484
|
+
if (!config2) {
|
|
485
|
+
throw new Error(`Chain config not found for chainId: ${chainId}`);
|
|
486
|
+
}
|
|
487
|
+
return config2;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// src/errors.ts
|
|
491
|
+
var PyxisError = class _PyxisError extends Error {
|
|
492
|
+
constructor(message, code, details) {
|
|
493
|
+
super(message);
|
|
494
|
+
this.name = "PyxisError";
|
|
495
|
+
this.code = code;
|
|
496
|
+
this.details = details || void 0;
|
|
497
|
+
if (Error.captureStackTrace) {
|
|
498
|
+
Error.captureStackTrace(this, _PyxisError);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
var NetworkError = class extends PyxisError {
|
|
503
|
+
constructor(message, details) {
|
|
504
|
+
super(message, "NETWORK_ERROR", details);
|
|
505
|
+
this.name = "NetworkError";
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
var ValidationError = class extends PyxisError {
|
|
509
|
+
constructor(message, field, details) {
|
|
510
|
+
super(message, "VALIDATION_ERROR", details);
|
|
511
|
+
this.name = "ValidationError";
|
|
512
|
+
this.field = field;
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
var InsufficientFundsError = class extends PyxisError {
|
|
516
|
+
constructor(message, requiredAmount, availableAmount, tokenType) {
|
|
517
|
+
super(message, "INSUFFICIENT_FUNDS", {
|
|
518
|
+
requiredAmount,
|
|
519
|
+
availableAmount,
|
|
520
|
+
tokenType
|
|
521
|
+
});
|
|
522
|
+
this.name = "InsufficientFundsError";
|
|
523
|
+
this.requiredAmount = requiredAmount;
|
|
524
|
+
this.availableAmount = availableAmount;
|
|
525
|
+
this.tokenType = tokenType;
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
var WalletError = class extends PyxisError {
|
|
529
|
+
constructor(message, details) {
|
|
530
|
+
super(message, "WALLET_ERROR", details);
|
|
531
|
+
this.name = "WalletError";
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
var ContractError = class extends PyxisError {
|
|
535
|
+
constructor(message, contractAddress, functionName, details) {
|
|
536
|
+
super(message, "CONTRACT_ERROR", details);
|
|
537
|
+
this.name = "ContractError";
|
|
538
|
+
this.contractAddress = contractAddress;
|
|
539
|
+
this.functionName = functionName;
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
var TransactionError = class extends PyxisError {
|
|
543
|
+
constructor(message, txHash, details) {
|
|
544
|
+
super(message, "TRANSACTION_ERROR", details);
|
|
545
|
+
this.name = "TransactionError";
|
|
546
|
+
this.txHash = txHash;
|
|
547
|
+
}
|
|
548
|
+
};
|
|
549
|
+
var VaultError = class extends PyxisError {
|
|
550
|
+
constructor(message, vaultId, details) {
|
|
551
|
+
super(message, "VAULT_ERROR", details);
|
|
552
|
+
this.name = "VaultError";
|
|
553
|
+
this.vaultId = vaultId;
|
|
554
|
+
}
|
|
555
|
+
};
|
|
556
|
+
var StrategyError = class extends PyxisError {
|
|
557
|
+
constructor(message, strategyId, details) {
|
|
558
|
+
super(message, "STRATEGY_ERROR", details);
|
|
559
|
+
this.name = "StrategyError";
|
|
560
|
+
this.strategyId = strategyId;
|
|
561
|
+
}
|
|
562
|
+
};
|
|
563
|
+
var RateLimitError = class extends PyxisError {
|
|
564
|
+
constructor(message, retryAfter) {
|
|
565
|
+
super(message, "RATE_LIMIT_ERROR", { retryAfter });
|
|
566
|
+
this.name = "RateLimitError";
|
|
567
|
+
this.retryAfter = retryAfter;
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
var ConfigError = class extends PyxisError {
|
|
571
|
+
constructor(message, details) {
|
|
572
|
+
super(message, "CONFIG_ERROR", details);
|
|
573
|
+
this.name = "ConfigError";
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
var ErrorCodes = /* @__PURE__ */ ((ErrorCodes2) => {
|
|
577
|
+
ErrorCodes2["NETWORK_ERROR"] = "NETWORK_ERROR";
|
|
578
|
+
ErrorCodes2["VALIDATION_ERROR"] = "VALIDATION_ERROR";
|
|
579
|
+
ErrorCodes2["INSUFFICIENT_FUNDS"] = "INSUFFICIENT_FUNDS";
|
|
580
|
+
ErrorCodes2["WALLET_ERROR"] = "WALLET_ERROR";
|
|
581
|
+
ErrorCodes2["CONTRACT_ERROR"] = "CONTRACT_ERROR";
|
|
582
|
+
ErrorCodes2["TRANSACTION_ERROR"] = "TRANSACTION_ERROR";
|
|
583
|
+
ErrorCodes2["VAULT_ERROR"] = "VAULT_ERROR";
|
|
584
|
+
ErrorCodes2["STRATEGY_ERROR"] = "STRATEGY_ERROR";
|
|
585
|
+
ErrorCodes2["RATE_LIMIT_ERROR"] = "RATE_LIMIT_ERROR";
|
|
586
|
+
ErrorCodes2["CONFIG_ERROR"] = "CONFIG_ERROR";
|
|
587
|
+
return ErrorCodes2;
|
|
588
|
+
})(ErrorCodes || {});
|
|
589
|
+
function createError(type, message, details) {
|
|
590
|
+
switch (type) {
|
|
591
|
+
case "NETWORK_ERROR":
|
|
592
|
+
return new NetworkError(message, details);
|
|
593
|
+
case "VALIDATION_ERROR":
|
|
594
|
+
return new ValidationError(
|
|
595
|
+
message,
|
|
596
|
+
details?.field,
|
|
597
|
+
details
|
|
598
|
+
);
|
|
599
|
+
case "WALLET_ERROR":
|
|
600
|
+
return new WalletError(message, details);
|
|
601
|
+
case "CONTRACT_ERROR":
|
|
602
|
+
return new ContractError(
|
|
603
|
+
message,
|
|
604
|
+
details?.contractAddress,
|
|
605
|
+
details?.functionName,
|
|
606
|
+
details
|
|
607
|
+
);
|
|
608
|
+
case "TRANSACTION_ERROR":
|
|
609
|
+
return new TransactionError(
|
|
610
|
+
message,
|
|
611
|
+
details?.txHash,
|
|
612
|
+
details
|
|
613
|
+
);
|
|
614
|
+
case "VAULT_ERROR":
|
|
615
|
+
return new VaultError(
|
|
616
|
+
message,
|
|
617
|
+
details?.vaultId,
|
|
618
|
+
details
|
|
619
|
+
);
|
|
620
|
+
case "STRATEGY_ERROR":
|
|
621
|
+
return new StrategyError(
|
|
622
|
+
message,
|
|
623
|
+
details?.strategyId,
|
|
624
|
+
details
|
|
625
|
+
);
|
|
626
|
+
case "RATE_LIMIT_ERROR":
|
|
627
|
+
return new RateLimitError(
|
|
628
|
+
message,
|
|
629
|
+
details?.retryAfter
|
|
630
|
+
);
|
|
631
|
+
case "CONFIG_ERROR":
|
|
632
|
+
return new ConfigError(message, details);
|
|
633
|
+
default:
|
|
634
|
+
return new PyxisError(message, type, details);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
// src/base-adapter.ts
|
|
639
|
+
var BaseChainAdapter = class {
|
|
640
|
+
constructor(chain) {
|
|
641
|
+
this.chain = chain;
|
|
642
|
+
this.client = null;
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Get the current client
|
|
646
|
+
*/
|
|
647
|
+
getClient() {
|
|
648
|
+
if (!this.client) {
|
|
649
|
+
throw new Error(`Client not initialized for chain ${this.chain.id}`);
|
|
650
|
+
}
|
|
651
|
+
return this.client;
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Get contract addresses from chain configuration
|
|
655
|
+
* @throws Error if contract addresses are not configured
|
|
656
|
+
*/
|
|
657
|
+
getContractAddresses() {
|
|
658
|
+
if (!this.chain.contractAddresses) {
|
|
659
|
+
throw new Error(
|
|
660
|
+
`Contract addresses not configured for chain ${this.chain.id}. Please provide contractAddresses when creating the adapter.`
|
|
661
|
+
);
|
|
662
|
+
}
|
|
663
|
+
return this.chain.contractAddresses;
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Get a specific contract address
|
|
667
|
+
*/
|
|
668
|
+
getContractAddress(contract) {
|
|
669
|
+
return this.getContractAddresses()[contract];
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* Get the chain type
|
|
673
|
+
*/
|
|
674
|
+
getChainType() {
|
|
675
|
+
const config2 = getChainConfig(this.chain.id);
|
|
676
|
+
return config2.type;
|
|
677
|
+
}
|
|
678
|
+
};
|
|
679
|
+
|
|
680
|
+
// src/utils/index.ts
|
|
681
|
+
var utils_exports = {};
|
|
682
|
+
__export(utils_exports, {
|
|
683
|
+
FLAG_BITS: () => FLAG_BITS,
|
|
684
|
+
SamplingStrategy: () => SamplingStrategy,
|
|
685
|
+
amountWithSlippage: () => amountWithSlippage,
|
|
686
|
+
buildRequestFlags: () => buildRequestFlags,
|
|
687
|
+
calculateApy: () => calculateApy,
|
|
688
|
+
calculateMinimumReceiveAmount: () => calculateMinimumReceiveAmount,
|
|
689
|
+
calculateWithdrawalAmount: () => calculateWithdrawalAmount,
|
|
690
|
+
getActiveFlags: () => getActiveFlags,
|
|
691
|
+
hasFlag: () => hasFlag,
|
|
692
|
+
mapIndexerToExchangeRateEvent: () => mapIndexerToExchangeRateEvent,
|
|
693
|
+
mapIndexerToVaultActivity: () => mapIndexerToVaultActivity,
|
|
694
|
+
mapOnchainToWithdrawalRequest: () => mapOnchainToWithdrawalRequest,
|
|
695
|
+
normalizeAddress: () => normalizeAddress,
|
|
696
|
+
parseRequestFlags: () => parseRequestFlags,
|
|
697
|
+
sampleEventsByDay: () => sampleEventsByDay
|
|
698
|
+
});
|
|
699
|
+
|
|
700
|
+
// ../../node_modules/.pnpm/tiny-invariant@1.3.3/node_modules/tiny-invariant/dist/esm/tiny-invariant.js
|
|
701
|
+
var isProduction = process.env.NODE_ENV === "production";
|
|
702
|
+
var prefix = "Invariant failed";
|
|
703
|
+
function invariant(condition, message) {
|
|
704
|
+
if (condition) {
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
if (isProduction) {
|
|
708
|
+
throw new Error(prefix);
|
|
709
|
+
}
|
|
710
|
+
var provided = typeof message === "function" ? message() : message;
|
|
711
|
+
var value = provided ? "".concat(prefix, ": ").concat(provided) : prefix;
|
|
712
|
+
throw new Error(value);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
// src/utils/number.ts
|
|
716
|
+
var BIP_BASE = 10000n;
|
|
717
|
+
var amountWithSlippage = (rawAmount, slippageBips) => {
|
|
718
|
+
invariant(slippageBips >= 0 && slippageBips < BIP_BASE, "Invalid slippage");
|
|
719
|
+
return rawAmount * (BIP_BASE - slippageBips) / BIP_BASE;
|
|
720
|
+
};
|
|
721
|
+
|
|
722
|
+
// src/utils/withdrawal.ts
|
|
723
|
+
var calculateWithdrawalAmount = ({
|
|
724
|
+
offerAmount,
|
|
725
|
+
price,
|
|
726
|
+
slippage,
|
|
727
|
+
offerDecimals
|
|
728
|
+
}) => {
|
|
729
|
+
const priceWithSlippage = amountWithSlippage(price, slippage);
|
|
730
|
+
return {
|
|
731
|
+
offerAmount,
|
|
732
|
+
// Preserve full offered amount
|
|
733
|
+
price: priceWithSlippage,
|
|
734
|
+
// Price need to be an integer after slippage
|
|
735
|
+
minimumReceive: offerAmount * priceWithSlippage / 10n ** BigInt(offerDecimals)
|
|
736
|
+
// Minimum receive after slippage
|
|
737
|
+
};
|
|
738
|
+
};
|
|
739
|
+
var calculateMinimumReceiveAmount = ({
|
|
740
|
+
wantAmount,
|
|
741
|
+
slippage
|
|
742
|
+
}) => {
|
|
743
|
+
return amountWithSlippage(wantAmount, slippage);
|
|
744
|
+
};
|
|
745
|
+
|
|
746
|
+
// src/utils/address.ts
|
|
747
|
+
function normalizeAddress(address) {
|
|
748
|
+
const hex = address.replace(/^0x/, "");
|
|
749
|
+
return `0x${hex.padStart(64, "0")}`;
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
// src/utils/apy.ts
|
|
753
|
+
var import_dayjs = __toESM(require_dayjs_min());
|
|
754
|
+
|
|
755
|
+
// ../../node_modules/.pnpm/decimal.js@10.6.0/node_modules/decimal.js/decimal.mjs
|
|
756
|
+
var EXP_LIMIT = 9e15;
|
|
757
|
+
var MAX_DIGITS = 1e9;
|
|
758
|
+
var NUMERALS = "0123456789abcdef";
|
|
759
|
+
var LN10 = "2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058";
|
|
760
|
+
var PI = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789";
|
|
761
|
+
var DEFAULTS = {
|
|
762
|
+
// These values must be integers within the stated ranges (inclusive).
|
|
763
|
+
// Most of these values can be changed at run-time using the `Decimal.config` method.
|
|
764
|
+
// The maximum number of significant digits of the result of a calculation or base conversion.
|
|
765
|
+
// E.g. `Decimal.config({ precision: 20 });`
|
|
766
|
+
precision: 20,
|
|
767
|
+
// 1 to MAX_DIGITS
|
|
768
|
+
// The rounding mode used when rounding to `precision`.
|
|
769
|
+
//
|
|
770
|
+
// ROUND_UP 0 Away from zero.
|
|
771
|
+
// ROUND_DOWN 1 Towards zero.
|
|
772
|
+
// ROUND_CEIL 2 Towards +Infinity.
|
|
773
|
+
// ROUND_FLOOR 3 Towards -Infinity.
|
|
774
|
+
// ROUND_HALF_UP 4 Towards nearest neighbour. If equidistant, up.
|
|
775
|
+
// ROUND_HALF_DOWN 5 Towards nearest neighbour. If equidistant, down.
|
|
776
|
+
// ROUND_HALF_EVEN 6 Towards nearest neighbour. If equidistant, towards even neighbour.
|
|
777
|
+
// ROUND_HALF_CEIL 7 Towards nearest neighbour. If equidistant, towards +Infinity.
|
|
778
|
+
// ROUND_HALF_FLOOR 8 Towards nearest neighbour. If equidistant, towards -Infinity.
|
|
779
|
+
//
|
|
780
|
+
// E.g.
|
|
781
|
+
// `Decimal.rounding = 4;`
|
|
782
|
+
// `Decimal.rounding = Decimal.ROUND_HALF_UP;`
|
|
783
|
+
rounding: 4,
|
|
784
|
+
// 0 to 8
|
|
785
|
+
// The modulo mode used when calculating the modulus: a mod n.
|
|
786
|
+
// The quotient (q = a / n) is calculated according to the corresponding rounding mode.
|
|
787
|
+
// The remainder (r) is calculated as: r = a - n * q.
|
|
788
|
+
//
|
|
789
|
+
// UP 0 The remainder is positive if the dividend is negative, else is negative.
|
|
790
|
+
// DOWN 1 The remainder has the same sign as the dividend (JavaScript %).
|
|
791
|
+
// FLOOR 3 The remainder has the same sign as the divisor (Python %).
|
|
792
|
+
// HALF_EVEN 6 The IEEE 754 remainder function.
|
|
793
|
+
// EUCLID 9 Euclidian division. q = sign(n) * floor(a / abs(n)). Always positive.
|
|
794
|
+
//
|
|
795
|
+
// Truncated division (1), floored division (3), the IEEE 754 remainder (6), and Euclidian
|
|
796
|
+
// division (9) are commonly used for the modulus operation. The other rounding modes can also
|
|
797
|
+
// be used, but they may not give useful results.
|
|
798
|
+
modulo: 1,
|
|
799
|
+
// 0 to 9
|
|
800
|
+
// The exponent value at and beneath which `toString` returns exponential notation.
|
|
801
|
+
// JavaScript numbers: -7
|
|
802
|
+
toExpNeg: -7,
|
|
803
|
+
// 0 to -EXP_LIMIT
|
|
804
|
+
// The exponent value at and above which `toString` returns exponential notation.
|
|
805
|
+
// JavaScript numbers: 21
|
|
806
|
+
toExpPos: 21,
|
|
807
|
+
// 0 to EXP_LIMIT
|
|
808
|
+
// The minimum exponent value, beneath which underflow to zero occurs.
|
|
809
|
+
// JavaScript numbers: -324 (5e-324)
|
|
810
|
+
minE: -EXP_LIMIT,
|
|
811
|
+
// -1 to -EXP_LIMIT
|
|
812
|
+
// The maximum exponent value, above which overflow to Infinity occurs.
|
|
813
|
+
// JavaScript numbers: 308 (1.7976931348623157e+308)
|
|
814
|
+
maxE: EXP_LIMIT,
|
|
815
|
+
// 1 to EXP_LIMIT
|
|
816
|
+
// Whether to use cryptographically-secure random number generation, if available.
|
|
817
|
+
crypto: false
|
|
818
|
+
// true/false
|
|
819
|
+
};
|
|
820
|
+
var inexact;
|
|
821
|
+
var quadrant;
|
|
822
|
+
var external = true;
|
|
823
|
+
var decimalError = "[DecimalError] ";
|
|
824
|
+
var invalidArgument = decimalError + "Invalid argument: ";
|
|
825
|
+
var precisionLimitExceeded = decimalError + "Precision limit exceeded";
|
|
826
|
+
var cryptoUnavailable = decimalError + "crypto unavailable";
|
|
827
|
+
var tag = "[object Decimal]";
|
|
828
|
+
var mathfloor = Math.floor;
|
|
829
|
+
var mathpow = Math.pow;
|
|
830
|
+
var isBinary = /^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i;
|
|
831
|
+
var isHex = /^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i;
|
|
832
|
+
var isOctal = /^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i;
|
|
833
|
+
var isDecimal = /^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;
|
|
834
|
+
var BASE = 1e7;
|
|
835
|
+
var LOG_BASE = 7;
|
|
836
|
+
var MAX_SAFE_INTEGER = 9007199254740991;
|
|
837
|
+
var LN10_PRECISION = LN10.length - 1;
|
|
838
|
+
var PI_PRECISION = PI.length - 1;
|
|
839
|
+
var P = { toStringTag: tag };
|
|
840
|
+
P.absoluteValue = P.abs = function() {
|
|
841
|
+
var x = new this.constructor(this);
|
|
842
|
+
if (x.s < 0) x.s = 1;
|
|
843
|
+
return finalise(x);
|
|
844
|
+
};
|
|
845
|
+
P.ceil = function() {
|
|
846
|
+
return finalise(new this.constructor(this), this.e + 1, 2);
|
|
847
|
+
};
|
|
848
|
+
P.clampedTo = P.clamp = function(min2, max2) {
|
|
849
|
+
var k, x = this, Ctor = x.constructor;
|
|
850
|
+
min2 = new Ctor(min2);
|
|
851
|
+
max2 = new Ctor(max2);
|
|
852
|
+
if (!min2.s || !max2.s) return new Ctor(NaN);
|
|
853
|
+
if (min2.gt(max2)) throw Error(invalidArgument + max2);
|
|
854
|
+
k = x.cmp(min2);
|
|
855
|
+
return k < 0 ? min2 : x.cmp(max2) > 0 ? max2 : new Ctor(x);
|
|
856
|
+
};
|
|
857
|
+
P.comparedTo = P.cmp = function(y) {
|
|
858
|
+
var i, j, xdL, ydL, x = this, xd = x.d, yd = (y = new x.constructor(y)).d, xs = x.s, ys = y.s;
|
|
859
|
+
if (!xd || !yd) {
|
|
860
|
+
return !xs || !ys ? NaN : xs !== ys ? xs : xd === yd ? 0 : !xd ^ xs < 0 ? 1 : -1;
|
|
861
|
+
}
|
|
862
|
+
if (!xd[0] || !yd[0]) return xd[0] ? xs : yd[0] ? -ys : 0;
|
|
863
|
+
if (xs !== ys) return xs;
|
|
864
|
+
if (x.e !== y.e) return x.e > y.e ^ xs < 0 ? 1 : -1;
|
|
865
|
+
xdL = xd.length;
|
|
866
|
+
ydL = yd.length;
|
|
867
|
+
for (i = 0, j = xdL < ydL ? xdL : ydL; i < j; ++i) {
|
|
868
|
+
if (xd[i] !== yd[i]) return xd[i] > yd[i] ^ xs < 0 ? 1 : -1;
|
|
869
|
+
}
|
|
870
|
+
return xdL === ydL ? 0 : xdL > ydL ^ xs < 0 ? 1 : -1;
|
|
871
|
+
};
|
|
872
|
+
P.cosine = P.cos = function() {
|
|
873
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
874
|
+
if (!x.d) return new Ctor(NaN);
|
|
875
|
+
if (!x.d[0]) return new Ctor(1);
|
|
876
|
+
pr = Ctor.precision;
|
|
877
|
+
rm = Ctor.rounding;
|
|
878
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
|
|
879
|
+
Ctor.rounding = 1;
|
|
880
|
+
x = cosine(Ctor, toLessThanHalfPi(Ctor, x));
|
|
881
|
+
Ctor.precision = pr;
|
|
882
|
+
Ctor.rounding = rm;
|
|
883
|
+
return finalise(quadrant == 2 || quadrant == 3 ? x.neg() : x, pr, rm, true);
|
|
884
|
+
};
|
|
885
|
+
P.cubeRoot = P.cbrt = function() {
|
|
886
|
+
var e, m, n, r, rep, s, sd, t, t3, t3plusx, x = this, Ctor = x.constructor;
|
|
887
|
+
if (!x.isFinite() || x.isZero()) return new Ctor(x);
|
|
888
|
+
external = false;
|
|
889
|
+
s = x.s * mathpow(x.s * x, 1 / 3);
|
|
890
|
+
if (!s || Math.abs(s) == 1 / 0) {
|
|
891
|
+
n = digitsToString(x.d);
|
|
892
|
+
e = x.e;
|
|
893
|
+
if (s = (e - n.length + 1) % 3) n += s == 1 || s == -2 ? "0" : "00";
|
|
894
|
+
s = mathpow(n, 1 / 3);
|
|
895
|
+
e = mathfloor((e + 1) / 3) - (e % 3 == (e < 0 ? -1 : 2));
|
|
896
|
+
if (s == 1 / 0) {
|
|
897
|
+
n = "5e" + e;
|
|
898
|
+
} else {
|
|
899
|
+
n = s.toExponential();
|
|
900
|
+
n = n.slice(0, n.indexOf("e") + 1) + e;
|
|
901
|
+
}
|
|
902
|
+
r = new Ctor(n);
|
|
903
|
+
r.s = x.s;
|
|
904
|
+
} else {
|
|
905
|
+
r = new Ctor(s.toString());
|
|
906
|
+
}
|
|
907
|
+
sd = (e = Ctor.precision) + 3;
|
|
908
|
+
for (; ; ) {
|
|
909
|
+
t = r;
|
|
910
|
+
t3 = t.times(t).times(t);
|
|
911
|
+
t3plusx = t3.plus(x);
|
|
912
|
+
r = divide(t3plusx.plus(x).times(t), t3plusx.plus(t3), sd + 2, 1);
|
|
913
|
+
if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
|
|
914
|
+
n = n.slice(sd - 3, sd + 1);
|
|
915
|
+
if (n == "9999" || !rep && n == "4999") {
|
|
916
|
+
if (!rep) {
|
|
917
|
+
finalise(t, e + 1, 0);
|
|
918
|
+
if (t.times(t).times(t).eq(x)) {
|
|
919
|
+
r = t;
|
|
920
|
+
break;
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
sd += 4;
|
|
924
|
+
rep = 1;
|
|
925
|
+
} else {
|
|
926
|
+
if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
|
|
927
|
+
finalise(r, e + 1, 1);
|
|
928
|
+
m = !r.times(r).times(r).eq(x);
|
|
929
|
+
}
|
|
930
|
+
break;
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
external = true;
|
|
935
|
+
return finalise(r, e, Ctor.rounding, m);
|
|
936
|
+
};
|
|
937
|
+
P.decimalPlaces = P.dp = function() {
|
|
938
|
+
var w, d = this.d, n = NaN;
|
|
939
|
+
if (d) {
|
|
940
|
+
w = d.length - 1;
|
|
941
|
+
n = (w - mathfloor(this.e / LOG_BASE)) * LOG_BASE;
|
|
942
|
+
w = d[w];
|
|
943
|
+
if (w) for (; w % 10 == 0; w /= 10) n--;
|
|
944
|
+
if (n < 0) n = 0;
|
|
945
|
+
}
|
|
946
|
+
return n;
|
|
947
|
+
};
|
|
948
|
+
P.dividedBy = P.div = function(y) {
|
|
949
|
+
return divide(this, new this.constructor(y));
|
|
950
|
+
};
|
|
951
|
+
P.dividedToIntegerBy = P.divToInt = function(y) {
|
|
952
|
+
var x = this, Ctor = x.constructor;
|
|
953
|
+
return finalise(divide(x, new Ctor(y), 0, 1, 1), Ctor.precision, Ctor.rounding);
|
|
954
|
+
};
|
|
955
|
+
P.equals = P.eq = function(y) {
|
|
956
|
+
return this.cmp(y) === 0;
|
|
957
|
+
};
|
|
958
|
+
P.floor = function() {
|
|
959
|
+
return finalise(new this.constructor(this), this.e + 1, 3);
|
|
960
|
+
};
|
|
961
|
+
P.greaterThan = P.gt = function(y) {
|
|
962
|
+
return this.cmp(y) > 0;
|
|
963
|
+
};
|
|
964
|
+
P.greaterThanOrEqualTo = P.gte = function(y) {
|
|
965
|
+
var k = this.cmp(y);
|
|
966
|
+
return k == 1 || k === 0;
|
|
967
|
+
};
|
|
968
|
+
P.hyperbolicCosine = P.cosh = function() {
|
|
969
|
+
var k, n, pr, rm, len, x = this, Ctor = x.constructor, one = new Ctor(1);
|
|
970
|
+
if (!x.isFinite()) return new Ctor(x.s ? 1 / 0 : NaN);
|
|
971
|
+
if (x.isZero()) return one;
|
|
972
|
+
pr = Ctor.precision;
|
|
973
|
+
rm = Ctor.rounding;
|
|
974
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
|
|
975
|
+
Ctor.rounding = 1;
|
|
976
|
+
len = x.d.length;
|
|
977
|
+
if (len < 32) {
|
|
978
|
+
k = Math.ceil(len / 3);
|
|
979
|
+
n = (1 / tinyPow(4, k)).toString();
|
|
980
|
+
} else {
|
|
981
|
+
k = 16;
|
|
982
|
+
n = "2.3283064365386962890625e-10";
|
|
983
|
+
}
|
|
984
|
+
x = taylorSeries(Ctor, 1, x.times(n), new Ctor(1), true);
|
|
985
|
+
var cosh2_x, i = k, d8 = new Ctor(8);
|
|
986
|
+
for (; i--; ) {
|
|
987
|
+
cosh2_x = x.times(x);
|
|
988
|
+
x = one.minus(cosh2_x.times(d8.minus(cosh2_x.times(d8))));
|
|
989
|
+
}
|
|
990
|
+
return finalise(x, Ctor.precision = pr, Ctor.rounding = rm, true);
|
|
991
|
+
};
|
|
992
|
+
P.hyperbolicSine = P.sinh = function() {
|
|
993
|
+
var k, pr, rm, len, x = this, Ctor = x.constructor;
|
|
994
|
+
if (!x.isFinite() || x.isZero()) return new Ctor(x);
|
|
995
|
+
pr = Ctor.precision;
|
|
996
|
+
rm = Ctor.rounding;
|
|
997
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
|
|
998
|
+
Ctor.rounding = 1;
|
|
999
|
+
len = x.d.length;
|
|
1000
|
+
if (len < 3) {
|
|
1001
|
+
x = taylorSeries(Ctor, 2, x, x, true);
|
|
1002
|
+
} else {
|
|
1003
|
+
k = 1.4 * Math.sqrt(len);
|
|
1004
|
+
k = k > 16 ? 16 : k | 0;
|
|
1005
|
+
x = x.times(1 / tinyPow(5, k));
|
|
1006
|
+
x = taylorSeries(Ctor, 2, x, x, true);
|
|
1007
|
+
var sinh2_x, d5 = new Ctor(5), d16 = new Ctor(16), d20 = new Ctor(20);
|
|
1008
|
+
for (; k--; ) {
|
|
1009
|
+
sinh2_x = x.times(x);
|
|
1010
|
+
x = x.times(d5.plus(sinh2_x.times(d16.times(sinh2_x).plus(d20))));
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
Ctor.precision = pr;
|
|
1014
|
+
Ctor.rounding = rm;
|
|
1015
|
+
return finalise(x, pr, rm, true);
|
|
1016
|
+
};
|
|
1017
|
+
P.hyperbolicTangent = P.tanh = function() {
|
|
1018
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
1019
|
+
if (!x.isFinite()) return new Ctor(x.s);
|
|
1020
|
+
if (x.isZero()) return new Ctor(x);
|
|
1021
|
+
pr = Ctor.precision;
|
|
1022
|
+
rm = Ctor.rounding;
|
|
1023
|
+
Ctor.precision = pr + 7;
|
|
1024
|
+
Ctor.rounding = 1;
|
|
1025
|
+
return divide(x.sinh(), x.cosh(), Ctor.precision = pr, Ctor.rounding = rm);
|
|
1026
|
+
};
|
|
1027
|
+
P.inverseCosine = P.acos = function() {
|
|
1028
|
+
var x = this, Ctor = x.constructor, k = x.abs().cmp(1), pr = Ctor.precision, rm = Ctor.rounding;
|
|
1029
|
+
if (k !== -1) {
|
|
1030
|
+
return k === 0 ? x.isNeg() ? getPi(Ctor, pr, rm) : new Ctor(0) : new Ctor(NaN);
|
|
1031
|
+
}
|
|
1032
|
+
if (x.isZero()) return getPi(Ctor, pr + 4, rm).times(0.5);
|
|
1033
|
+
Ctor.precision = pr + 6;
|
|
1034
|
+
Ctor.rounding = 1;
|
|
1035
|
+
x = new Ctor(1).minus(x).div(x.plus(1)).sqrt().atan();
|
|
1036
|
+
Ctor.precision = pr;
|
|
1037
|
+
Ctor.rounding = rm;
|
|
1038
|
+
return x.times(2);
|
|
1039
|
+
};
|
|
1040
|
+
P.inverseHyperbolicCosine = P.acosh = function() {
|
|
1041
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
1042
|
+
if (x.lte(1)) return new Ctor(x.eq(1) ? 0 : NaN);
|
|
1043
|
+
if (!x.isFinite()) return new Ctor(x);
|
|
1044
|
+
pr = Ctor.precision;
|
|
1045
|
+
rm = Ctor.rounding;
|
|
1046
|
+
Ctor.precision = pr + Math.max(Math.abs(x.e), x.sd()) + 4;
|
|
1047
|
+
Ctor.rounding = 1;
|
|
1048
|
+
external = false;
|
|
1049
|
+
x = x.times(x).minus(1).sqrt().plus(x);
|
|
1050
|
+
external = true;
|
|
1051
|
+
Ctor.precision = pr;
|
|
1052
|
+
Ctor.rounding = rm;
|
|
1053
|
+
return x.ln();
|
|
1054
|
+
};
|
|
1055
|
+
P.inverseHyperbolicSine = P.asinh = function() {
|
|
1056
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
1057
|
+
if (!x.isFinite() || x.isZero()) return new Ctor(x);
|
|
1058
|
+
pr = Ctor.precision;
|
|
1059
|
+
rm = Ctor.rounding;
|
|
1060
|
+
Ctor.precision = pr + 2 * Math.max(Math.abs(x.e), x.sd()) + 6;
|
|
1061
|
+
Ctor.rounding = 1;
|
|
1062
|
+
external = false;
|
|
1063
|
+
x = x.times(x).plus(1).sqrt().plus(x);
|
|
1064
|
+
external = true;
|
|
1065
|
+
Ctor.precision = pr;
|
|
1066
|
+
Ctor.rounding = rm;
|
|
1067
|
+
return x.ln();
|
|
1068
|
+
};
|
|
1069
|
+
P.inverseHyperbolicTangent = P.atanh = function() {
|
|
1070
|
+
var pr, rm, wpr, xsd, x = this, Ctor = x.constructor;
|
|
1071
|
+
if (!x.isFinite()) return new Ctor(NaN);
|
|
1072
|
+
if (x.e >= 0) return new Ctor(x.abs().eq(1) ? x.s / 0 : x.isZero() ? x : NaN);
|
|
1073
|
+
pr = Ctor.precision;
|
|
1074
|
+
rm = Ctor.rounding;
|
|
1075
|
+
xsd = x.sd();
|
|
1076
|
+
if (Math.max(xsd, pr) < 2 * -x.e - 1) return finalise(new Ctor(x), pr, rm, true);
|
|
1077
|
+
Ctor.precision = wpr = xsd - x.e;
|
|
1078
|
+
x = divide(x.plus(1), new Ctor(1).minus(x), wpr + pr, 1);
|
|
1079
|
+
Ctor.precision = pr + 4;
|
|
1080
|
+
Ctor.rounding = 1;
|
|
1081
|
+
x = x.ln();
|
|
1082
|
+
Ctor.precision = pr;
|
|
1083
|
+
Ctor.rounding = rm;
|
|
1084
|
+
return x.times(0.5);
|
|
1085
|
+
};
|
|
1086
|
+
P.inverseSine = P.asin = function() {
|
|
1087
|
+
var halfPi, k, pr, rm, x = this, Ctor = x.constructor;
|
|
1088
|
+
if (x.isZero()) return new Ctor(x);
|
|
1089
|
+
k = x.abs().cmp(1);
|
|
1090
|
+
pr = Ctor.precision;
|
|
1091
|
+
rm = Ctor.rounding;
|
|
1092
|
+
if (k !== -1) {
|
|
1093
|
+
if (k === 0) {
|
|
1094
|
+
halfPi = getPi(Ctor, pr + 4, rm).times(0.5);
|
|
1095
|
+
halfPi.s = x.s;
|
|
1096
|
+
return halfPi;
|
|
1097
|
+
}
|
|
1098
|
+
return new Ctor(NaN);
|
|
1099
|
+
}
|
|
1100
|
+
Ctor.precision = pr + 6;
|
|
1101
|
+
Ctor.rounding = 1;
|
|
1102
|
+
x = x.div(new Ctor(1).minus(x.times(x)).sqrt().plus(1)).atan();
|
|
1103
|
+
Ctor.precision = pr;
|
|
1104
|
+
Ctor.rounding = rm;
|
|
1105
|
+
return x.times(2);
|
|
1106
|
+
};
|
|
1107
|
+
P.inverseTangent = P.atan = function() {
|
|
1108
|
+
var i, j, k, n, px, t, r, wpr, x2, x = this, Ctor = x.constructor, pr = Ctor.precision, rm = Ctor.rounding;
|
|
1109
|
+
if (!x.isFinite()) {
|
|
1110
|
+
if (!x.s) return new Ctor(NaN);
|
|
1111
|
+
if (pr + 4 <= PI_PRECISION) {
|
|
1112
|
+
r = getPi(Ctor, pr + 4, rm).times(0.5);
|
|
1113
|
+
r.s = x.s;
|
|
1114
|
+
return r;
|
|
1115
|
+
}
|
|
1116
|
+
} else if (x.isZero()) {
|
|
1117
|
+
return new Ctor(x);
|
|
1118
|
+
} else if (x.abs().eq(1) && pr + 4 <= PI_PRECISION) {
|
|
1119
|
+
r = getPi(Ctor, pr + 4, rm).times(0.25);
|
|
1120
|
+
r.s = x.s;
|
|
1121
|
+
return r;
|
|
1122
|
+
}
|
|
1123
|
+
Ctor.precision = wpr = pr + 10;
|
|
1124
|
+
Ctor.rounding = 1;
|
|
1125
|
+
k = Math.min(28, wpr / LOG_BASE + 2 | 0);
|
|
1126
|
+
for (i = k; i; --i) x = x.div(x.times(x).plus(1).sqrt().plus(1));
|
|
1127
|
+
external = false;
|
|
1128
|
+
j = Math.ceil(wpr / LOG_BASE);
|
|
1129
|
+
n = 1;
|
|
1130
|
+
x2 = x.times(x);
|
|
1131
|
+
r = new Ctor(x);
|
|
1132
|
+
px = x;
|
|
1133
|
+
for (; i !== -1; ) {
|
|
1134
|
+
px = px.times(x2);
|
|
1135
|
+
t = r.minus(px.div(n += 2));
|
|
1136
|
+
px = px.times(x2);
|
|
1137
|
+
r = t.plus(px.div(n += 2));
|
|
1138
|
+
if (r.d[j] !== void 0) for (i = j; r.d[i] === t.d[i] && i--; ) ;
|
|
1139
|
+
}
|
|
1140
|
+
if (k) r = r.times(2 << k - 1);
|
|
1141
|
+
external = true;
|
|
1142
|
+
return finalise(r, Ctor.precision = pr, Ctor.rounding = rm, true);
|
|
1143
|
+
};
|
|
1144
|
+
P.isFinite = function() {
|
|
1145
|
+
return !!this.d;
|
|
1146
|
+
};
|
|
1147
|
+
P.isInteger = P.isInt = function() {
|
|
1148
|
+
return !!this.d && mathfloor(this.e / LOG_BASE) > this.d.length - 2;
|
|
1149
|
+
};
|
|
1150
|
+
P.isNaN = function() {
|
|
1151
|
+
return !this.s;
|
|
1152
|
+
};
|
|
1153
|
+
P.isNegative = P.isNeg = function() {
|
|
1154
|
+
return this.s < 0;
|
|
1155
|
+
};
|
|
1156
|
+
P.isPositive = P.isPos = function() {
|
|
1157
|
+
return this.s > 0;
|
|
1158
|
+
};
|
|
1159
|
+
P.isZero = function() {
|
|
1160
|
+
return !!this.d && this.d[0] === 0;
|
|
1161
|
+
};
|
|
1162
|
+
P.lessThan = P.lt = function(y) {
|
|
1163
|
+
return this.cmp(y) < 0;
|
|
1164
|
+
};
|
|
1165
|
+
P.lessThanOrEqualTo = P.lte = function(y) {
|
|
1166
|
+
return this.cmp(y) < 1;
|
|
1167
|
+
};
|
|
1168
|
+
P.logarithm = P.log = function(base) {
|
|
1169
|
+
var isBase10, d, denominator, k, inf, num, sd, r, arg = this, Ctor = arg.constructor, pr = Ctor.precision, rm = Ctor.rounding, guard = 5;
|
|
1170
|
+
if (base == null) {
|
|
1171
|
+
base = new Ctor(10);
|
|
1172
|
+
isBase10 = true;
|
|
1173
|
+
} else {
|
|
1174
|
+
base = new Ctor(base);
|
|
1175
|
+
d = base.d;
|
|
1176
|
+
if (base.s < 0 || !d || !d[0] || base.eq(1)) return new Ctor(NaN);
|
|
1177
|
+
isBase10 = base.eq(10);
|
|
1178
|
+
}
|
|
1179
|
+
d = arg.d;
|
|
1180
|
+
if (arg.s < 0 || !d || !d[0] || arg.eq(1)) {
|
|
1181
|
+
return new Ctor(d && !d[0] ? -1 / 0 : arg.s != 1 ? NaN : d ? 0 : 1 / 0);
|
|
1182
|
+
}
|
|
1183
|
+
if (isBase10) {
|
|
1184
|
+
if (d.length > 1) {
|
|
1185
|
+
inf = true;
|
|
1186
|
+
} else {
|
|
1187
|
+
for (k = d[0]; k % 10 === 0; ) k /= 10;
|
|
1188
|
+
inf = k !== 1;
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
external = false;
|
|
1192
|
+
sd = pr + guard;
|
|
1193
|
+
num = naturalLogarithm(arg, sd);
|
|
1194
|
+
denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
|
|
1195
|
+
r = divide(num, denominator, sd, 1);
|
|
1196
|
+
if (checkRoundingDigits(r.d, k = pr, rm)) {
|
|
1197
|
+
do {
|
|
1198
|
+
sd += 10;
|
|
1199
|
+
num = naturalLogarithm(arg, sd);
|
|
1200
|
+
denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
|
|
1201
|
+
r = divide(num, denominator, sd, 1);
|
|
1202
|
+
if (!inf) {
|
|
1203
|
+
if (+digitsToString(r.d).slice(k + 1, k + 15) + 1 == 1e14) {
|
|
1204
|
+
r = finalise(r, pr + 1, 0);
|
|
1205
|
+
}
|
|
1206
|
+
break;
|
|
1207
|
+
}
|
|
1208
|
+
} while (checkRoundingDigits(r.d, k += 10, rm));
|
|
1209
|
+
}
|
|
1210
|
+
external = true;
|
|
1211
|
+
return finalise(r, pr, rm);
|
|
1212
|
+
};
|
|
1213
|
+
P.minus = P.sub = function(y) {
|
|
1214
|
+
var d, e, i, j, k, len, pr, rm, xd, xe, xLTy, yd, x = this, Ctor = x.constructor;
|
|
1215
|
+
y = new Ctor(y);
|
|
1216
|
+
if (!x.d || !y.d) {
|
|
1217
|
+
if (!x.s || !y.s) y = new Ctor(NaN);
|
|
1218
|
+
else if (x.d) y.s = -y.s;
|
|
1219
|
+
else y = new Ctor(y.d || x.s !== y.s ? x : NaN);
|
|
1220
|
+
return y;
|
|
1221
|
+
}
|
|
1222
|
+
if (x.s != y.s) {
|
|
1223
|
+
y.s = -y.s;
|
|
1224
|
+
return x.plus(y);
|
|
1225
|
+
}
|
|
1226
|
+
xd = x.d;
|
|
1227
|
+
yd = y.d;
|
|
1228
|
+
pr = Ctor.precision;
|
|
1229
|
+
rm = Ctor.rounding;
|
|
1230
|
+
if (!xd[0] || !yd[0]) {
|
|
1231
|
+
if (yd[0]) y.s = -y.s;
|
|
1232
|
+
else if (xd[0]) y = new Ctor(x);
|
|
1233
|
+
else return new Ctor(rm === 3 ? -0 : 0);
|
|
1234
|
+
return external ? finalise(y, pr, rm) : y;
|
|
1235
|
+
}
|
|
1236
|
+
e = mathfloor(y.e / LOG_BASE);
|
|
1237
|
+
xe = mathfloor(x.e / LOG_BASE);
|
|
1238
|
+
xd = xd.slice();
|
|
1239
|
+
k = xe - e;
|
|
1240
|
+
if (k) {
|
|
1241
|
+
xLTy = k < 0;
|
|
1242
|
+
if (xLTy) {
|
|
1243
|
+
d = xd;
|
|
1244
|
+
k = -k;
|
|
1245
|
+
len = yd.length;
|
|
1246
|
+
} else {
|
|
1247
|
+
d = yd;
|
|
1248
|
+
e = xe;
|
|
1249
|
+
len = xd.length;
|
|
1250
|
+
}
|
|
1251
|
+
i = Math.max(Math.ceil(pr / LOG_BASE), len) + 2;
|
|
1252
|
+
if (k > i) {
|
|
1253
|
+
k = i;
|
|
1254
|
+
d.length = 1;
|
|
1255
|
+
}
|
|
1256
|
+
d.reverse();
|
|
1257
|
+
for (i = k; i--; ) d.push(0);
|
|
1258
|
+
d.reverse();
|
|
1259
|
+
} else {
|
|
1260
|
+
i = xd.length;
|
|
1261
|
+
len = yd.length;
|
|
1262
|
+
xLTy = i < len;
|
|
1263
|
+
if (xLTy) len = i;
|
|
1264
|
+
for (i = 0; i < len; i++) {
|
|
1265
|
+
if (xd[i] != yd[i]) {
|
|
1266
|
+
xLTy = xd[i] < yd[i];
|
|
1267
|
+
break;
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
k = 0;
|
|
1271
|
+
}
|
|
1272
|
+
if (xLTy) {
|
|
1273
|
+
d = xd;
|
|
1274
|
+
xd = yd;
|
|
1275
|
+
yd = d;
|
|
1276
|
+
y.s = -y.s;
|
|
1277
|
+
}
|
|
1278
|
+
len = xd.length;
|
|
1279
|
+
for (i = yd.length - len; i > 0; --i) xd[len++] = 0;
|
|
1280
|
+
for (i = yd.length; i > k; ) {
|
|
1281
|
+
if (xd[--i] < yd[i]) {
|
|
1282
|
+
for (j = i; j && xd[--j] === 0; ) xd[j] = BASE - 1;
|
|
1283
|
+
--xd[j];
|
|
1284
|
+
xd[i] += BASE;
|
|
1285
|
+
}
|
|
1286
|
+
xd[i] -= yd[i];
|
|
1287
|
+
}
|
|
1288
|
+
for (; xd[--len] === 0; ) xd.pop();
|
|
1289
|
+
for (; xd[0] === 0; xd.shift()) --e;
|
|
1290
|
+
if (!xd[0]) return new Ctor(rm === 3 ? -0 : 0);
|
|
1291
|
+
y.d = xd;
|
|
1292
|
+
y.e = getBase10Exponent(xd, e);
|
|
1293
|
+
return external ? finalise(y, pr, rm) : y;
|
|
1294
|
+
};
|
|
1295
|
+
P.modulo = P.mod = function(y) {
|
|
1296
|
+
var q, x = this, Ctor = x.constructor;
|
|
1297
|
+
y = new Ctor(y);
|
|
1298
|
+
if (!x.d || !y.s || y.d && !y.d[0]) return new Ctor(NaN);
|
|
1299
|
+
if (!y.d || x.d && !x.d[0]) {
|
|
1300
|
+
return finalise(new Ctor(x), Ctor.precision, Ctor.rounding);
|
|
1301
|
+
}
|
|
1302
|
+
external = false;
|
|
1303
|
+
if (Ctor.modulo == 9) {
|
|
1304
|
+
q = divide(x, y.abs(), 0, 3, 1);
|
|
1305
|
+
q.s *= y.s;
|
|
1306
|
+
} else {
|
|
1307
|
+
q = divide(x, y, 0, Ctor.modulo, 1);
|
|
1308
|
+
}
|
|
1309
|
+
q = q.times(y);
|
|
1310
|
+
external = true;
|
|
1311
|
+
return x.minus(q);
|
|
1312
|
+
};
|
|
1313
|
+
P.naturalExponential = P.exp = function() {
|
|
1314
|
+
return naturalExponential(this);
|
|
1315
|
+
};
|
|
1316
|
+
P.naturalLogarithm = P.ln = function() {
|
|
1317
|
+
return naturalLogarithm(this);
|
|
1318
|
+
};
|
|
1319
|
+
P.negated = P.neg = function() {
|
|
1320
|
+
var x = new this.constructor(this);
|
|
1321
|
+
x.s = -x.s;
|
|
1322
|
+
return finalise(x);
|
|
1323
|
+
};
|
|
1324
|
+
P.plus = P.add = function(y) {
|
|
1325
|
+
var carry, d, e, i, k, len, pr, rm, xd, yd, x = this, Ctor = x.constructor;
|
|
1326
|
+
y = new Ctor(y);
|
|
1327
|
+
if (!x.d || !y.d) {
|
|
1328
|
+
if (!x.s || !y.s) y = new Ctor(NaN);
|
|
1329
|
+
else if (!x.d) y = new Ctor(y.d || x.s === y.s ? x : NaN);
|
|
1330
|
+
return y;
|
|
1331
|
+
}
|
|
1332
|
+
if (x.s != y.s) {
|
|
1333
|
+
y.s = -y.s;
|
|
1334
|
+
return x.minus(y);
|
|
1335
|
+
}
|
|
1336
|
+
xd = x.d;
|
|
1337
|
+
yd = y.d;
|
|
1338
|
+
pr = Ctor.precision;
|
|
1339
|
+
rm = Ctor.rounding;
|
|
1340
|
+
if (!xd[0] || !yd[0]) {
|
|
1341
|
+
if (!yd[0]) y = new Ctor(x);
|
|
1342
|
+
return external ? finalise(y, pr, rm) : y;
|
|
1343
|
+
}
|
|
1344
|
+
k = mathfloor(x.e / LOG_BASE);
|
|
1345
|
+
e = mathfloor(y.e / LOG_BASE);
|
|
1346
|
+
xd = xd.slice();
|
|
1347
|
+
i = k - e;
|
|
1348
|
+
if (i) {
|
|
1349
|
+
if (i < 0) {
|
|
1350
|
+
d = xd;
|
|
1351
|
+
i = -i;
|
|
1352
|
+
len = yd.length;
|
|
1353
|
+
} else {
|
|
1354
|
+
d = yd;
|
|
1355
|
+
e = k;
|
|
1356
|
+
len = xd.length;
|
|
1357
|
+
}
|
|
1358
|
+
k = Math.ceil(pr / LOG_BASE);
|
|
1359
|
+
len = k > len ? k + 1 : len + 1;
|
|
1360
|
+
if (i > len) {
|
|
1361
|
+
i = len;
|
|
1362
|
+
d.length = 1;
|
|
1363
|
+
}
|
|
1364
|
+
d.reverse();
|
|
1365
|
+
for (; i--; ) d.push(0);
|
|
1366
|
+
d.reverse();
|
|
1367
|
+
}
|
|
1368
|
+
len = xd.length;
|
|
1369
|
+
i = yd.length;
|
|
1370
|
+
if (len - i < 0) {
|
|
1371
|
+
i = len;
|
|
1372
|
+
d = yd;
|
|
1373
|
+
yd = xd;
|
|
1374
|
+
xd = d;
|
|
1375
|
+
}
|
|
1376
|
+
for (carry = 0; i; ) {
|
|
1377
|
+
carry = (xd[--i] = xd[i] + yd[i] + carry) / BASE | 0;
|
|
1378
|
+
xd[i] %= BASE;
|
|
1379
|
+
}
|
|
1380
|
+
if (carry) {
|
|
1381
|
+
xd.unshift(carry);
|
|
1382
|
+
++e;
|
|
1383
|
+
}
|
|
1384
|
+
for (len = xd.length; xd[--len] == 0; ) xd.pop();
|
|
1385
|
+
y.d = xd;
|
|
1386
|
+
y.e = getBase10Exponent(xd, e);
|
|
1387
|
+
return external ? finalise(y, pr, rm) : y;
|
|
1388
|
+
};
|
|
1389
|
+
P.precision = P.sd = function(z) {
|
|
1390
|
+
var k, x = this;
|
|
1391
|
+
if (z !== void 0 && z !== !!z && z !== 1 && z !== 0) throw Error(invalidArgument + z);
|
|
1392
|
+
if (x.d) {
|
|
1393
|
+
k = getPrecision(x.d);
|
|
1394
|
+
if (z && x.e + 1 > k) k = x.e + 1;
|
|
1395
|
+
} else {
|
|
1396
|
+
k = NaN;
|
|
1397
|
+
}
|
|
1398
|
+
return k;
|
|
1399
|
+
};
|
|
1400
|
+
P.round = function() {
|
|
1401
|
+
var x = this, Ctor = x.constructor;
|
|
1402
|
+
return finalise(new Ctor(x), x.e + 1, Ctor.rounding);
|
|
1403
|
+
};
|
|
1404
|
+
P.sine = P.sin = function() {
|
|
1405
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
1406
|
+
if (!x.isFinite()) return new Ctor(NaN);
|
|
1407
|
+
if (x.isZero()) return new Ctor(x);
|
|
1408
|
+
pr = Ctor.precision;
|
|
1409
|
+
rm = Ctor.rounding;
|
|
1410
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
|
|
1411
|
+
Ctor.rounding = 1;
|
|
1412
|
+
x = sine(Ctor, toLessThanHalfPi(Ctor, x));
|
|
1413
|
+
Ctor.precision = pr;
|
|
1414
|
+
Ctor.rounding = rm;
|
|
1415
|
+
return finalise(quadrant > 2 ? x.neg() : x, pr, rm, true);
|
|
1416
|
+
};
|
|
1417
|
+
P.squareRoot = P.sqrt = function() {
|
|
1418
|
+
var m, n, sd, r, rep, t, x = this, d = x.d, e = x.e, s = x.s, Ctor = x.constructor;
|
|
1419
|
+
if (s !== 1 || !d || !d[0]) {
|
|
1420
|
+
return new Ctor(!s || s < 0 && (!d || d[0]) ? NaN : d ? x : 1 / 0);
|
|
1421
|
+
}
|
|
1422
|
+
external = false;
|
|
1423
|
+
s = Math.sqrt(+x);
|
|
1424
|
+
if (s == 0 || s == 1 / 0) {
|
|
1425
|
+
n = digitsToString(d);
|
|
1426
|
+
if ((n.length + e) % 2 == 0) n += "0";
|
|
1427
|
+
s = Math.sqrt(n);
|
|
1428
|
+
e = mathfloor((e + 1) / 2) - (e < 0 || e % 2);
|
|
1429
|
+
if (s == 1 / 0) {
|
|
1430
|
+
n = "5e" + e;
|
|
1431
|
+
} else {
|
|
1432
|
+
n = s.toExponential();
|
|
1433
|
+
n = n.slice(0, n.indexOf("e") + 1) + e;
|
|
1434
|
+
}
|
|
1435
|
+
r = new Ctor(n);
|
|
1436
|
+
} else {
|
|
1437
|
+
r = new Ctor(s.toString());
|
|
1438
|
+
}
|
|
1439
|
+
sd = (e = Ctor.precision) + 3;
|
|
1440
|
+
for (; ; ) {
|
|
1441
|
+
t = r;
|
|
1442
|
+
r = t.plus(divide(x, t, sd + 2, 1)).times(0.5);
|
|
1443
|
+
if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
|
|
1444
|
+
n = n.slice(sd - 3, sd + 1);
|
|
1445
|
+
if (n == "9999" || !rep && n == "4999") {
|
|
1446
|
+
if (!rep) {
|
|
1447
|
+
finalise(t, e + 1, 0);
|
|
1448
|
+
if (t.times(t).eq(x)) {
|
|
1449
|
+
r = t;
|
|
1450
|
+
break;
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
sd += 4;
|
|
1454
|
+
rep = 1;
|
|
1455
|
+
} else {
|
|
1456
|
+
if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
|
|
1457
|
+
finalise(r, e + 1, 1);
|
|
1458
|
+
m = !r.times(r).eq(x);
|
|
1459
|
+
}
|
|
1460
|
+
break;
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
external = true;
|
|
1465
|
+
return finalise(r, e, Ctor.rounding, m);
|
|
1466
|
+
};
|
|
1467
|
+
P.tangent = P.tan = function() {
|
|
1468
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
1469
|
+
if (!x.isFinite()) return new Ctor(NaN);
|
|
1470
|
+
if (x.isZero()) return new Ctor(x);
|
|
1471
|
+
pr = Ctor.precision;
|
|
1472
|
+
rm = Ctor.rounding;
|
|
1473
|
+
Ctor.precision = pr + 10;
|
|
1474
|
+
Ctor.rounding = 1;
|
|
1475
|
+
x = x.sin();
|
|
1476
|
+
x.s = 1;
|
|
1477
|
+
x = divide(x, new Ctor(1).minus(x.times(x)).sqrt(), pr + 10, 0);
|
|
1478
|
+
Ctor.precision = pr;
|
|
1479
|
+
Ctor.rounding = rm;
|
|
1480
|
+
return finalise(quadrant == 2 || quadrant == 4 ? x.neg() : x, pr, rm, true);
|
|
1481
|
+
};
|
|
1482
|
+
P.times = P.mul = function(y) {
|
|
1483
|
+
var carry, e, i, k, r, rL, t, xdL, ydL, x = this, Ctor = x.constructor, xd = x.d, yd = (y = new Ctor(y)).d;
|
|
1484
|
+
y.s *= x.s;
|
|
1485
|
+
if (!xd || !xd[0] || !yd || !yd[0]) {
|
|
1486
|
+
return new Ctor(!y.s || xd && !xd[0] && !yd || yd && !yd[0] && !xd ? NaN : !xd || !yd ? y.s / 0 : y.s * 0);
|
|
1487
|
+
}
|
|
1488
|
+
e = mathfloor(x.e / LOG_BASE) + mathfloor(y.e / LOG_BASE);
|
|
1489
|
+
xdL = xd.length;
|
|
1490
|
+
ydL = yd.length;
|
|
1491
|
+
if (xdL < ydL) {
|
|
1492
|
+
r = xd;
|
|
1493
|
+
xd = yd;
|
|
1494
|
+
yd = r;
|
|
1495
|
+
rL = xdL;
|
|
1496
|
+
xdL = ydL;
|
|
1497
|
+
ydL = rL;
|
|
1498
|
+
}
|
|
1499
|
+
r = [];
|
|
1500
|
+
rL = xdL + ydL;
|
|
1501
|
+
for (i = rL; i--; ) r.push(0);
|
|
1502
|
+
for (i = ydL; --i >= 0; ) {
|
|
1503
|
+
carry = 0;
|
|
1504
|
+
for (k = xdL + i; k > i; ) {
|
|
1505
|
+
t = r[k] + yd[i] * xd[k - i - 1] + carry;
|
|
1506
|
+
r[k--] = t % BASE | 0;
|
|
1507
|
+
carry = t / BASE | 0;
|
|
1508
|
+
}
|
|
1509
|
+
r[k] = (r[k] + carry) % BASE | 0;
|
|
1510
|
+
}
|
|
1511
|
+
for (; !r[--rL]; ) r.pop();
|
|
1512
|
+
if (carry) ++e;
|
|
1513
|
+
else r.shift();
|
|
1514
|
+
y.d = r;
|
|
1515
|
+
y.e = getBase10Exponent(r, e);
|
|
1516
|
+
return external ? finalise(y, Ctor.precision, Ctor.rounding) : y;
|
|
1517
|
+
};
|
|
1518
|
+
P.toBinary = function(sd, rm) {
|
|
1519
|
+
return toStringBinary(this, 2, sd, rm);
|
|
1520
|
+
};
|
|
1521
|
+
P.toDecimalPlaces = P.toDP = function(dp, rm) {
|
|
1522
|
+
var x = this, Ctor = x.constructor;
|
|
1523
|
+
x = new Ctor(x);
|
|
1524
|
+
if (dp === void 0) return x;
|
|
1525
|
+
checkInt32(dp, 0, MAX_DIGITS);
|
|
1526
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
1527
|
+
else checkInt32(rm, 0, 8);
|
|
1528
|
+
return finalise(x, dp + x.e + 1, rm);
|
|
1529
|
+
};
|
|
1530
|
+
P.toExponential = function(dp, rm) {
|
|
1531
|
+
var str, x = this, Ctor = x.constructor;
|
|
1532
|
+
if (dp === void 0) {
|
|
1533
|
+
str = finiteToString(x, true);
|
|
1534
|
+
} else {
|
|
1535
|
+
checkInt32(dp, 0, MAX_DIGITS);
|
|
1536
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
1537
|
+
else checkInt32(rm, 0, 8);
|
|
1538
|
+
x = finalise(new Ctor(x), dp + 1, rm);
|
|
1539
|
+
str = finiteToString(x, true, dp + 1);
|
|
1540
|
+
}
|
|
1541
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
1542
|
+
};
|
|
1543
|
+
P.toFixed = function(dp, rm) {
|
|
1544
|
+
var str, y, x = this, Ctor = x.constructor;
|
|
1545
|
+
if (dp === void 0) {
|
|
1546
|
+
str = finiteToString(x);
|
|
1547
|
+
} else {
|
|
1548
|
+
checkInt32(dp, 0, MAX_DIGITS);
|
|
1549
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
1550
|
+
else checkInt32(rm, 0, 8);
|
|
1551
|
+
y = finalise(new Ctor(x), dp + x.e + 1, rm);
|
|
1552
|
+
str = finiteToString(y, false, dp + y.e + 1);
|
|
1553
|
+
}
|
|
1554
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
1555
|
+
};
|
|
1556
|
+
P.toFraction = function(maxD) {
|
|
1557
|
+
var d, d0, d1, d2, e, k, n, n0, n1, pr, q, r, x = this, xd = x.d, Ctor = x.constructor;
|
|
1558
|
+
if (!xd) return new Ctor(x);
|
|
1559
|
+
n1 = d0 = new Ctor(1);
|
|
1560
|
+
d1 = n0 = new Ctor(0);
|
|
1561
|
+
d = new Ctor(d1);
|
|
1562
|
+
e = d.e = getPrecision(xd) - x.e - 1;
|
|
1563
|
+
k = e % LOG_BASE;
|
|
1564
|
+
d.d[0] = mathpow(10, k < 0 ? LOG_BASE + k : k);
|
|
1565
|
+
if (maxD == null) {
|
|
1566
|
+
maxD = e > 0 ? d : n1;
|
|
1567
|
+
} else {
|
|
1568
|
+
n = new Ctor(maxD);
|
|
1569
|
+
if (!n.isInt() || n.lt(n1)) throw Error(invalidArgument + n);
|
|
1570
|
+
maxD = n.gt(d) ? e > 0 ? d : n1 : n;
|
|
1571
|
+
}
|
|
1572
|
+
external = false;
|
|
1573
|
+
n = new Ctor(digitsToString(xd));
|
|
1574
|
+
pr = Ctor.precision;
|
|
1575
|
+
Ctor.precision = e = xd.length * LOG_BASE * 2;
|
|
1576
|
+
for (; ; ) {
|
|
1577
|
+
q = divide(n, d, 0, 1, 1);
|
|
1578
|
+
d2 = d0.plus(q.times(d1));
|
|
1579
|
+
if (d2.cmp(maxD) == 1) break;
|
|
1580
|
+
d0 = d1;
|
|
1581
|
+
d1 = d2;
|
|
1582
|
+
d2 = n1;
|
|
1583
|
+
n1 = n0.plus(q.times(d2));
|
|
1584
|
+
n0 = d2;
|
|
1585
|
+
d2 = d;
|
|
1586
|
+
d = n.minus(q.times(d2));
|
|
1587
|
+
n = d2;
|
|
1588
|
+
}
|
|
1589
|
+
d2 = divide(maxD.minus(d0), d1, 0, 1, 1);
|
|
1590
|
+
n0 = n0.plus(d2.times(n1));
|
|
1591
|
+
d0 = d0.plus(d2.times(d1));
|
|
1592
|
+
n0.s = n1.s = x.s;
|
|
1593
|
+
r = divide(n1, d1, e, 1).minus(x).abs().cmp(divide(n0, d0, e, 1).minus(x).abs()) < 1 ? [n1, d1] : [n0, d0];
|
|
1594
|
+
Ctor.precision = pr;
|
|
1595
|
+
external = true;
|
|
1596
|
+
return r;
|
|
1597
|
+
};
|
|
1598
|
+
P.toHexadecimal = P.toHex = function(sd, rm) {
|
|
1599
|
+
return toStringBinary(this, 16, sd, rm);
|
|
1600
|
+
};
|
|
1601
|
+
P.toNearest = function(y, rm) {
|
|
1602
|
+
var x = this, Ctor = x.constructor;
|
|
1603
|
+
x = new Ctor(x);
|
|
1604
|
+
if (y == null) {
|
|
1605
|
+
if (!x.d) return x;
|
|
1606
|
+
y = new Ctor(1);
|
|
1607
|
+
rm = Ctor.rounding;
|
|
1608
|
+
} else {
|
|
1609
|
+
y = new Ctor(y);
|
|
1610
|
+
if (rm === void 0) {
|
|
1611
|
+
rm = Ctor.rounding;
|
|
1612
|
+
} else {
|
|
1613
|
+
checkInt32(rm, 0, 8);
|
|
1614
|
+
}
|
|
1615
|
+
if (!x.d) return y.s ? x : y;
|
|
1616
|
+
if (!y.d) {
|
|
1617
|
+
if (y.s) y.s = x.s;
|
|
1618
|
+
return y;
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
if (y.d[0]) {
|
|
1622
|
+
external = false;
|
|
1623
|
+
x = divide(x, y, 0, rm, 1).times(y);
|
|
1624
|
+
external = true;
|
|
1625
|
+
finalise(x);
|
|
1626
|
+
} else {
|
|
1627
|
+
y.s = x.s;
|
|
1628
|
+
x = y;
|
|
1629
|
+
}
|
|
1630
|
+
return x;
|
|
1631
|
+
};
|
|
1632
|
+
P.toNumber = function() {
|
|
1633
|
+
return +this;
|
|
1634
|
+
};
|
|
1635
|
+
P.toOctal = function(sd, rm) {
|
|
1636
|
+
return toStringBinary(this, 8, sd, rm);
|
|
1637
|
+
};
|
|
1638
|
+
P.toPower = P.pow = function(y) {
|
|
1639
|
+
var e, k, pr, r, rm, s, x = this, Ctor = x.constructor, yn = +(y = new Ctor(y));
|
|
1640
|
+
if (!x.d || !y.d || !x.d[0] || !y.d[0]) return new Ctor(mathpow(+x, yn));
|
|
1641
|
+
x = new Ctor(x);
|
|
1642
|
+
if (x.eq(1)) return x;
|
|
1643
|
+
pr = Ctor.precision;
|
|
1644
|
+
rm = Ctor.rounding;
|
|
1645
|
+
if (y.eq(1)) return finalise(x, pr, rm);
|
|
1646
|
+
e = mathfloor(y.e / LOG_BASE);
|
|
1647
|
+
if (e >= y.d.length - 1 && (k = yn < 0 ? -yn : yn) <= MAX_SAFE_INTEGER) {
|
|
1648
|
+
r = intPow(Ctor, x, k, pr);
|
|
1649
|
+
return y.s < 0 ? new Ctor(1).div(r) : finalise(r, pr, rm);
|
|
1650
|
+
}
|
|
1651
|
+
s = x.s;
|
|
1652
|
+
if (s < 0) {
|
|
1653
|
+
if (e < y.d.length - 1) return new Ctor(NaN);
|
|
1654
|
+
if ((y.d[e] & 1) == 0) s = 1;
|
|
1655
|
+
if (x.e == 0 && x.d[0] == 1 && x.d.length == 1) {
|
|
1656
|
+
x.s = s;
|
|
1657
|
+
return x;
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
k = mathpow(+x, yn);
|
|
1661
|
+
e = k == 0 || !isFinite(k) ? mathfloor(yn * (Math.log("0." + digitsToString(x.d)) / Math.LN10 + x.e + 1)) : new Ctor(k + "").e;
|
|
1662
|
+
if (e > Ctor.maxE + 1 || e < Ctor.minE - 1) return new Ctor(e > 0 ? s / 0 : 0);
|
|
1663
|
+
external = false;
|
|
1664
|
+
Ctor.rounding = x.s = 1;
|
|
1665
|
+
k = Math.min(12, (e + "").length);
|
|
1666
|
+
r = naturalExponential(y.times(naturalLogarithm(x, pr + k)), pr);
|
|
1667
|
+
if (r.d) {
|
|
1668
|
+
r = finalise(r, pr + 5, 1);
|
|
1669
|
+
if (checkRoundingDigits(r.d, pr, rm)) {
|
|
1670
|
+
e = pr + 10;
|
|
1671
|
+
r = finalise(naturalExponential(y.times(naturalLogarithm(x, e + k)), e), e + 5, 1);
|
|
1672
|
+
if (+digitsToString(r.d).slice(pr + 1, pr + 15) + 1 == 1e14) {
|
|
1673
|
+
r = finalise(r, pr + 1, 0);
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
r.s = s;
|
|
1678
|
+
external = true;
|
|
1679
|
+
Ctor.rounding = rm;
|
|
1680
|
+
return finalise(r, pr, rm);
|
|
1681
|
+
};
|
|
1682
|
+
P.toPrecision = function(sd, rm) {
|
|
1683
|
+
var str, x = this, Ctor = x.constructor;
|
|
1684
|
+
if (sd === void 0) {
|
|
1685
|
+
str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
|
|
1686
|
+
} else {
|
|
1687
|
+
checkInt32(sd, 1, MAX_DIGITS);
|
|
1688
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
1689
|
+
else checkInt32(rm, 0, 8);
|
|
1690
|
+
x = finalise(new Ctor(x), sd, rm);
|
|
1691
|
+
str = finiteToString(x, sd <= x.e || x.e <= Ctor.toExpNeg, sd);
|
|
1692
|
+
}
|
|
1693
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
1694
|
+
};
|
|
1695
|
+
P.toSignificantDigits = P.toSD = function(sd, rm) {
|
|
1696
|
+
var x = this, Ctor = x.constructor;
|
|
1697
|
+
if (sd === void 0) {
|
|
1698
|
+
sd = Ctor.precision;
|
|
1699
|
+
rm = Ctor.rounding;
|
|
1700
|
+
} else {
|
|
1701
|
+
checkInt32(sd, 1, MAX_DIGITS);
|
|
1702
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
1703
|
+
else checkInt32(rm, 0, 8);
|
|
1704
|
+
}
|
|
1705
|
+
return finalise(new Ctor(x), sd, rm);
|
|
1706
|
+
};
|
|
1707
|
+
P.toString = function() {
|
|
1708
|
+
var x = this, Ctor = x.constructor, str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
|
|
1709
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
1710
|
+
};
|
|
1711
|
+
P.truncated = P.trunc = function() {
|
|
1712
|
+
return finalise(new this.constructor(this), this.e + 1, 1);
|
|
1713
|
+
};
|
|
1714
|
+
P.valueOf = P.toJSON = function() {
|
|
1715
|
+
var x = this, Ctor = x.constructor, str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
|
|
1716
|
+
return x.isNeg() ? "-" + str : str;
|
|
1717
|
+
};
|
|
1718
|
+
function digitsToString(d) {
|
|
1719
|
+
var i, k, ws, indexOfLastWord = d.length - 1, str = "", w = d[0];
|
|
1720
|
+
if (indexOfLastWord > 0) {
|
|
1721
|
+
str += w;
|
|
1722
|
+
for (i = 1; i < indexOfLastWord; i++) {
|
|
1723
|
+
ws = d[i] + "";
|
|
1724
|
+
k = LOG_BASE - ws.length;
|
|
1725
|
+
if (k) str += getZeroString(k);
|
|
1726
|
+
str += ws;
|
|
1727
|
+
}
|
|
1728
|
+
w = d[i];
|
|
1729
|
+
ws = w + "";
|
|
1730
|
+
k = LOG_BASE - ws.length;
|
|
1731
|
+
if (k) str += getZeroString(k);
|
|
1732
|
+
} else if (w === 0) {
|
|
1733
|
+
return "0";
|
|
1734
|
+
}
|
|
1735
|
+
for (; w % 10 === 0; ) w /= 10;
|
|
1736
|
+
return str + w;
|
|
1737
|
+
}
|
|
1738
|
+
function checkInt32(i, min2, max2) {
|
|
1739
|
+
if (i !== ~~i || i < min2 || i > max2) {
|
|
1740
|
+
throw Error(invalidArgument + i);
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
function checkRoundingDigits(d, i, rm, repeating) {
|
|
1744
|
+
var di, k, r, rd;
|
|
1745
|
+
for (k = d[0]; k >= 10; k /= 10) --i;
|
|
1746
|
+
if (--i < 0) {
|
|
1747
|
+
i += LOG_BASE;
|
|
1748
|
+
di = 0;
|
|
1749
|
+
} else {
|
|
1750
|
+
di = Math.ceil((i + 1) / LOG_BASE);
|
|
1751
|
+
i %= LOG_BASE;
|
|
1752
|
+
}
|
|
1753
|
+
k = mathpow(10, LOG_BASE - i);
|
|
1754
|
+
rd = d[di] % k | 0;
|
|
1755
|
+
if (repeating == null) {
|
|
1756
|
+
if (i < 3) {
|
|
1757
|
+
if (i == 0) rd = rd / 100 | 0;
|
|
1758
|
+
else if (i == 1) rd = rd / 10 | 0;
|
|
1759
|
+
r = rm < 4 && rd == 99999 || rm > 3 && rd == 49999 || rd == 5e4 || rd == 0;
|
|
1760
|
+
} else {
|
|
1761
|
+
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;
|
|
1762
|
+
}
|
|
1763
|
+
} else {
|
|
1764
|
+
if (i < 4) {
|
|
1765
|
+
if (i == 0) rd = rd / 1e3 | 0;
|
|
1766
|
+
else if (i == 1) rd = rd / 100 | 0;
|
|
1767
|
+
else if (i == 2) rd = rd / 10 | 0;
|
|
1768
|
+
r = (repeating || rm < 4) && rd == 9999 || !repeating && rm > 3 && rd == 4999;
|
|
1769
|
+
} else {
|
|
1770
|
+
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;
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
return r;
|
|
1774
|
+
}
|
|
1775
|
+
function convertBase(str, baseIn, baseOut) {
|
|
1776
|
+
var j, arr = [0], arrL, i = 0, strL = str.length;
|
|
1777
|
+
for (; i < strL; ) {
|
|
1778
|
+
for (arrL = arr.length; arrL--; ) arr[arrL] *= baseIn;
|
|
1779
|
+
arr[0] += NUMERALS.indexOf(str.charAt(i++));
|
|
1780
|
+
for (j = 0; j < arr.length; j++) {
|
|
1781
|
+
if (arr[j] > baseOut - 1) {
|
|
1782
|
+
if (arr[j + 1] === void 0) arr[j + 1] = 0;
|
|
1783
|
+
arr[j + 1] += arr[j] / baseOut | 0;
|
|
1784
|
+
arr[j] %= baseOut;
|
|
1785
|
+
}
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
return arr.reverse();
|
|
1789
|
+
}
|
|
1790
|
+
function cosine(Ctor, x) {
|
|
1791
|
+
var k, len, y;
|
|
1792
|
+
if (x.isZero()) return x;
|
|
1793
|
+
len = x.d.length;
|
|
1794
|
+
if (len < 32) {
|
|
1795
|
+
k = Math.ceil(len / 3);
|
|
1796
|
+
y = (1 / tinyPow(4, k)).toString();
|
|
1797
|
+
} else {
|
|
1798
|
+
k = 16;
|
|
1799
|
+
y = "2.3283064365386962890625e-10";
|
|
1800
|
+
}
|
|
1801
|
+
Ctor.precision += k;
|
|
1802
|
+
x = taylorSeries(Ctor, 1, x.times(y), new Ctor(1));
|
|
1803
|
+
for (var i = k; i--; ) {
|
|
1804
|
+
var cos2x = x.times(x);
|
|
1805
|
+
x = cos2x.times(cos2x).minus(cos2x).times(8).plus(1);
|
|
1806
|
+
}
|
|
1807
|
+
Ctor.precision -= k;
|
|
1808
|
+
return x;
|
|
1809
|
+
}
|
|
1810
|
+
var divide = /* @__PURE__ */ (function() {
|
|
1811
|
+
function multiplyInteger(x, k, base) {
|
|
1812
|
+
var temp, carry = 0, i = x.length;
|
|
1813
|
+
for (x = x.slice(); i--; ) {
|
|
1814
|
+
temp = x[i] * k + carry;
|
|
1815
|
+
x[i] = temp % base | 0;
|
|
1816
|
+
carry = temp / base | 0;
|
|
1817
|
+
}
|
|
1818
|
+
if (carry) x.unshift(carry);
|
|
1819
|
+
return x;
|
|
1820
|
+
}
|
|
1821
|
+
function compare(a, b, aL, bL) {
|
|
1822
|
+
var i, r;
|
|
1823
|
+
if (aL != bL) {
|
|
1824
|
+
r = aL > bL ? 1 : -1;
|
|
1825
|
+
} else {
|
|
1826
|
+
for (i = r = 0; i < aL; i++) {
|
|
1827
|
+
if (a[i] != b[i]) {
|
|
1828
|
+
r = a[i] > b[i] ? 1 : -1;
|
|
1829
|
+
break;
|
|
1830
|
+
}
|
|
1831
|
+
}
|
|
1832
|
+
}
|
|
1833
|
+
return r;
|
|
1834
|
+
}
|
|
1835
|
+
function subtract(a, b, aL, base) {
|
|
1836
|
+
var i = 0;
|
|
1837
|
+
for (; aL--; ) {
|
|
1838
|
+
a[aL] -= i;
|
|
1839
|
+
i = a[aL] < b[aL] ? 1 : 0;
|
|
1840
|
+
a[aL] = i * base + a[aL] - b[aL];
|
|
1841
|
+
}
|
|
1842
|
+
for (; !a[0] && a.length > 1; ) a.shift();
|
|
1843
|
+
}
|
|
1844
|
+
return function(x, y, pr, rm, dp, base) {
|
|
1845
|
+
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;
|
|
1846
|
+
if (!xd || !xd[0] || !yd || !yd[0]) {
|
|
1847
|
+
return new Ctor(
|
|
1848
|
+
// Return NaN if either NaN, or both Infinity or 0.
|
|
1849
|
+
!x.s || !y.s || (xd ? yd && xd[0] == yd[0] : !yd) ? NaN : (
|
|
1850
|
+
// Return ±0 if x is 0 or y is ±Infinity, or return ±Infinity as y is 0.
|
|
1851
|
+
xd && xd[0] == 0 || !yd ? sign2 * 0 : sign2 / 0
|
|
1852
|
+
)
|
|
1853
|
+
);
|
|
1854
|
+
}
|
|
1855
|
+
if (base) {
|
|
1856
|
+
logBase = 1;
|
|
1857
|
+
e = x.e - y.e;
|
|
1858
|
+
} else {
|
|
1859
|
+
base = BASE;
|
|
1860
|
+
logBase = LOG_BASE;
|
|
1861
|
+
e = mathfloor(x.e / logBase) - mathfloor(y.e / logBase);
|
|
1862
|
+
}
|
|
1863
|
+
yL = yd.length;
|
|
1864
|
+
xL = xd.length;
|
|
1865
|
+
q = new Ctor(sign2);
|
|
1866
|
+
qd = q.d = [];
|
|
1867
|
+
for (i = 0; yd[i] == (xd[i] || 0); i++) ;
|
|
1868
|
+
if (yd[i] > (xd[i] || 0)) e--;
|
|
1869
|
+
if (pr == null) {
|
|
1870
|
+
sd = pr = Ctor.precision;
|
|
1871
|
+
rm = Ctor.rounding;
|
|
1872
|
+
} else if (dp) {
|
|
1873
|
+
sd = pr + (x.e - y.e) + 1;
|
|
1874
|
+
} else {
|
|
1875
|
+
sd = pr;
|
|
1876
|
+
}
|
|
1877
|
+
if (sd < 0) {
|
|
1878
|
+
qd.push(1);
|
|
1879
|
+
more = true;
|
|
1880
|
+
} else {
|
|
1881
|
+
sd = sd / logBase + 2 | 0;
|
|
1882
|
+
i = 0;
|
|
1883
|
+
if (yL == 1) {
|
|
1884
|
+
k = 0;
|
|
1885
|
+
yd = yd[0];
|
|
1886
|
+
sd++;
|
|
1887
|
+
for (; (i < xL || k) && sd--; i++) {
|
|
1888
|
+
t = k * base + (xd[i] || 0);
|
|
1889
|
+
qd[i] = t / yd | 0;
|
|
1890
|
+
k = t % yd | 0;
|
|
1891
|
+
}
|
|
1892
|
+
more = k || i < xL;
|
|
1893
|
+
} else {
|
|
1894
|
+
k = base / (yd[0] + 1) | 0;
|
|
1895
|
+
if (k > 1) {
|
|
1896
|
+
yd = multiplyInteger(yd, k, base);
|
|
1897
|
+
xd = multiplyInteger(xd, k, base);
|
|
1898
|
+
yL = yd.length;
|
|
1899
|
+
xL = xd.length;
|
|
1900
|
+
}
|
|
1901
|
+
xi = yL;
|
|
1902
|
+
rem = xd.slice(0, yL);
|
|
1903
|
+
remL = rem.length;
|
|
1904
|
+
for (; remL < yL; ) rem[remL++] = 0;
|
|
1905
|
+
yz = yd.slice();
|
|
1906
|
+
yz.unshift(0);
|
|
1907
|
+
yd0 = yd[0];
|
|
1908
|
+
if (yd[1] >= base / 2) ++yd0;
|
|
1909
|
+
do {
|
|
1910
|
+
k = 0;
|
|
1911
|
+
cmp = compare(yd, rem, yL, remL);
|
|
1912
|
+
if (cmp < 0) {
|
|
1913
|
+
rem0 = rem[0];
|
|
1914
|
+
if (yL != remL) rem0 = rem0 * base + (rem[1] || 0);
|
|
1915
|
+
k = rem0 / yd0 | 0;
|
|
1916
|
+
if (k > 1) {
|
|
1917
|
+
if (k >= base) k = base - 1;
|
|
1918
|
+
prod = multiplyInteger(yd, k, base);
|
|
1919
|
+
prodL = prod.length;
|
|
1920
|
+
remL = rem.length;
|
|
1921
|
+
cmp = compare(prod, rem, prodL, remL);
|
|
1922
|
+
if (cmp == 1) {
|
|
1923
|
+
k--;
|
|
1924
|
+
subtract(prod, yL < prodL ? yz : yd, prodL, base);
|
|
1925
|
+
}
|
|
1926
|
+
} else {
|
|
1927
|
+
if (k == 0) cmp = k = 1;
|
|
1928
|
+
prod = yd.slice();
|
|
1929
|
+
}
|
|
1930
|
+
prodL = prod.length;
|
|
1931
|
+
if (prodL < remL) prod.unshift(0);
|
|
1932
|
+
subtract(rem, prod, remL, base);
|
|
1933
|
+
if (cmp == -1) {
|
|
1934
|
+
remL = rem.length;
|
|
1935
|
+
cmp = compare(yd, rem, yL, remL);
|
|
1936
|
+
if (cmp < 1) {
|
|
1937
|
+
k++;
|
|
1938
|
+
subtract(rem, yL < remL ? yz : yd, remL, base);
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
remL = rem.length;
|
|
1942
|
+
} else if (cmp === 0) {
|
|
1943
|
+
k++;
|
|
1944
|
+
rem = [0];
|
|
1945
|
+
}
|
|
1946
|
+
qd[i++] = k;
|
|
1947
|
+
if (cmp && rem[0]) {
|
|
1948
|
+
rem[remL++] = xd[xi] || 0;
|
|
1949
|
+
} else {
|
|
1950
|
+
rem = [xd[xi]];
|
|
1951
|
+
remL = 1;
|
|
1952
|
+
}
|
|
1953
|
+
} while ((xi++ < xL || rem[0] !== void 0) && sd--);
|
|
1954
|
+
more = rem[0] !== void 0;
|
|
1955
|
+
}
|
|
1956
|
+
if (!qd[0]) qd.shift();
|
|
1957
|
+
}
|
|
1958
|
+
if (logBase == 1) {
|
|
1959
|
+
q.e = e;
|
|
1960
|
+
inexact = more;
|
|
1961
|
+
} else {
|
|
1962
|
+
for (i = 1, k = qd[0]; k >= 10; k /= 10) i++;
|
|
1963
|
+
q.e = i + e * logBase - 1;
|
|
1964
|
+
finalise(q, dp ? pr + q.e + 1 : pr, rm, more);
|
|
1965
|
+
}
|
|
1966
|
+
return q;
|
|
1967
|
+
};
|
|
1968
|
+
})();
|
|
1969
|
+
function finalise(x, sd, rm, isTruncated) {
|
|
1970
|
+
var digits, i, j, k, rd, roundUp, w, xd, xdi, Ctor = x.constructor;
|
|
1971
|
+
out: if (sd != null) {
|
|
1972
|
+
xd = x.d;
|
|
1973
|
+
if (!xd) return x;
|
|
1974
|
+
for (digits = 1, k = xd[0]; k >= 10; k /= 10) digits++;
|
|
1975
|
+
i = sd - digits;
|
|
1976
|
+
if (i < 0) {
|
|
1977
|
+
i += LOG_BASE;
|
|
1978
|
+
j = sd;
|
|
1979
|
+
w = xd[xdi = 0];
|
|
1980
|
+
rd = w / mathpow(10, digits - j - 1) % 10 | 0;
|
|
1981
|
+
} else {
|
|
1982
|
+
xdi = Math.ceil((i + 1) / LOG_BASE);
|
|
1983
|
+
k = xd.length;
|
|
1984
|
+
if (xdi >= k) {
|
|
1985
|
+
if (isTruncated) {
|
|
1986
|
+
for (; k++ <= xdi; ) xd.push(0);
|
|
1987
|
+
w = rd = 0;
|
|
1988
|
+
digits = 1;
|
|
1989
|
+
i %= LOG_BASE;
|
|
1990
|
+
j = i - LOG_BASE + 1;
|
|
1991
|
+
} else {
|
|
1992
|
+
break out;
|
|
1993
|
+
}
|
|
1994
|
+
} else {
|
|
1995
|
+
w = k = xd[xdi];
|
|
1996
|
+
for (digits = 1; k >= 10; k /= 10) digits++;
|
|
1997
|
+
i %= LOG_BASE;
|
|
1998
|
+
j = i - LOG_BASE + digits;
|
|
1999
|
+
rd = j < 0 ? 0 : w / mathpow(10, digits - j - 1) % 10 | 0;
|
|
2000
|
+
}
|
|
2001
|
+
}
|
|
2002
|
+
isTruncated = isTruncated || sd < 0 || xd[xdi + 1] !== void 0 || (j < 0 ? w : w % mathpow(10, digits - j - 1));
|
|
2003
|
+
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.
|
|
2004
|
+
(i > 0 ? j > 0 ? w / mathpow(10, digits - j) : 0 : xd[xdi - 1]) % 10 & 1 || rm == (x.s < 0 ? 8 : 7));
|
|
2005
|
+
if (sd < 1 || !xd[0]) {
|
|
2006
|
+
xd.length = 0;
|
|
2007
|
+
if (roundUp) {
|
|
2008
|
+
sd -= x.e + 1;
|
|
2009
|
+
xd[0] = mathpow(10, (LOG_BASE - sd % LOG_BASE) % LOG_BASE);
|
|
2010
|
+
x.e = -sd || 0;
|
|
2011
|
+
} else {
|
|
2012
|
+
xd[0] = x.e = 0;
|
|
2013
|
+
}
|
|
2014
|
+
return x;
|
|
2015
|
+
}
|
|
2016
|
+
if (i == 0) {
|
|
2017
|
+
xd.length = xdi;
|
|
2018
|
+
k = 1;
|
|
2019
|
+
xdi--;
|
|
2020
|
+
} else {
|
|
2021
|
+
xd.length = xdi + 1;
|
|
2022
|
+
k = mathpow(10, LOG_BASE - i);
|
|
2023
|
+
xd[xdi] = j > 0 ? (w / mathpow(10, digits - j) % mathpow(10, j) | 0) * k : 0;
|
|
2024
|
+
}
|
|
2025
|
+
if (roundUp) {
|
|
2026
|
+
for (; ; ) {
|
|
2027
|
+
if (xdi == 0) {
|
|
2028
|
+
for (i = 1, j = xd[0]; j >= 10; j /= 10) i++;
|
|
2029
|
+
j = xd[0] += k;
|
|
2030
|
+
for (k = 1; j >= 10; j /= 10) k++;
|
|
2031
|
+
if (i != k) {
|
|
2032
|
+
x.e++;
|
|
2033
|
+
if (xd[0] == BASE) xd[0] = 1;
|
|
2034
|
+
}
|
|
2035
|
+
break;
|
|
2036
|
+
} else {
|
|
2037
|
+
xd[xdi] += k;
|
|
2038
|
+
if (xd[xdi] != BASE) break;
|
|
2039
|
+
xd[xdi--] = 0;
|
|
2040
|
+
k = 1;
|
|
2041
|
+
}
|
|
2042
|
+
}
|
|
2043
|
+
}
|
|
2044
|
+
for (i = xd.length; xd[--i] === 0; ) xd.pop();
|
|
2045
|
+
}
|
|
2046
|
+
if (external) {
|
|
2047
|
+
if (x.e > Ctor.maxE) {
|
|
2048
|
+
x.d = null;
|
|
2049
|
+
x.e = NaN;
|
|
2050
|
+
} else if (x.e < Ctor.minE) {
|
|
2051
|
+
x.e = 0;
|
|
2052
|
+
x.d = [0];
|
|
2053
|
+
}
|
|
2054
|
+
}
|
|
2055
|
+
return x;
|
|
2056
|
+
}
|
|
2057
|
+
function finiteToString(x, isExp, sd) {
|
|
2058
|
+
if (!x.isFinite()) return nonFiniteToString(x);
|
|
2059
|
+
var k, e = x.e, str = digitsToString(x.d), len = str.length;
|
|
2060
|
+
if (isExp) {
|
|
2061
|
+
if (sd && (k = sd - len) > 0) {
|
|
2062
|
+
str = str.charAt(0) + "." + str.slice(1) + getZeroString(k);
|
|
2063
|
+
} else if (len > 1) {
|
|
2064
|
+
str = str.charAt(0) + "." + str.slice(1);
|
|
2065
|
+
}
|
|
2066
|
+
str = str + (x.e < 0 ? "e" : "e+") + x.e;
|
|
2067
|
+
} else if (e < 0) {
|
|
2068
|
+
str = "0." + getZeroString(-e - 1) + str;
|
|
2069
|
+
if (sd && (k = sd - len) > 0) str += getZeroString(k);
|
|
2070
|
+
} else if (e >= len) {
|
|
2071
|
+
str += getZeroString(e + 1 - len);
|
|
2072
|
+
if (sd && (k = sd - e - 1) > 0) str = str + "." + getZeroString(k);
|
|
2073
|
+
} else {
|
|
2074
|
+
if ((k = e + 1) < len) str = str.slice(0, k) + "." + str.slice(k);
|
|
2075
|
+
if (sd && (k = sd - len) > 0) {
|
|
2076
|
+
if (e + 1 === len) str += ".";
|
|
2077
|
+
str += getZeroString(k);
|
|
2078
|
+
}
|
|
2079
|
+
}
|
|
2080
|
+
return str;
|
|
2081
|
+
}
|
|
2082
|
+
function getBase10Exponent(digits, e) {
|
|
2083
|
+
var w = digits[0];
|
|
2084
|
+
for (e *= LOG_BASE; w >= 10; w /= 10) e++;
|
|
2085
|
+
return e;
|
|
2086
|
+
}
|
|
2087
|
+
function getLn10(Ctor, sd, pr) {
|
|
2088
|
+
if (sd > LN10_PRECISION) {
|
|
2089
|
+
external = true;
|
|
2090
|
+
if (pr) Ctor.precision = pr;
|
|
2091
|
+
throw Error(precisionLimitExceeded);
|
|
2092
|
+
}
|
|
2093
|
+
return finalise(new Ctor(LN10), sd, 1, true);
|
|
2094
|
+
}
|
|
2095
|
+
function getPi(Ctor, sd, rm) {
|
|
2096
|
+
if (sd > PI_PRECISION) throw Error(precisionLimitExceeded);
|
|
2097
|
+
return finalise(new Ctor(PI), sd, rm, true);
|
|
2098
|
+
}
|
|
2099
|
+
function getPrecision(digits) {
|
|
2100
|
+
var w = digits.length - 1, len = w * LOG_BASE + 1;
|
|
2101
|
+
w = digits[w];
|
|
2102
|
+
if (w) {
|
|
2103
|
+
for (; w % 10 == 0; w /= 10) len--;
|
|
2104
|
+
for (w = digits[0]; w >= 10; w /= 10) len++;
|
|
2105
|
+
}
|
|
2106
|
+
return len;
|
|
2107
|
+
}
|
|
2108
|
+
function getZeroString(k) {
|
|
2109
|
+
var zs = "";
|
|
2110
|
+
for (; k--; ) zs += "0";
|
|
2111
|
+
return zs;
|
|
2112
|
+
}
|
|
2113
|
+
function intPow(Ctor, x, n, pr) {
|
|
2114
|
+
var isTruncated, r = new Ctor(1), k = Math.ceil(pr / LOG_BASE + 4);
|
|
2115
|
+
external = false;
|
|
2116
|
+
for (; ; ) {
|
|
2117
|
+
if (n % 2) {
|
|
2118
|
+
r = r.times(x);
|
|
2119
|
+
if (truncate(r.d, k)) isTruncated = true;
|
|
2120
|
+
}
|
|
2121
|
+
n = mathfloor(n / 2);
|
|
2122
|
+
if (n === 0) {
|
|
2123
|
+
n = r.d.length - 1;
|
|
2124
|
+
if (isTruncated && r.d[n] === 0) ++r.d[n];
|
|
2125
|
+
break;
|
|
2126
|
+
}
|
|
2127
|
+
x = x.times(x);
|
|
2128
|
+
truncate(x.d, k);
|
|
2129
|
+
}
|
|
2130
|
+
external = true;
|
|
2131
|
+
return r;
|
|
2132
|
+
}
|
|
2133
|
+
function isOdd(n) {
|
|
2134
|
+
return n.d[n.d.length - 1] & 1;
|
|
2135
|
+
}
|
|
2136
|
+
function maxOrMin(Ctor, args, n) {
|
|
2137
|
+
var k, y, x = new Ctor(args[0]), i = 0;
|
|
2138
|
+
for (; ++i < args.length; ) {
|
|
2139
|
+
y = new Ctor(args[i]);
|
|
2140
|
+
if (!y.s) {
|
|
2141
|
+
x = y;
|
|
2142
|
+
break;
|
|
2143
|
+
}
|
|
2144
|
+
k = x.cmp(y);
|
|
2145
|
+
if (k === n || k === 0 && x.s === n) {
|
|
2146
|
+
x = y;
|
|
2147
|
+
}
|
|
2148
|
+
}
|
|
2149
|
+
return x;
|
|
2150
|
+
}
|
|
2151
|
+
function naturalExponential(x, sd) {
|
|
2152
|
+
var denominator, guard, j, pow2, sum2, t, wpr, rep = 0, i = 0, k = 0, Ctor = x.constructor, rm = Ctor.rounding, pr = Ctor.precision;
|
|
2153
|
+
if (!x.d || !x.d[0] || x.e > 17) {
|
|
2154
|
+
return new Ctor(x.d ? !x.d[0] ? 1 : x.s < 0 ? 0 : 1 / 0 : x.s ? x.s < 0 ? 0 : x : 0 / 0);
|
|
2155
|
+
}
|
|
2156
|
+
if (sd == null) {
|
|
2157
|
+
external = false;
|
|
2158
|
+
wpr = pr;
|
|
2159
|
+
} else {
|
|
2160
|
+
wpr = sd;
|
|
2161
|
+
}
|
|
2162
|
+
t = new Ctor(0.03125);
|
|
2163
|
+
while (x.e > -2) {
|
|
2164
|
+
x = x.times(t);
|
|
2165
|
+
k += 5;
|
|
2166
|
+
}
|
|
2167
|
+
guard = Math.log(mathpow(2, k)) / Math.LN10 * 2 + 5 | 0;
|
|
2168
|
+
wpr += guard;
|
|
2169
|
+
denominator = pow2 = sum2 = new Ctor(1);
|
|
2170
|
+
Ctor.precision = wpr;
|
|
2171
|
+
for (; ; ) {
|
|
2172
|
+
pow2 = finalise(pow2.times(x), wpr, 1);
|
|
2173
|
+
denominator = denominator.times(++i);
|
|
2174
|
+
t = sum2.plus(divide(pow2, denominator, wpr, 1));
|
|
2175
|
+
if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum2.d).slice(0, wpr)) {
|
|
2176
|
+
j = k;
|
|
2177
|
+
while (j--) sum2 = finalise(sum2.times(sum2), wpr, 1);
|
|
2178
|
+
if (sd == null) {
|
|
2179
|
+
if (rep < 3 && checkRoundingDigits(sum2.d, wpr - guard, rm, rep)) {
|
|
2180
|
+
Ctor.precision = wpr += 10;
|
|
2181
|
+
denominator = pow2 = t = new Ctor(1);
|
|
2182
|
+
i = 0;
|
|
2183
|
+
rep++;
|
|
2184
|
+
} else {
|
|
2185
|
+
return finalise(sum2, Ctor.precision = pr, rm, external = true);
|
|
2186
|
+
}
|
|
2187
|
+
} else {
|
|
2188
|
+
Ctor.precision = pr;
|
|
2189
|
+
return sum2;
|
|
2190
|
+
}
|
|
2191
|
+
}
|
|
2192
|
+
sum2 = t;
|
|
2193
|
+
}
|
|
2194
|
+
}
|
|
2195
|
+
function naturalLogarithm(y, sd) {
|
|
2196
|
+
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;
|
|
2197
|
+
if (x.s < 0 || !xd || !xd[0] || !x.e && xd[0] == 1 && xd.length == 1) {
|
|
2198
|
+
return new Ctor(xd && !xd[0] ? -1 / 0 : x.s != 1 ? NaN : xd ? 0 : x);
|
|
2199
|
+
}
|
|
2200
|
+
if (sd == null) {
|
|
2201
|
+
external = false;
|
|
2202
|
+
wpr = pr;
|
|
2203
|
+
} else {
|
|
2204
|
+
wpr = sd;
|
|
2205
|
+
}
|
|
2206
|
+
Ctor.precision = wpr += guard;
|
|
2207
|
+
c = digitsToString(xd);
|
|
2208
|
+
c0 = c.charAt(0);
|
|
2209
|
+
if (Math.abs(e = x.e) < 15e14) {
|
|
2210
|
+
while (c0 < 7 && c0 != 1 || c0 == 1 && c.charAt(1) > 3) {
|
|
2211
|
+
x = x.times(y);
|
|
2212
|
+
c = digitsToString(x.d);
|
|
2213
|
+
c0 = c.charAt(0);
|
|
2214
|
+
n++;
|
|
2215
|
+
}
|
|
2216
|
+
e = x.e;
|
|
2217
|
+
if (c0 > 1) {
|
|
2218
|
+
x = new Ctor("0." + c);
|
|
2219
|
+
e++;
|
|
2220
|
+
} else {
|
|
2221
|
+
x = new Ctor(c0 + "." + c.slice(1));
|
|
2222
|
+
}
|
|
2223
|
+
} else {
|
|
2224
|
+
t = getLn10(Ctor, wpr + 2, pr).times(e + "");
|
|
2225
|
+
x = naturalLogarithm(new Ctor(c0 + "." + c.slice(1)), wpr - guard).plus(t);
|
|
2226
|
+
Ctor.precision = pr;
|
|
2227
|
+
return sd == null ? finalise(x, pr, rm, external = true) : x;
|
|
2228
|
+
}
|
|
2229
|
+
x1 = x;
|
|
2230
|
+
sum2 = numerator = x = divide(x.minus(1), x.plus(1), wpr, 1);
|
|
2231
|
+
x2 = finalise(x.times(x), wpr, 1);
|
|
2232
|
+
denominator = 3;
|
|
2233
|
+
for (; ; ) {
|
|
2234
|
+
numerator = finalise(numerator.times(x2), wpr, 1);
|
|
2235
|
+
t = sum2.plus(divide(numerator, new Ctor(denominator), wpr, 1));
|
|
2236
|
+
if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum2.d).slice(0, wpr)) {
|
|
2237
|
+
sum2 = sum2.times(2);
|
|
2238
|
+
if (e !== 0) sum2 = sum2.plus(getLn10(Ctor, wpr + 2, pr).times(e + ""));
|
|
2239
|
+
sum2 = divide(sum2, new Ctor(n), wpr, 1);
|
|
2240
|
+
if (sd == null) {
|
|
2241
|
+
if (checkRoundingDigits(sum2.d, wpr - guard, rm, rep)) {
|
|
2242
|
+
Ctor.precision = wpr += guard;
|
|
2243
|
+
t = numerator = x = divide(x1.minus(1), x1.plus(1), wpr, 1);
|
|
2244
|
+
x2 = finalise(x.times(x), wpr, 1);
|
|
2245
|
+
denominator = rep = 1;
|
|
2246
|
+
} else {
|
|
2247
|
+
return finalise(sum2, Ctor.precision = pr, rm, external = true);
|
|
2248
|
+
}
|
|
2249
|
+
} else {
|
|
2250
|
+
Ctor.precision = pr;
|
|
2251
|
+
return sum2;
|
|
2252
|
+
}
|
|
2253
|
+
}
|
|
2254
|
+
sum2 = t;
|
|
2255
|
+
denominator += 2;
|
|
2256
|
+
}
|
|
2257
|
+
}
|
|
2258
|
+
function nonFiniteToString(x) {
|
|
2259
|
+
return String(x.s * x.s / 0);
|
|
2260
|
+
}
|
|
2261
|
+
function parseDecimal(x, str) {
|
|
2262
|
+
var e, i, len;
|
|
2263
|
+
if ((e = str.indexOf(".")) > -1) str = str.replace(".", "");
|
|
2264
|
+
if ((i = str.search(/e/i)) > 0) {
|
|
2265
|
+
if (e < 0) e = i;
|
|
2266
|
+
e += +str.slice(i + 1);
|
|
2267
|
+
str = str.substring(0, i);
|
|
2268
|
+
} else if (e < 0) {
|
|
2269
|
+
e = str.length;
|
|
2270
|
+
}
|
|
2271
|
+
for (i = 0; str.charCodeAt(i) === 48; i++) ;
|
|
2272
|
+
for (len = str.length; str.charCodeAt(len - 1) === 48; --len) ;
|
|
2273
|
+
str = str.slice(i, len);
|
|
2274
|
+
if (str) {
|
|
2275
|
+
len -= i;
|
|
2276
|
+
x.e = e = e - i - 1;
|
|
2277
|
+
x.d = [];
|
|
2278
|
+
i = (e + 1) % LOG_BASE;
|
|
2279
|
+
if (e < 0) i += LOG_BASE;
|
|
2280
|
+
if (i < len) {
|
|
2281
|
+
if (i) x.d.push(+str.slice(0, i));
|
|
2282
|
+
for (len -= LOG_BASE; i < len; ) x.d.push(+str.slice(i, i += LOG_BASE));
|
|
2283
|
+
str = str.slice(i);
|
|
2284
|
+
i = LOG_BASE - str.length;
|
|
2285
|
+
} else {
|
|
2286
|
+
i -= len;
|
|
2287
|
+
}
|
|
2288
|
+
for (; i--; ) str += "0";
|
|
2289
|
+
x.d.push(+str);
|
|
2290
|
+
if (external) {
|
|
2291
|
+
if (x.e > x.constructor.maxE) {
|
|
2292
|
+
x.d = null;
|
|
2293
|
+
x.e = NaN;
|
|
2294
|
+
} else if (x.e < x.constructor.minE) {
|
|
2295
|
+
x.e = 0;
|
|
2296
|
+
x.d = [0];
|
|
2297
|
+
}
|
|
2298
|
+
}
|
|
2299
|
+
} else {
|
|
2300
|
+
x.e = 0;
|
|
2301
|
+
x.d = [0];
|
|
2302
|
+
}
|
|
2303
|
+
return x;
|
|
2304
|
+
}
|
|
2305
|
+
function parseOther(x, str) {
|
|
2306
|
+
var base, Ctor, divisor, i, isFloat, len, p, xd, xe;
|
|
2307
|
+
if (str.indexOf("_") > -1) {
|
|
2308
|
+
str = str.replace(/(\d)_(?=\d)/g, "$1");
|
|
2309
|
+
if (isDecimal.test(str)) return parseDecimal(x, str);
|
|
2310
|
+
} else if (str === "Infinity" || str === "NaN") {
|
|
2311
|
+
if (!+str) x.s = NaN;
|
|
2312
|
+
x.e = NaN;
|
|
2313
|
+
x.d = null;
|
|
2314
|
+
return x;
|
|
2315
|
+
}
|
|
2316
|
+
if (isHex.test(str)) {
|
|
2317
|
+
base = 16;
|
|
2318
|
+
str = str.toLowerCase();
|
|
2319
|
+
} else if (isBinary.test(str)) {
|
|
2320
|
+
base = 2;
|
|
2321
|
+
} else if (isOctal.test(str)) {
|
|
2322
|
+
base = 8;
|
|
2323
|
+
} else {
|
|
2324
|
+
throw Error(invalidArgument + str);
|
|
2325
|
+
}
|
|
2326
|
+
i = str.search(/p/i);
|
|
2327
|
+
if (i > 0) {
|
|
2328
|
+
p = +str.slice(i + 1);
|
|
2329
|
+
str = str.substring(2, i);
|
|
2330
|
+
} else {
|
|
2331
|
+
str = str.slice(2);
|
|
2332
|
+
}
|
|
2333
|
+
i = str.indexOf(".");
|
|
2334
|
+
isFloat = i >= 0;
|
|
2335
|
+
Ctor = x.constructor;
|
|
2336
|
+
if (isFloat) {
|
|
2337
|
+
str = str.replace(".", "");
|
|
2338
|
+
len = str.length;
|
|
2339
|
+
i = len - i;
|
|
2340
|
+
divisor = intPow(Ctor, new Ctor(base), i, i * 2);
|
|
2341
|
+
}
|
|
2342
|
+
xd = convertBase(str, base, BASE);
|
|
2343
|
+
xe = xd.length - 1;
|
|
2344
|
+
for (i = xe; xd[i] === 0; --i) xd.pop();
|
|
2345
|
+
if (i < 0) return new Ctor(x.s * 0);
|
|
2346
|
+
x.e = getBase10Exponent(xd, xe);
|
|
2347
|
+
x.d = xd;
|
|
2348
|
+
external = false;
|
|
2349
|
+
if (isFloat) x = divide(x, divisor, len * 4);
|
|
2350
|
+
if (p) x = x.times(Math.abs(p) < 54 ? mathpow(2, p) : Decimal.pow(2, p));
|
|
2351
|
+
external = true;
|
|
2352
|
+
return x;
|
|
2353
|
+
}
|
|
2354
|
+
function sine(Ctor, x) {
|
|
2355
|
+
var k, len = x.d.length;
|
|
2356
|
+
if (len < 3) {
|
|
2357
|
+
return x.isZero() ? x : taylorSeries(Ctor, 2, x, x);
|
|
2358
|
+
}
|
|
2359
|
+
k = 1.4 * Math.sqrt(len);
|
|
2360
|
+
k = k > 16 ? 16 : k | 0;
|
|
2361
|
+
x = x.times(1 / tinyPow(5, k));
|
|
2362
|
+
x = taylorSeries(Ctor, 2, x, x);
|
|
2363
|
+
var sin2_x, d5 = new Ctor(5), d16 = new Ctor(16), d20 = new Ctor(20);
|
|
2364
|
+
for (; k--; ) {
|
|
2365
|
+
sin2_x = x.times(x);
|
|
2366
|
+
x = x.times(d5.plus(sin2_x.times(d16.times(sin2_x).minus(d20))));
|
|
2367
|
+
}
|
|
2368
|
+
return x;
|
|
2369
|
+
}
|
|
2370
|
+
function taylorSeries(Ctor, n, x, y, isHyperbolic) {
|
|
2371
|
+
var j, t, u, x2, i = 1, pr = Ctor.precision, k = Math.ceil(pr / LOG_BASE);
|
|
2372
|
+
external = false;
|
|
2373
|
+
x2 = x.times(x);
|
|
2374
|
+
u = new Ctor(y);
|
|
2375
|
+
for (; ; ) {
|
|
2376
|
+
t = divide(u.times(x2), new Ctor(n++ * n++), pr, 1);
|
|
2377
|
+
u = isHyperbolic ? y.plus(t) : y.minus(t);
|
|
2378
|
+
y = divide(t.times(x2), new Ctor(n++ * n++), pr, 1);
|
|
2379
|
+
t = u.plus(y);
|
|
2380
|
+
if (t.d[k] !== void 0) {
|
|
2381
|
+
for (j = k; t.d[j] === u.d[j] && j--; ) ;
|
|
2382
|
+
if (j == -1) break;
|
|
2383
|
+
}
|
|
2384
|
+
j = u;
|
|
2385
|
+
u = y;
|
|
2386
|
+
y = t;
|
|
2387
|
+
t = j;
|
|
2388
|
+
i++;
|
|
2389
|
+
}
|
|
2390
|
+
external = true;
|
|
2391
|
+
t.d.length = k + 1;
|
|
2392
|
+
return t;
|
|
2393
|
+
}
|
|
2394
|
+
function tinyPow(b, e) {
|
|
2395
|
+
var n = b;
|
|
2396
|
+
while (--e) n *= b;
|
|
2397
|
+
return n;
|
|
2398
|
+
}
|
|
2399
|
+
function toLessThanHalfPi(Ctor, x) {
|
|
2400
|
+
var t, isNeg = x.s < 0, pi = getPi(Ctor, Ctor.precision, 1), halfPi = pi.times(0.5);
|
|
2401
|
+
x = x.abs();
|
|
2402
|
+
if (x.lte(halfPi)) {
|
|
2403
|
+
quadrant = isNeg ? 4 : 1;
|
|
2404
|
+
return x;
|
|
2405
|
+
}
|
|
2406
|
+
t = x.divToInt(pi);
|
|
2407
|
+
if (t.isZero()) {
|
|
2408
|
+
quadrant = isNeg ? 3 : 2;
|
|
2409
|
+
} else {
|
|
2410
|
+
x = x.minus(t.times(pi));
|
|
2411
|
+
if (x.lte(halfPi)) {
|
|
2412
|
+
quadrant = isOdd(t) ? isNeg ? 2 : 3 : isNeg ? 4 : 1;
|
|
2413
|
+
return x;
|
|
2414
|
+
}
|
|
2415
|
+
quadrant = isOdd(t) ? isNeg ? 1 : 4 : isNeg ? 3 : 2;
|
|
2416
|
+
}
|
|
2417
|
+
return x.minus(pi).abs();
|
|
2418
|
+
}
|
|
2419
|
+
function toStringBinary(x, baseOut, sd, rm) {
|
|
2420
|
+
var base, e, i, k, len, roundUp, str, xd, y, Ctor = x.constructor, isExp = sd !== void 0;
|
|
2421
|
+
if (isExp) {
|
|
2422
|
+
checkInt32(sd, 1, MAX_DIGITS);
|
|
2423
|
+
if (rm === void 0) rm = Ctor.rounding;
|
|
2424
|
+
else checkInt32(rm, 0, 8);
|
|
2425
|
+
} else {
|
|
2426
|
+
sd = Ctor.precision;
|
|
2427
|
+
rm = Ctor.rounding;
|
|
2428
|
+
}
|
|
2429
|
+
if (!x.isFinite()) {
|
|
2430
|
+
str = nonFiniteToString(x);
|
|
2431
|
+
} else {
|
|
2432
|
+
str = finiteToString(x);
|
|
2433
|
+
i = str.indexOf(".");
|
|
2434
|
+
if (isExp) {
|
|
2435
|
+
base = 2;
|
|
2436
|
+
if (baseOut == 16) {
|
|
2437
|
+
sd = sd * 4 - 3;
|
|
2438
|
+
} else if (baseOut == 8) {
|
|
2439
|
+
sd = sd * 3 - 2;
|
|
2440
|
+
}
|
|
2441
|
+
} else {
|
|
2442
|
+
base = baseOut;
|
|
2443
|
+
}
|
|
2444
|
+
if (i >= 0) {
|
|
2445
|
+
str = str.replace(".", "");
|
|
2446
|
+
y = new Ctor(1);
|
|
2447
|
+
y.e = str.length - i;
|
|
2448
|
+
y.d = convertBase(finiteToString(y), 10, base);
|
|
2449
|
+
y.e = y.d.length;
|
|
2450
|
+
}
|
|
2451
|
+
xd = convertBase(str, 10, base);
|
|
2452
|
+
e = len = xd.length;
|
|
2453
|
+
for (; xd[--len] == 0; ) xd.pop();
|
|
2454
|
+
if (!xd[0]) {
|
|
2455
|
+
str = isExp ? "0p+0" : "0";
|
|
2456
|
+
} else {
|
|
2457
|
+
if (i < 0) {
|
|
2458
|
+
e--;
|
|
2459
|
+
} else {
|
|
2460
|
+
x = new Ctor(x);
|
|
2461
|
+
x.d = xd;
|
|
2462
|
+
x.e = e;
|
|
2463
|
+
x = divide(x, y, sd, rm, 0, base);
|
|
2464
|
+
xd = x.d;
|
|
2465
|
+
e = x.e;
|
|
2466
|
+
roundUp = inexact;
|
|
2467
|
+
}
|
|
2468
|
+
i = xd[sd];
|
|
2469
|
+
k = base / 2;
|
|
2470
|
+
roundUp = roundUp || xd[sd + 1] !== void 0;
|
|
2471
|
+
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));
|
|
2472
|
+
xd.length = sd;
|
|
2473
|
+
if (roundUp) {
|
|
2474
|
+
for (; ++xd[--sd] > base - 1; ) {
|
|
2475
|
+
xd[sd] = 0;
|
|
2476
|
+
if (!sd) {
|
|
2477
|
+
++e;
|
|
2478
|
+
xd.unshift(1);
|
|
2479
|
+
}
|
|
2480
|
+
}
|
|
2481
|
+
}
|
|
2482
|
+
for (len = xd.length; !xd[len - 1]; --len) ;
|
|
2483
|
+
for (i = 0, str = ""; i < len; i++) str += NUMERALS.charAt(xd[i]);
|
|
2484
|
+
if (isExp) {
|
|
2485
|
+
if (len > 1) {
|
|
2486
|
+
if (baseOut == 16 || baseOut == 8) {
|
|
2487
|
+
i = baseOut == 16 ? 4 : 3;
|
|
2488
|
+
for (--len; len % i; len++) str += "0";
|
|
2489
|
+
xd = convertBase(str, base, baseOut);
|
|
2490
|
+
for (len = xd.length; !xd[len - 1]; --len) ;
|
|
2491
|
+
for (i = 1, str = "1."; i < len; i++) str += NUMERALS.charAt(xd[i]);
|
|
2492
|
+
} else {
|
|
2493
|
+
str = str.charAt(0) + "." + str.slice(1);
|
|
2494
|
+
}
|
|
2495
|
+
}
|
|
2496
|
+
str = str + (e < 0 ? "p" : "p+") + e;
|
|
2497
|
+
} else if (e < 0) {
|
|
2498
|
+
for (; ++e; ) str = "0" + str;
|
|
2499
|
+
str = "0." + str;
|
|
2500
|
+
} else {
|
|
2501
|
+
if (++e > len) for (e -= len; e--; ) str += "0";
|
|
2502
|
+
else if (e < len) str = str.slice(0, e) + "." + str.slice(e);
|
|
2503
|
+
}
|
|
2504
|
+
}
|
|
2505
|
+
str = (baseOut == 16 ? "0x" : baseOut == 2 ? "0b" : baseOut == 8 ? "0o" : "") + str;
|
|
2506
|
+
}
|
|
2507
|
+
return x.s < 0 ? "-" + str : str;
|
|
2508
|
+
}
|
|
2509
|
+
function truncate(arr, len) {
|
|
2510
|
+
if (arr.length > len) {
|
|
2511
|
+
arr.length = len;
|
|
2512
|
+
return true;
|
|
2513
|
+
}
|
|
2514
|
+
}
|
|
2515
|
+
function abs(x) {
|
|
2516
|
+
return new this(x).abs();
|
|
2517
|
+
}
|
|
2518
|
+
function acos(x) {
|
|
2519
|
+
return new this(x).acos();
|
|
2520
|
+
}
|
|
2521
|
+
function acosh(x) {
|
|
2522
|
+
return new this(x).acosh();
|
|
2523
|
+
}
|
|
2524
|
+
function add(x, y) {
|
|
2525
|
+
return new this(x).plus(y);
|
|
2526
|
+
}
|
|
2527
|
+
function asin(x) {
|
|
2528
|
+
return new this(x).asin();
|
|
2529
|
+
}
|
|
2530
|
+
function asinh(x) {
|
|
2531
|
+
return new this(x).asinh();
|
|
2532
|
+
}
|
|
2533
|
+
function atan(x) {
|
|
2534
|
+
return new this(x).atan();
|
|
2535
|
+
}
|
|
2536
|
+
function atanh(x) {
|
|
2537
|
+
return new this(x).atanh();
|
|
2538
|
+
}
|
|
2539
|
+
function atan2(y, x) {
|
|
2540
|
+
y = new this(y);
|
|
2541
|
+
x = new this(x);
|
|
2542
|
+
var r, pr = this.precision, rm = this.rounding, wpr = pr + 4;
|
|
2543
|
+
if (!y.s || !x.s) {
|
|
2544
|
+
r = new this(NaN);
|
|
2545
|
+
} else if (!y.d && !x.d) {
|
|
2546
|
+
r = getPi(this, wpr, 1).times(x.s > 0 ? 0.25 : 0.75);
|
|
2547
|
+
r.s = y.s;
|
|
2548
|
+
} else if (!x.d || y.isZero()) {
|
|
2549
|
+
r = x.s < 0 ? getPi(this, pr, rm) : new this(0);
|
|
2550
|
+
r.s = y.s;
|
|
2551
|
+
} else if (!y.d || x.isZero()) {
|
|
2552
|
+
r = getPi(this, wpr, 1).times(0.5);
|
|
2553
|
+
r.s = y.s;
|
|
2554
|
+
} else if (x.s < 0) {
|
|
2555
|
+
this.precision = wpr;
|
|
2556
|
+
this.rounding = 1;
|
|
2557
|
+
r = this.atan(divide(y, x, wpr, 1));
|
|
2558
|
+
x = getPi(this, wpr, 1);
|
|
2559
|
+
this.precision = pr;
|
|
2560
|
+
this.rounding = rm;
|
|
2561
|
+
r = y.s < 0 ? r.minus(x) : r.plus(x);
|
|
2562
|
+
} else {
|
|
2563
|
+
r = this.atan(divide(y, x, wpr, 1));
|
|
2564
|
+
}
|
|
2565
|
+
return r;
|
|
2566
|
+
}
|
|
2567
|
+
function cbrt(x) {
|
|
2568
|
+
return new this(x).cbrt();
|
|
2569
|
+
}
|
|
2570
|
+
function ceil(x) {
|
|
2571
|
+
return finalise(x = new this(x), x.e + 1, 2);
|
|
2572
|
+
}
|
|
2573
|
+
function clamp(x, min2, max2) {
|
|
2574
|
+
return new this(x).clamp(min2, max2);
|
|
2575
|
+
}
|
|
2576
|
+
function config(obj) {
|
|
2577
|
+
if (!obj || typeof obj !== "object") throw Error(decimalError + "Object expected");
|
|
2578
|
+
var i, p, v, useDefaults = obj.defaults === true, ps = [
|
|
2579
|
+
"precision",
|
|
2580
|
+
1,
|
|
2581
|
+
MAX_DIGITS,
|
|
2582
|
+
"rounding",
|
|
2583
|
+
0,
|
|
2584
|
+
8,
|
|
2585
|
+
"toExpNeg",
|
|
2586
|
+
-EXP_LIMIT,
|
|
2587
|
+
0,
|
|
2588
|
+
"toExpPos",
|
|
2589
|
+
0,
|
|
2590
|
+
EXP_LIMIT,
|
|
2591
|
+
"maxE",
|
|
2592
|
+
0,
|
|
2593
|
+
EXP_LIMIT,
|
|
2594
|
+
"minE",
|
|
2595
|
+
-EXP_LIMIT,
|
|
2596
|
+
0,
|
|
2597
|
+
"modulo",
|
|
2598
|
+
0,
|
|
2599
|
+
9
|
|
2600
|
+
];
|
|
2601
|
+
for (i = 0; i < ps.length; i += 3) {
|
|
2602
|
+
if (p = ps[i], useDefaults) this[p] = DEFAULTS[p];
|
|
2603
|
+
if ((v = obj[p]) !== void 0) {
|
|
2604
|
+
if (mathfloor(v) === v && v >= ps[i + 1] && v <= ps[i + 2]) this[p] = v;
|
|
2605
|
+
else throw Error(invalidArgument + p + ": " + v);
|
|
2606
|
+
}
|
|
2607
|
+
}
|
|
2608
|
+
if (p = "crypto", useDefaults) this[p] = DEFAULTS[p];
|
|
2609
|
+
if ((v = obj[p]) !== void 0) {
|
|
2610
|
+
if (v === true || v === false || v === 0 || v === 1) {
|
|
2611
|
+
if (v) {
|
|
2612
|
+
if (typeof crypto != "undefined" && crypto && (crypto.getRandomValues || crypto.randomBytes)) {
|
|
2613
|
+
this[p] = true;
|
|
2614
|
+
} else {
|
|
2615
|
+
throw Error(cryptoUnavailable);
|
|
2616
|
+
}
|
|
2617
|
+
} else {
|
|
2618
|
+
this[p] = false;
|
|
2619
|
+
}
|
|
2620
|
+
} else {
|
|
2621
|
+
throw Error(invalidArgument + p + ": " + v);
|
|
2622
|
+
}
|
|
2623
|
+
}
|
|
2624
|
+
return this;
|
|
2625
|
+
}
|
|
2626
|
+
function cos(x) {
|
|
2627
|
+
return new this(x).cos();
|
|
2628
|
+
}
|
|
2629
|
+
function cosh(x) {
|
|
2630
|
+
return new this(x).cosh();
|
|
2631
|
+
}
|
|
2632
|
+
function clone(obj) {
|
|
2633
|
+
var i, p, ps;
|
|
2634
|
+
function Decimal2(v) {
|
|
2635
|
+
var e, i2, t, x = this;
|
|
2636
|
+
if (!(x instanceof Decimal2)) return new Decimal2(v);
|
|
2637
|
+
x.constructor = Decimal2;
|
|
2638
|
+
if (isDecimalInstance(v)) {
|
|
2639
|
+
x.s = v.s;
|
|
2640
|
+
if (external) {
|
|
2641
|
+
if (!v.d || v.e > Decimal2.maxE) {
|
|
2642
|
+
x.e = NaN;
|
|
2643
|
+
x.d = null;
|
|
2644
|
+
} else if (v.e < Decimal2.minE) {
|
|
2645
|
+
x.e = 0;
|
|
2646
|
+
x.d = [0];
|
|
2647
|
+
} else {
|
|
2648
|
+
x.e = v.e;
|
|
2649
|
+
x.d = v.d.slice();
|
|
2650
|
+
}
|
|
2651
|
+
} else {
|
|
2652
|
+
x.e = v.e;
|
|
2653
|
+
x.d = v.d ? v.d.slice() : v.d;
|
|
2654
|
+
}
|
|
2655
|
+
return;
|
|
2656
|
+
}
|
|
2657
|
+
t = typeof v;
|
|
2658
|
+
if (t === "number") {
|
|
2659
|
+
if (v === 0) {
|
|
2660
|
+
x.s = 1 / v < 0 ? -1 : 1;
|
|
2661
|
+
x.e = 0;
|
|
2662
|
+
x.d = [0];
|
|
2663
|
+
return;
|
|
2664
|
+
}
|
|
2665
|
+
if (v < 0) {
|
|
2666
|
+
v = -v;
|
|
2667
|
+
x.s = -1;
|
|
2668
|
+
} else {
|
|
2669
|
+
x.s = 1;
|
|
2670
|
+
}
|
|
2671
|
+
if (v === ~~v && v < 1e7) {
|
|
2672
|
+
for (e = 0, i2 = v; i2 >= 10; i2 /= 10) e++;
|
|
2673
|
+
if (external) {
|
|
2674
|
+
if (e > Decimal2.maxE) {
|
|
2675
|
+
x.e = NaN;
|
|
2676
|
+
x.d = null;
|
|
2677
|
+
} else if (e < Decimal2.minE) {
|
|
2678
|
+
x.e = 0;
|
|
2679
|
+
x.d = [0];
|
|
2680
|
+
} else {
|
|
2681
|
+
x.e = e;
|
|
2682
|
+
x.d = [v];
|
|
2683
|
+
}
|
|
2684
|
+
} else {
|
|
2685
|
+
x.e = e;
|
|
2686
|
+
x.d = [v];
|
|
2687
|
+
}
|
|
2688
|
+
return;
|
|
2689
|
+
}
|
|
2690
|
+
if (v * 0 !== 0) {
|
|
2691
|
+
if (!v) x.s = NaN;
|
|
2692
|
+
x.e = NaN;
|
|
2693
|
+
x.d = null;
|
|
2694
|
+
return;
|
|
2695
|
+
}
|
|
2696
|
+
return parseDecimal(x, v.toString());
|
|
2697
|
+
}
|
|
2698
|
+
if (t === "string") {
|
|
2699
|
+
if ((i2 = v.charCodeAt(0)) === 45) {
|
|
2700
|
+
v = v.slice(1);
|
|
2701
|
+
x.s = -1;
|
|
2702
|
+
} else {
|
|
2703
|
+
if (i2 === 43) v = v.slice(1);
|
|
2704
|
+
x.s = 1;
|
|
2705
|
+
}
|
|
2706
|
+
return isDecimal.test(v) ? parseDecimal(x, v) : parseOther(x, v);
|
|
2707
|
+
}
|
|
2708
|
+
if (t === "bigint") {
|
|
2709
|
+
if (v < 0) {
|
|
2710
|
+
v = -v;
|
|
2711
|
+
x.s = -1;
|
|
2712
|
+
} else {
|
|
2713
|
+
x.s = 1;
|
|
2714
|
+
}
|
|
2715
|
+
return parseDecimal(x, v.toString());
|
|
2716
|
+
}
|
|
2717
|
+
throw Error(invalidArgument + v);
|
|
2718
|
+
}
|
|
2719
|
+
Decimal2.prototype = P;
|
|
2720
|
+
Decimal2.ROUND_UP = 0;
|
|
2721
|
+
Decimal2.ROUND_DOWN = 1;
|
|
2722
|
+
Decimal2.ROUND_CEIL = 2;
|
|
2723
|
+
Decimal2.ROUND_FLOOR = 3;
|
|
2724
|
+
Decimal2.ROUND_HALF_UP = 4;
|
|
2725
|
+
Decimal2.ROUND_HALF_DOWN = 5;
|
|
2726
|
+
Decimal2.ROUND_HALF_EVEN = 6;
|
|
2727
|
+
Decimal2.ROUND_HALF_CEIL = 7;
|
|
2728
|
+
Decimal2.ROUND_HALF_FLOOR = 8;
|
|
2729
|
+
Decimal2.EUCLID = 9;
|
|
2730
|
+
Decimal2.config = Decimal2.set = config;
|
|
2731
|
+
Decimal2.clone = clone;
|
|
2732
|
+
Decimal2.isDecimal = isDecimalInstance;
|
|
2733
|
+
Decimal2.abs = abs;
|
|
2734
|
+
Decimal2.acos = acos;
|
|
2735
|
+
Decimal2.acosh = acosh;
|
|
2736
|
+
Decimal2.add = add;
|
|
2737
|
+
Decimal2.asin = asin;
|
|
2738
|
+
Decimal2.asinh = asinh;
|
|
2739
|
+
Decimal2.atan = atan;
|
|
2740
|
+
Decimal2.atanh = atanh;
|
|
2741
|
+
Decimal2.atan2 = atan2;
|
|
2742
|
+
Decimal2.cbrt = cbrt;
|
|
2743
|
+
Decimal2.ceil = ceil;
|
|
2744
|
+
Decimal2.clamp = clamp;
|
|
2745
|
+
Decimal2.cos = cos;
|
|
2746
|
+
Decimal2.cosh = cosh;
|
|
2747
|
+
Decimal2.div = div;
|
|
2748
|
+
Decimal2.exp = exp;
|
|
2749
|
+
Decimal2.floor = floor;
|
|
2750
|
+
Decimal2.hypot = hypot;
|
|
2751
|
+
Decimal2.ln = ln;
|
|
2752
|
+
Decimal2.log = log;
|
|
2753
|
+
Decimal2.log10 = log10;
|
|
2754
|
+
Decimal2.log2 = log2;
|
|
2755
|
+
Decimal2.max = max;
|
|
2756
|
+
Decimal2.min = min;
|
|
2757
|
+
Decimal2.mod = mod;
|
|
2758
|
+
Decimal2.mul = mul;
|
|
2759
|
+
Decimal2.pow = pow;
|
|
2760
|
+
Decimal2.random = random;
|
|
2761
|
+
Decimal2.round = round;
|
|
2762
|
+
Decimal2.sign = sign;
|
|
2763
|
+
Decimal2.sin = sin;
|
|
2764
|
+
Decimal2.sinh = sinh;
|
|
2765
|
+
Decimal2.sqrt = sqrt;
|
|
2766
|
+
Decimal2.sub = sub;
|
|
2767
|
+
Decimal2.sum = sum;
|
|
2768
|
+
Decimal2.tan = tan;
|
|
2769
|
+
Decimal2.tanh = tanh;
|
|
2770
|
+
Decimal2.trunc = trunc;
|
|
2771
|
+
if (obj === void 0) obj = {};
|
|
2772
|
+
if (obj) {
|
|
2773
|
+
if (obj.defaults !== true) {
|
|
2774
|
+
ps = ["precision", "rounding", "toExpNeg", "toExpPos", "maxE", "minE", "modulo", "crypto"];
|
|
2775
|
+
for (i = 0; i < ps.length; ) if (!obj.hasOwnProperty(p = ps[i++])) obj[p] = this[p];
|
|
2776
|
+
}
|
|
2777
|
+
}
|
|
2778
|
+
Decimal2.config(obj);
|
|
2779
|
+
return Decimal2;
|
|
2780
|
+
}
|
|
2781
|
+
function div(x, y) {
|
|
2782
|
+
return new this(x).div(y);
|
|
2783
|
+
}
|
|
2784
|
+
function exp(x) {
|
|
2785
|
+
return new this(x).exp();
|
|
2786
|
+
}
|
|
2787
|
+
function floor(x) {
|
|
2788
|
+
return finalise(x = new this(x), x.e + 1, 3);
|
|
2789
|
+
}
|
|
2790
|
+
function hypot() {
|
|
2791
|
+
var i, n, t = new this(0);
|
|
2792
|
+
external = false;
|
|
2793
|
+
for (i = 0; i < arguments.length; ) {
|
|
2794
|
+
n = new this(arguments[i++]);
|
|
2795
|
+
if (!n.d) {
|
|
2796
|
+
if (n.s) {
|
|
2797
|
+
external = true;
|
|
2798
|
+
return new this(1 / 0);
|
|
2799
|
+
}
|
|
2800
|
+
t = n;
|
|
2801
|
+
} else if (t.d) {
|
|
2802
|
+
t = t.plus(n.times(n));
|
|
2803
|
+
}
|
|
2804
|
+
}
|
|
2805
|
+
external = true;
|
|
2806
|
+
return t.sqrt();
|
|
2807
|
+
}
|
|
2808
|
+
function isDecimalInstance(obj) {
|
|
2809
|
+
return obj instanceof Decimal || obj && obj.toStringTag === tag || false;
|
|
2810
|
+
}
|
|
2811
|
+
function ln(x) {
|
|
2812
|
+
return new this(x).ln();
|
|
2813
|
+
}
|
|
2814
|
+
function log(x, y) {
|
|
2815
|
+
return new this(x).log(y);
|
|
2816
|
+
}
|
|
2817
|
+
function log2(x) {
|
|
2818
|
+
return new this(x).log(2);
|
|
2819
|
+
}
|
|
2820
|
+
function log10(x) {
|
|
2821
|
+
return new this(x).log(10);
|
|
2822
|
+
}
|
|
2823
|
+
function max() {
|
|
2824
|
+
return maxOrMin(this, arguments, -1);
|
|
2825
|
+
}
|
|
2826
|
+
function min() {
|
|
2827
|
+
return maxOrMin(this, arguments, 1);
|
|
2828
|
+
}
|
|
2829
|
+
function mod(x, y) {
|
|
2830
|
+
return new this(x).mod(y);
|
|
2831
|
+
}
|
|
2832
|
+
function mul(x, y) {
|
|
2833
|
+
return new this(x).mul(y);
|
|
2834
|
+
}
|
|
2835
|
+
function pow(x, y) {
|
|
2836
|
+
return new this(x).pow(y);
|
|
2837
|
+
}
|
|
2838
|
+
function random(sd) {
|
|
2839
|
+
var d, e, k, n, i = 0, r = new this(1), rd = [];
|
|
2840
|
+
if (sd === void 0) sd = this.precision;
|
|
2841
|
+
else checkInt32(sd, 1, MAX_DIGITS);
|
|
2842
|
+
k = Math.ceil(sd / LOG_BASE);
|
|
2843
|
+
if (!this.crypto) {
|
|
2844
|
+
for (; i < k; ) rd[i++] = Math.random() * 1e7 | 0;
|
|
2845
|
+
} else if (crypto.getRandomValues) {
|
|
2846
|
+
d = crypto.getRandomValues(new Uint32Array(k));
|
|
2847
|
+
for (; i < k; ) {
|
|
2848
|
+
n = d[i];
|
|
2849
|
+
if (n >= 429e7) {
|
|
2850
|
+
d[i] = crypto.getRandomValues(new Uint32Array(1))[0];
|
|
2851
|
+
} else {
|
|
2852
|
+
rd[i++] = n % 1e7;
|
|
2853
|
+
}
|
|
2854
|
+
}
|
|
2855
|
+
} else if (crypto.randomBytes) {
|
|
2856
|
+
d = crypto.randomBytes(k *= 4);
|
|
2857
|
+
for (; i < k; ) {
|
|
2858
|
+
n = d[i] + (d[i + 1] << 8) + (d[i + 2] << 16) + ((d[i + 3] & 127) << 24);
|
|
2859
|
+
if (n >= 214e7) {
|
|
2860
|
+
crypto.randomBytes(4).copy(d, i);
|
|
2861
|
+
} else {
|
|
2862
|
+
rd.push(n % 1e7);
|
|
2863
|
+
i += 4;
|
|
2864
|
+
}
|
|
2865
|
+
}
|
|
2866
|
+
i = k / 4;
|
|
2867
|
+
} else {
|
|
2868
|
+
throw Error(cryptoUnavailable);
|
|
2869
|
+
}
|
|
2870
|
+
k = rd[--i];
|
|
2871
|
+
sd %= LOG_BASE;
|
|
2872
|
+
if (k && sd) {
|
|
2873
|
+
n = mathpow(10, LOG_BASE - sd);
|
|
2874
|
+
rd[i] = (k / n | 0) * n;
|
|
2875
|
+
}
|
|
2876
|
+
for (; rd[i] === 0; i--) rd.pop();
|
|
2877
|
+
if (i < 0) {
|
|
2878
|
+
e = 0;
|
|
2879
|
+
rd = [0];
|
|
2880
|
+
} else {
|
|
2881
|
+
e = -1;
|
|
2882
|
+
for (; rd[0] === 0; e -= LOG_BASE) rd.shift();
|
|
2883
|
+
for (k = 1, n = rd[0]; n >= 10; n /= 10) k++;
|
|
2884
|
+
if (k < LOG_BASE) e -= LOG_BASE - k;
|
|
2885
|
+
}
|
|
2886
|
+
r.e = e;
|
|
2887
|
+
r.d = rd;
|
|
2888
|
+
return r;
|
|
2889
|
+
}
|
|
2890
|
+
function round(x) {
|
|
2891
|
+
return finalise(x = new this(x), x.e + 1, this.rounding);
|
|
2892
|
+
}
|
|
2893
|
+
function sign(x) {
|
|
2894
|
+
x = new this(x);
|
|
2895
|
+
return x.d ? x.d[0] ? x.s : 0 * x.s : x.s || NaN;
|
|
2896
|
+
}
|
|
2897
|
+
function sin(x) {
|
|
2898
|
+
return new this(x).sin();
|
|
2899
|
+
}
|
|
2900
|
+
function sinh(x) {
|
|
2901
|
+
return new this(x).sinh();
|
|
2902
|
+
}
|
|
2903
|
+
function sqrt(x) {
|
|
2904
|
+
return new this(x).sqrt();
|
|
2905
|
+
}
|
|
2906
|
+
function sub(x, y) {
|
|
2907
|
+
return new this(x).sub(y);
|
|
2908
|
+
}
|
|
2909
|
+
function sum() {
|
|
2910
|
+
var i = 0, args = arguments, x = new this(args[i]);
|
|
2911
|
+
external = false;
|
|
2912
|
+
for (; x.s && ++i < args.length; ) x = x.plus(args[i]);
|
|
2913
|
+
external = true;
|
|
2914
|
+
return finalise(x, this.precision, this.rounding);
|
|
2915
|
+
}
|
|
2916
|
+
function tan(x) {
|
|
2917
|
+
return new this(x).tan();
|
|
2918
|
+
}
|
|
2919
|
+
function tanh(x) {
|
|
2920
|
+
return new this(x).tanh();
|
|
2921
|
+
}
|
|
2922
|
+
function trunc(x) {
|
|
2923
|
+
return finalise(x = new this(x), x.e + 1, 1);
|
|
2924
|
+
}
|
|
2925
|
+
P[Symbol.for("nodejs.util.inspect.custom")] = P.toString;
|
|
2926
|
+
P[Symbol.toStringTag] = "Decimal";
|
|
2927
|
+
var Decimal = P.constructor = clone(DEFAULTS);
|
|
2928
|
+
LN10 = new Decimal(LN10);
|
|
2929
|
+
PI = new Decimal(PI);
|
|
2930
|
+
var decimal_default = Decimal;
|
|
2931
|
+
|
|
2932
|
+
// src/utils/apy.ts
|
|
2933
|
+
var calculateApy = (firstEvent, lastEvent) => {
|
|
2934
|
+
const withdrawRateChange = Number(lastEvent.newWithdrawRate) - Number(firstEvent.newWithdrawRate);
|
|
2935
|
+
const withdrawRateInitial = Number(firstEvent.newWithdrawRate);
|
|
2936
|
+
if (withdrawRateInitial === 0) {
|
|
2937
|
+
return "0.00";
|
|
2938
|
+
}
|
|
2939
|
+
const secondsDiff = Math.abs(import_dayjs.default.unix(Number(lastEvent.timestamp)).diff(
|
|
2940
|
+
import_dayjs.default.unix(Number(firstEvent.timestamp)),
|
|
2941
|
+
"seconds"
|
|
2942
|
+
));
|
|
2943
|
+
if (secondsDiff === 0) {
|
|
2944
|
+
return "0.00";
|
|
2945
|
+
}
|
|
2946
|
+
const apy = new decimal_default(1 + withdrawRateChange / withdrawRateInitial).pow(365 * 24 * 3600 / secondsDiff).minus(1).times(100);
|
|
2947
|
+
return apy.toFixed(8);
|
|
2948
|
+
};
|
|
2949
|
+
|
|
2950
|
+
// src/utils/flag.ts
|
|
2951
|
+
var FLAG_BITS = {
|
|
2952
|
+
/** bit 0 (1): deadline passed */
|
|
2953
|
+
DEADLINE_PASSED: 1,
|
|
2954
|
+
// 1 << 0
|
|
2955
|
+
/** bit 1 (2): zero offer amount */
|
|
2956
|
+
ZERO_OFFER_AMOUNT: 2,
|
|
2957
|
+
// 1 << 1
|
|
2958
|
+
/** bit 2 (4): minimum age not met */
|
|
2959
|
+
MINIMUM_AGE_NOT_MET: 4
|
|
2960
|
+
// 1 << 2
|
|
2961
|
+
};
|
|
2962
|
+
var parseRequestFlags = (flagNumber) => {
|
|
2963
|
+
return {
|
|
2964
|
+
deadlinePassed: (flagNumber & FLAG_BITS.DEADLINE_PASSED) !== 0,
|
|
2965
|
+
zeroOfferAmount: (flagNumber & FLAG_BITS.ZERO_OFFER_AMOUNT) !== 0,
|
|
2966
|
+
minimumAgeNotMet: (flagNumber & FLAG_BITS.MINIMUM_AGE_NOT_MET) !== 0
|
|
2967
|
+
};
|
|
2968
|
+
};
|
|
2969
|
+
var buildRequestFlags = (flags) => {
|
|
2970
|
+
let result = 0;
|
|
2971
|
+
if (flags.deadlinePassed) {
|
|
2972
|
+
result |= FLAG_BITS.DEADLINE_PASSED;
|
|
2973
|
+
}
|
|
2974
|
+
if (flags.zeroOfferAmount) {
|
|
2975
|
+
result |= FLAG_BITS.ZERO_OFFER_AMOUNT;
|
|
2976
|
+
}
|
|
2977
|
+
if (flags.minimumAgeNotMet) {
|
|
2978
|
+
result |= FLAG_BITS.MINIMUM_AGE_NOT_MET;
|
|
2979
|
+
}
|
|
2980
|
+
return result;
|
|
2981
|
+
};
|
|
2982
|
+
var hasFlag = (flagNumber, flagType) => {
|
|
2983
|
+
return (flagNumber & FLAG_BITS[flagType]) !== 0;
|
|
2984
|
+
};
|
|
2985
|
+
var getActiveFlags = (flagNumber) => {
|
|
2986
|
+
const activeFlags = [];
|
|
2987
|
+
if (hasFlag(flagNumber, "DEADLINE_PASSED")) {
|
|
2988
|
+
activeFlags.push("DEADLINE_PASSED");
|
|
2989
|
+
}
|
|
2990
|
+
if (hasFlag(flagNumber, "ZERO_OFFER_AMOUNT")) {
|
|
2991
|
+
activeFlags.push("ZERO_OFFER_AMOUNT");
|
|
2992
|
+
}
|
|
2993
|
+
if (hasFlag(flagNumber, "MINIMUM_AGE_NOT_MET")) {
|
|
2994
|
+
activeFlags.push("MINIMUM_AGE_NOT_MET");
|
|
2995
|
+
}
|
|
2996
|
+
return activeFlags;
|
|
2997
|
+
};
|
|
2998
|
+
|
|
2999
|
+
// src/utils/mapper.ts
|
|
3000
|
+
function mapOnchainToWithdrawalRequest(onchainRequest, updatedAt) {
|
|
3001
|
+
return {
|
|
3002
|
+
atomicPrice: onchainRequest.atomic_price,
|
|
3003
|
+
deadline: onchainRequest.deadline,
|
|
3004
|
+
flags: parseRequestFlags(onchainRequest.flags?.flag ?? 0),
|
|
3005
|
+
offerAmount: onchainRequest.offer_amount,
|
|
3006
|
+
offerToken: onchainRequest.offer_token,
|
|
3007
|
+
userAddress: onchainRequest.user_address,
|
|
3008
|
+
wantAmount: onchainRequest.want_amount,
|
|
3009
|
+
wantToken: onchainRequest.want_token,
|
|
3010
|
+
updatedAt: updatedAt ?? ""
|
|
3011
|
+
};
|
|
3012
|
+
}
|
|
3013
|
+
function mapIndexerToVaultActivity(indexerActivity) {
|
|
3014
|
+
return {
|
|
3015
|
+
depositAmount: indexerActivity.deposit_amount,
|
|
3016
|
+
depositToken: indexerActivity.deposit_token,
|
|
3017
|
+
eventType: indexerActivity.event_type,
|
|
3018
|
+
txHash: indexerActivity.tx_hash,
|
|
3019
|
+
txVersion: indexerActivity.tx_version,
|
|
3020
|
+
txTimestamp: indexerActivity.tx_timestamp,
|
|
3021
|
+
user: indexerActivity.user,
|
|
3022
|
+
vault: indexerActivity.vault,
|
|
3023
|
+
withdrawAmount: indexerActivity.withdraw_amount,
|
|
3024
|
+
withdrawToken: indexerActivity.withdraw_token
|
|
3025
|
+
};
|
|
3026
|
+
}
|
|
3027
|
+
function mapIndexerToExchangeRateEvent(indexerEvent) {
|
|
3028
|
+
return {
|
|
3029
|
+
timestamp: indexerEvent.current_time,
|
|
3030
|
+
newDepositRate: indexerEvent.new_deposit_rate,
|
|
3031
|
+
newWithdrawRate: indexerEvent.new_withdraw_rate,
|
|
3032
|
+
oldDepositRate: indexerEvent.old_deposit_rate,
|
|
3033
|
+
oldWithdrawRate: indexerEvent.old_withdraw_rate,
|
|
3034
|
+
vault: indexerEvent.vault,
|
|
3035
|
+
type: indexerEvent.type
|
|
3036
|
+
};
|
|
3037
|
+
}
|
|
3038
|
+
|
|
3039
|
+
// src/utils/sampling.ts
|
|
3040
|
+
var import_dayjs2 = __toESM(require_dayjs_min());
|
|
3041
|
+
var import_utc = __toESM(require_utc());
|
|
3042
|
+
import_dayjs2.default.extend(import_utc.default);
|
|
3043
|
+
var SamplingStrategy = /* @__PURE__ */ ((SamplingStrategy2) => {
|
|
3044
|
+
SamplingStrategy2["START"] = "start";
|
|
3045
|
+
SamplingStrategy2["END"] = "end";
|
|
3046
|
+
SamplingStrategy2["MEDIAN"] = "median";
|
|
3047
|
+
SamplingStrategy2["AVERAGE"] = "average";
|
|
3048
|
+
return SamplingStrategy2;
|
|
3049
|
+
})(SamplingStrategy || {});
|
|
3050
|
+
var applySamplingStrategy = (dayEvents, strategy) => {
|
|
3051
|
+
switch (strategy) {
|
|
3052
|
+
case "start" /* START */: {
|
|
3053
|
+
return dayEvents[0];
|
|
3054
|
+
}
|
|
3055
|
+
case "end" /* END */: {
|
|
3056
|
+
return dayEvents[dayEvents.length - 1];
|
|
3057
|
+
}
|
|
3058
|
+
case "median" /* MEDIAN */: {
|
|
3059
|
+
const medianIndex = Math.floor(dayEvents.length / 2);
|
|
3060
|
+
return dayEvents[medianIndex];
|
|
3061
|
+
}
|
|
3062
|
+
case "average" /* AVERAGE */: {
|
|
3063
|
+
const sumRate = dayEvents.reduce(
|
|
3064
|
+
(sum2, event) => sum2 + Number(event.newWithdrawRate),
|
|
3065
|
+
0
|
|
3066
|
+
);
|
|
3067
|
+
const avgRate = sumRate / dayEvents.length;
|
|
3068
|
+
const middleEvent = dayEvents[Math.floor(dayEvents.length / 2)];
|
|
3069
|
+
return {
|
|
3070
|
+
...middleEvent,
|
|
3071
|
+
newWithdrawRate: avgRate.toString()
|
|
3072
|
+
};
|
|
3073
|
+
}
|
|
3074
|
+
default: {
|
|
3075
|
+
return dayEvents[dayEvents.length - 1];
|
|
3076
|
+
}
|
|
3077
|
+
}
|
|
3078
|
+
};
|
|
3079
|
+
var sampleEventsByDay = (events, strategy = "start" /* START */) => {
|
|
3080
|
+
if (events.length === 0) {
|
|
3081
|
+
return [];
|
|
3082
|
+
}
|
|
3083
|
+
const eventsByDay = /* @__PURE__ */ new Map();
|
|
3084
|
+
events.forEach((event) => {
|
|
3085
|
+
const dayKey = import_dayjs2.default.unix(Number(event.timestamp)).utc().format("YYYY-MM-DD");
|
|
3086
|
+
const dayEvents = eventsByDay.get(dayKey) || [];
|
|
3087
|
+
dayEvents.push(event);
|
|
3088
|
+
eventsByDay.set(dayKey, dayEvents);
|
|
3089
|
+
});
|
|
3090
|
+
const sampledEvents = [];
|
|
3091
|
+
eventsByDay.forEach((dayEvents) => {
|
|
3092
|
+
dayEvents.sort((a, b) => Number(a.timestamp) - Number(b.timestamp));
|
|
3093
|
+
const selectedEvent = applySamplingStrategy(dayEvents, strategy);
|
|
3094
|
+
sampledEvents.push(selectedEvent);
|
|
3095
|
+
});
|
|
3096
|
+
sampledEvents.sort((a, b) => Number(a.timestamp) - Number(b.timestamp));
|
|
3097
|
+
return sampledEvents;
|
|
3098
|
+
};
|
|
3099
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
3100
|
+
0 && (module.exports = {
|
|
3101
|
+
BaseChainAdapter,
|
|
3102
|
+
CHAIN_CONFIGS,
|
|
3103
|
+
ChainId,
|
|
3104
|
+
ChainType,
|
|
3105
|
+
ConfigError,
|
|
3106
|
+
ContractError,
|
|
3107
|
+
ErrorCodes,
|
|
3108
|
+
InsufficientFundsError,
|
|
3109
|
+
NetworkError,
|
|
3110
|
+
NetworkType,
|
|
3111
|
+
PyxisError,
|
|
3112
|
+
PyxisUtils,
|
|
3113
|
+
RateLimitError,
|
|
3114
|
+
StrategyError,
|
|
3115
|
+
TransactionError,
|
|
3116
|
+
ValidationError,
|
|
3117
|
+
VaultError,
|
|
3118
|
+
WalletError,
|
|
3119
|
+
createError,
|
|
3120
|
+
getChainConfig
|
|
3121
|
+
});
|
|
3122
|
+
/*! Bundled license information:
|
|
3123
|
+
|
|
3124
|
+
decimal.js/decimal.mjs:
|
|
3125
|
+
(*!
|
|
3126
|
+
* decimal.js v10.6.0
|
|
3127
|
+
* An arbitrary-precision Decimal type for JavaScript.
|
|
3128
|
+
* https://github.com/MikeMcl/decimal.js
|
|
3129
|
+
* Copyright (c) 2025 Michael Mclaughlin <M8ch88l@gmail.com>
|
|
3130
|
+
* MIT Licence
|
|
3131
|
+
*)
|
|
3132
|
+
*/
|
|
3133
|
+
//# sourceMappingURL=index.js.map
|