powiaina_num.js 0.1.2

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.
@@ -0,0 +1,1974 @@
1
+ //Code snippets and templates from ExpantaNum.js
2
+
3
+
4
+ ; export default (function (globalScope) {
5
+
6
+ function deepCopyProps(source, target) {
7
+ for (let key in source) {
8
+ if (source.hasOwnProperty(key)) {
9
+ // 如果源对象的属性是对象或数组,则递归复制
10
+ if ((typeof source[key] === 'object' && !(source[key] instanceof PowiainaNum)) && source[key] !== null) {
11
+ // 如果目标对象没有这个属性,或者属性是null,则创建一个新的
12
+ if (!target.hasOwnProperty(key) || target[key] == null || Array.isArray(source[key]) !== Array.isArray(target[key])) {
13
+ target[key] = Array.isArray(source[key]) ? [] : {};
14
+ }
15
+ // 递归复制属性
16
+ deepCopyProps(source[key], target[key]);
17
+ } else {
18
+ // 如果属性不是对象或数组,则直接复制
19
+ target[key] = source[key];
20
+ }
21
+ }
22
+ }
23
+ }
24
+
25
+ var PowiainaNum = {
26
+ maxOps: 10,
27
+
28
+ // Specify what format is used when serializing for JSON.stringify
29
+ //
30
+ // JSON 0 JSON object
31
+ // STRING 1 String
32
+ serializeMode: 1,
33
+
34
+ // Deprecated
35
+ // Level of debug information printed in console
36
+ //
37
+ // NONE 0 Show no information.
38
+ // NORMAL 1 Show operations.
39
+ // ALL 2 Show everything.
40
+ debug: 0,
41
+
42
+
43
+ },
44
+ external = true,
45
+
46
+ powiainaNumError = "[PowiainaNumError] ",
47
+ invalidArgument = powiainaNumError + "Invalid argument: ",
48
+
49
+
50
+ MAX_SAFE_INTEGER = 9007199254740991,
51
+ MAX_E = Math.log10(MAX_SAFE_INTEGER), //15.954589770191003
52
+
53
+ //PowiainaNum string cache object
54
+ C = {
55
+ "0" : {array: [0], layer: 0, sign: 0},
56
+ "1" : {array: [1], layer: 0, sign: 0},
57
+ "2" : {array: [2], layer: 0, sign: 0},
58
+ "3" : {array: [3], layer: 0, sign: 0},
59
+ "4" : {array: [4], layer: 0, sign: 0},
60
+ /*get "5"(){
61
+ return {array: [5], layer: 0, sign: 0}
62
+ },
63
+ set "5"(x){
64
+ },*/
65
+ "6" : {array: [6], layer: 0, sign: 0},
66
+ "7" : {array: [7], layer: 0, sign: 0},
67
+ "8" : {array: [8], layer: 0, sign: 0},
68
+ "9" : {array: [9], layer: 0, sign: 0},
69
+ "10" : {array: [10], layer: 0, sign: 0},
70
+ },
71
+ //PowiainaNum.prototype object
72
+ P = {},
73
+
74
+ //PowiainaNum static object
75
+ Q = {},
76
+
77
+ R = {};
78
+
79
+ //#region PowiainaNum constants
80
+ R.ZERO = 0;
81
+ R.ONE = 1;
82
+ R.NaN = NaN;
83
+
84
+ R.E = Math.E;
85
+ R.LN2 = Math.LN2;
86
+ R.LN10 = Math.LN10;
87
+ R.LOG2E = Math.LOG2E;
88
+ R.LOG10E = Math.LOG10E;
89
+ R.PI = Math.PI;
90
+ R.SQRT1_2 = Math.SQRT1_2;
91
+ R.SQRT2 = Math.SQRT2;
92
+ R.MAX_SAFE_INTEGER = MAX_SAFE_INTEGER;
93
+ R.MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER;
94
+ R.NEGATIVE_INFINITY = Number.NEGATIVE_INFINITY;
95
+ R.POSITIVE_INFINITY = Number.POSITIVE_INFINITY;
96
+ R.E_MAX_SAFE_INTEGER = "l0 s1 a[" + MAX_SAFE_INTEGER + ",[1,1,1,1]]";
97
+ R.EE_MAX_SAFE_INTEGER = "l0 s1 a[" + MAX_SAFE_INTEGER + ",[1,2,1,1]]";
98
+ R.MAX_POWIAINANUM_VALUE = "l" + MAX_SAFE_INTEGER + " s1 a[" + MAX_SAFE_INTEGER + ",[" + MAX_SAFE_INTEGER + "," + MAX_SAFE_INTEGER + "," + MAX_SAFE_INTEGER + "," + MAX_SAFE_INTEGER + "]]";
99
+ R.TETRATED_MAX_SAFE_INTEGER = "l0 s1 a[10000000000,[1," + (MAX_SAFE_INTEGER - 1).toString() + ",1,1]]";
100
+ R.PENTATED_MAX_SAFE_INTEGER = "l0 s1 a[10000000000,[2," + (MAX_SAFE_INTEGER - 1).toString() + ",1,1]]";
101
+ R.MULTIEXPANSIONED_MAX_SAFE_INTEGER = "l0 s1 a[10000000000,[1," + (MAX_SAFE_INTEGER - 1).toString() + ",2,1]]";
102
+ R.POWEREXPANSIONED_MAX_SAFE_INTEGER = "l0 s1 a[10000000000,[2," + (MAX_SAFE_INTEGER - 1).toString() + ",2,1]]";
103
+ R.GRAHAMS_NUMBER = "l0 s1 a[3638334640023.7783,[1,7625597484984,1,1],[3,1,1,1],[\"x\",63,1,1]]"
104
+
105
+ // sample, Throotriadekol == {100, 100, 100, 99, 12}
106
+ // At there 100 => 10, {10, 10, 10, 99, 12}
107
+ R.THROOTRIADEKOL = "l0 s1 a [10,[10,1,99,12]]"
108
+ //#endregion
109
+
110
+ /*
111
+ isPowiainaNum is a regex to judge string is a valid PowiainaNum string source.
112
+ */
113
+ var isPowiainaNum = /^[-\+]*(Infinity|NaN|(J+|J\^\d+(((\.\d*)?([Ee][-\+]*))\d*)? )?(10(\^+|\{[1-9]\d*\})|\(10(\^+|\{[1-9]\d*\})\)\^[1-9]\d* )*((\d+(\.\d*)?|\d*\.\d+)?([Ee][-\+]*))*(0|\d+(\.\d*)?|\d*\.\d+))$/;
114
+ var isPowiainaNum2 = /^l(\d+) s(1|0|\-1) a\[\d+((.\d*)?e\d+|\.\d*)?\,(\[(\d+((.\d*)?e\d+)?|\"x\")\,\d+((.\d*)?e\d+)?\,(\d+((.\d*)?e\d+)?|\"x\")\,\d+((.\d*)?e\d+)?\])*\]$/;
115
+ //#region calculating
116
+
117
+
118
+ //#region four basic calculating
119
+ //#region plus
120
+ P.plus = P.add = function (other) {
121
+ var x = this.clone();
122
+ other = new PowiainaNum(other);
123
+ if (PowiainaNum.debug >= PowiainaNum.NORMAL) {
124
+ console.log(this, "calculate add", other);
125
+ }
126
+ if (x.sign == -1) return x.neg().add(other.neg()).neg();
127
+ if (other.sign == -1) return x.sub(other.neg());
128
+
129
+ if (x.eq(PowiainaNum.ZERO)) return other; // 0+x
130
+ if (other.eq(PowiainaNum.ZERO)) return x;
131
+ if (x.isNaN() || other.isNaN() || x.isInfinite() && other.isInfinite() && x.eq(other.neg())) return PowiainaNum.NaN.clone();
132
+ if (x.isInfinite()) return x;
133
+ if (other.isInfinite()) return other;
134
+
135
+ var p = x.min(other); // who is smallest
136
+ var q = x.max(other); // who is biggest
137
+ var op0 = q.operatorE(0);
138
+ var op1 = q.operatorE(1);
139
+
140
+ var t;
141
+
142
+ if (q.gt(PowiainaNum.E_MAX_SAFE_INTEGER) || q.div(p).gt(PowiainaNum.MAX_SAFE_INTEGER)) {
143
+ t = q;
144
+ } else if (!op1) {
145
+ t = new PowiainaNum(x.toNumber() + other.toNumber());
146
+ } else if (op1 == 1) {
147
+ var a = p.operator(1) ? p.operator(0) : Math.log10(p.operator(0));
148
+ t = new PowiainaNum(a + Math.log10(Math.pow(10, op0 - a) + 1));
149
+ t = PowiainaNum.pow(10, t);
150
+ }
151
+ p = q = null;
152
+ return t;
153
+ }
154
+
155
+ Q.plus = Q.add = function (x, y) {
156
+ return new PowiainaNum(x).add(y);
157
+ };
158
+ //#endregion
159
+
160
+ //#region minus
161
+ P.minus = P.sub = function (other) {
162
+ var x = this.clone();
163
+ other = new PowiainaNum(other);
164
+ if (PowiainaNum.debug >= PowiainaNum.NORMAL) console.log(x + "-" + other);
165
+ if (x.sign == -1) return x.neg().sub(other.neg()).neg();
166
+ if (other.sign == -1) return x.add(other.neg());
167
+ if (x.eq(other)) return PowiainaNum.ZERO.clone();
168
+ if (other.eq(PowiainaNum.ZERO)) return x;
169
+ if (x.isNaN() || other.isNaN() || x.isInfinite() && other.isInfinite()) return PowiainaNum.NaN.clone();
170
+ if (x.isInfinite()) return x;
171
+ if (other.isInfinite()) return other.neg();
172
+ var p = x.min(other);
173
+ var q = x.max(other);
174
+ var n = other.gt(x);
175
+ var op0 = q.operator(0);
176
+ var op1 = q.operator(1);
177
+ var t;
178
+ if (q.gt(PowiainaNum.E_MAX_SAFE_INTEGER) || q.div(p).gt(PowiainaNum.MAX_SAFE_INTEGER)) {
179
+ t = q;
180
+ t = n ? t.neg() : t;
181
+ } else if (!op1) {
182
+ t = new PowiainaNum(x.toNumber() - other.toNumber());
183
+ } else if (op1 == 1) {
184
+ var a = p.operator(1) ? p.operator(0) : Math.log10(p.operator(0));
185
+ t = new PowiainaNum(a + Math.log10(Math.pow(10, op0 - a) - 1));
186
+ t = PowiainaNum.pow(10, t)
187
+ t = n ? t.neg() : t;
188
+ }
189
+ p = q = null;
190
+ return t;
191
+ };
192
+ Q.minus = Q.sub = function (x, y) {
193
+ return new PowiainaNum(x).sub(y);
194
+ };
195
+ //#endregion
196
+
197
+ //#region times
198
+ P.times = P.mul = function (other) {
199
+ var x = this.clone();
200
+ other = new PowiainaNum(other);
201
+ if (PowiainaNum.debug >= PowiainaNum.NORMAL) console.log(x + "*" + other);
202
+ if (x.sign * other.sign == -1) return x.abs().mul(other.abs()).neg();
203
+ if (x.sign == -1) return x.abs().mul(other.abs());
204
+ if (x.isNaN() || other.isNaN() || x.eq(PowiainaNum.ZERO) && other.isInfinite() || x.isInfinite() && other.abs().eq(PowiainaNum.ZERO)) return PowiainaNum.NaN.clone();
205
+ if (other.eq(PowiainaNum.ZERO)) return PowiainaNum.ZERO.clone();
206
+ if (other.eq(PowiainaNum.ONE)) return x.clone();
207
+ if (x.isInfinite()) return x;
208
+ if (other.isInfinite()) return other;
209
+ if (x.max(other).gt(PowiainaNum.EE_MAX_SAFE_INTEGER)) return x.max(other);
210
+ var n = x.toNumber() * other.toNumber();
211
+ if (n <= MAX_SAFE_INTEGER) return new PowiainaNum(n);
212
+ return PowiainaNum.pow(10, x.log10().add(other.log10()));
213
+ };
214
+ Q.times = Q.mul = function (x, y) {
215
+ return new PowiainaNum(x).mul(y);
216
+ };
217
+
218
+ //#endregion
219
+
220
+ //#region division
221
+
222
+ P.divide = P.div = function (other) {
223
+ var x = this.clone();
224
+ other = new PowiainaNum(other);
225
+ if (PowiainaNum.debug >= PowiainaNum.NORMAL) console.log(x + "/" + "other");
226
+ if (x.sign * other.sign == -1) return x.abs().div(other.abs()).neg();
227
+ if (x.sign == -1) return x.abs().div(other.abs());
228
+ if (x.isNaN() || other.isNaN() || x.isInfinite() && other.isInfinite() || x.eq(PowiainaNum.ZERO) && other.eq(PowiainaNum.ZERO)) return PowiainaNum.NaN.clone();
229
+ if (other.eq(PowiainaNum.ZERO)) return PowiainaNum.POSITIVE_INFINITY.clone();
230
+ if (other.eq(PowiainaNum.ZERO)) return PowiainaNum.ONE.clone();
231
+ if (x.isInfinite()) return x;
232
+ if (other.isInfinite()) return PowiainaNum.ZERO.clone();
233
+ if (x.max(other).gt(PowiainaNum.EE_MAX_SAFE_INTEGER)) return x.gt(other) ? x.clone() : PowiainaNum.ZERO.clone();
234
+ var n = x.toNumber() / other.toNumber();
235
+ if (n <= MAX_SAFE_INTEGER) return new PowiainaNum(n);
236
+
237
+ var pw = PowiainaNum.pow(10, x.log10().sub(other.log10()));
238
+
239
+ var fp = pw.floor();
240
+ if (pw.sub(fp).lt(new PowiainaNum(1e-9))) return fp;
241
+ return pw;
242
+ }
243
+ Q.divide = Q.div = function (x, y) {
244
+ return new PowiainaNum(x).div(y);
245
+ };
246
+ P.reciprocate = P.rec = function () {
247
+ if (PowiainaNum.debug >= PowiainaNum.NORMAL) console.log(this + "^-1");
248
+ if (this.isNaN() || this.eq(PowiainaNum.ZERO)) return PowiainaNum.NaN.clone();
249
+ if (this.abs().gt("2e323")) return PowiainaNum.ZERO.clone();
250
+ return new PowiainaNum(1 / this);
251
+ };
252
+ Q.reciprocate = Q.rec = function (x) {
253
+ return new PowiainaNum(x).rec();
254
+ };
255
+
256
+
257
+ //#endregion
258
+ //#endregion
259
+
260
+ //#region power, root, logarithm, iteratedlog, iteratedexp, iterated slog
261
+ P.toPower = P.pow = function (other) {
262
+ other = new PowiainaNum(other);
263
+ if (PowiainaNum.debug >= PowiainaNum.NORMAL) console.log(this + "^" + other);
264
+ if (other.eq(PowiainaNum.ZERO)) return PowiainaNum.ONE.clone();
265
+ if (other.eq(PowiainaNum.ONE)) return this.clone();
266
+ if (other.lt(PowiainaNum.ZERO)) return this.pow(other.neg()).rec();
267
+ if (this.lt(PowiainaNum.ZERO) && other.isint()) {
268
+ if (other.mod(2).lt(PowiainaNum.ONE)) return this.abs().pow(other);
269
+ return this.abs().pow(other).neg();
270
+ }
271
+ if (this.lt(PowiainaNum.ZERO)) return PowiainaNum.NaN.clone();
272
+ if (this.eq(PowiainaNum.ONE)) return PowiainaNum.ONE.clone();
273
+ if (this.eq(PowiainaNum.ZERO)) return PowiainaNum.ZERO.clone();
274
+ if (this.max(other).gt(PowiainaNum.TETRATED_MAX_SAFE_INTEGER)) return this.max(other);
275
+ if (this.eq(10)) {
276
+ if (other.gt(PowiainaNum.ZERO)) {
277
+ other.operatorE(1, (other.operatorE(1) + 1) || 1);
278
+ other.normalize();
279
+ return other;
280
+ } else {
281
+ return new PowiainaNum(Math.pow(10, other.toNumber()));
282
+ }
283
+ }
284
+ if (other.lt(PowiainaNum.ONE)) return this.root(other.rec());
285
+ var n = Math.pow(this.toNumber(), other.toNumber());
286
+ if (n <= MAX_SAFE_INTEGER) return new PowiainaNum(n);
287
+ return PowiainaNum.pow(10, this.log10().mul(other));
288
+ }
289
+ Q.toPower = Q.pow = function (x, y) {
290
+ return new PowiainaNum(x).pow(y);
291
+ };
292
+ P.exponential = P.exp = function () {
293
+ return PowiainaNum.pow(Math.E, this);
294
+ };
295
+ Q.exponential = Q.exp = function (x) {
296
+ return PowiainaNum.pow(Math.E, x);
297
+ };
298
+ P.squareRoot = P.sqrt = function () {
299
+ return this.root(2);
300
+ };
301
+ Q.squareRoot = Q.sqrt = function (x) {
302
+ return new PowiainaNum(x).root(2);
303
+ };
304
+ P.cubeRoot = P.cbrt = function () {
305
+ return this.root(3);
306
+ };
307
+ Q.cubeRoot = Q.cbrt = function (x) {
308
+ return new PowiainaNum(x).root(3);
309
+ };
310
+ P.root = function (other) {
311
+ other = new PowiainaNum(other);
312
+ if (PowiainaNum.debug >= PowiainaNum.NORMAL) console.log(this + "root" + other);
313
+ if (other.eq(PowiainaNum.ONE)) return this.clone();
314
+ if (other.lt(PowiainaNum.ZERO)) return this.root(other.neg()).rec();
315
+ if (other.lt(PowiainaNum.ONE)) return this.pow(other.rec());
316
+ if (this.lt(PowiainaNum.ZERO) && other.isint() && other.mod(2).eq(PowiainaNum.ONE)) return this.neg().root(other).neg();
317
+ if (this.lt(PowiainaNum.ZERO)) return PowiainaNum.NaN.clone();
318
+ if (this.eq(PowiainaNum.ONE)) return PowiainaNum.ONE.clone();
319
+ if (this.eq(PowiainaNum.ZERO)) return PowiainaNum.ZERO.clone();
320
+ if (this.max(other).gt(PowiainaNum.TETRATED_MAX_SAFE_INTEGER)) return this.gt(other) ? this.clone() : PowiainaNum.ZERO.clone();
321
+ return PowiainaNum.pow(10, this.log10().div(other));
322
+ };
323
+ Q.root = function (x, y) {
324
+ return new PowiainaNum(x).root(y);
325
+ };
326
+ P.generalLogarithm = P.log10 = function () {
327
+ var x = this.clone();
328
+ if (PowiainaNum.debug >= PowiainaNum.NORMAL) console.log("log" + this);
329
+ if (x.lt(PowiainaNum.ZERO)) return PowiainaNum.NaN.clone();
330
+ if (x.eq(PowiainaNum.ZERO)) return PowiainaNum.NEGATIVE_INFINITY.clone();
331
+ if (x.lte(PowiainaNum.MAX_SAFE_INTEGER)) return new PowiainaNum(Math.log10(x.toNumber()));
332
+ if (!x.isFinite()) return x;
333
+ if (x.gt(PowiainaNum.TETRATED_MAX_SAFE_INTEGER)) return x;
334
+ x.operatorE(1, x.operatorE(1) - 1);
335
+ return x.normalize();
336
+ };
337
+ Q.generalLogarithm = Q.log10 = function (x) {
338
+ return new PowiainaNum(x).log10();
339
+ };
340
+ P.logarithm = P.logBase = function (base) {
341
+ if (base === undefined) base = Math.E;
342
+ return this.log10().div(PowiainaNum.log10(base));
343
+ };
344
+ Q.logarithm = Q.logBase = function (x, base) {
345
+ return new PowiainaNum(x).logBase(base);
346
+ };
347
+ P.naturalLogarithm = P.log = P.ln = function () {
348
+ return this.logBase(Math.E);
349
+ };
350
+ Q.naturalLogarithm = Q.log = Q.ln = function (x) {
351
+ return new PowiainaNum(x).ln();
352
+ };
353
+ //Implementation of functions from break_eternity.js
354
+ P.iteratedexp = function (other, payload) {
355
+ return this.tetr(other, payload);
356
+ };
357
+ Q.iteratedexp = function (x, other, payload) {
358
+ return new PowiainaNum(x).iteratedexp(other, payload);
359
+ };
360
+ //This implementation is highly inaccurate and slow, and probably be given custom code
361
+ P.iteratedlog = function (base, other) {
362
+ if (base === undefined) base = 10;
363
+ if (other === undefined) other = PowiainaNum.ONE.clone();
364
+ var t = this.clone();
365
+ base = new PowiainaNum(base);
366
+ other = new PowiainaNum(other);
367
+ if (other.eq(PowiainaNum.ZERO)) return t;
368
+ if (other.eq(PowiainaNum.ONE)) return t.logBase(base);
369
+ return base.tetr(t.slog(base).sub(other));
370
+ };
371
+ Q.iteratedlog = function (x, y, z) {
372
+ return new PowiainaNum(x).iteratedlog(y, z);
373
+ };
374
+ P.iteratedslog = function (other) {
375
+ if (other === undefined) other = PowiainaNum.ONE.clone();
376
+ var t = this.clone();
377
+ other = new PowiainaNum(other);
378
+ if (other.eq(PowiainaNum.ZERO)) return t;
379
+ if (other.eq(PowiainaNum.ONE)) return t.slog();
380
+ return E(10).pent(t.mlog().sub(other))
381
+ };
382
+ //#endregion
383
+
384
+ //#region lambertw
385
+
386
+ //All of these are from Patashu's break_eternity.js
387
+ var OMEGA = 0.56714329040978387299997; //W(1,0)
388
+ //from https://math.stackexchange.com/a/465183
389
+ //The evaluation can become inaccurate very close to the branch point
390
+ var f_lambertw = function (z, tol, principal) {
391
+ if (tol === undefined) tol = 1e-10;
392
+ if (principal === undefined) principal = true;
393
+ var w;
394
+ if (!Number.isFinite(z)) return z;
395
+ if (principal) {
396
+ if (z === 0) return z;
397
+ if (z === 1) return OMEGA;
398
+ if (z < 10) w = 0;
399
+ else w = Math.log(z) - Math.log(Math.log(z));
400
+ } else {
401
+ if (z === 0) return -Infinity;
402
+ if (z <= -0.1) w = -2;
403
+ else w = Math.log(-z) - Math.log(-Math.log(-z));
404
+ }
405
+ for (var i = 0; i < 100; ++i) {
406
+ var wn = (z * Math.exp(-w) + w * w) / (w + 1);
407
+ if (Math.abs(wn - w) < tol * Math.abs(wn)) return wn;
408
+ w = wn;
409
+ }
410
+ throw Error("Iteration failed to converge: " + z);
411
+ };
412
+ //from https://github.com/scipy/scipy/blob/8dba340293fe20e62e173bdf2c10ae208286692f/scipy/special/lambertw.pxd
413
+ //The evaluation can become inaccurate very close to the branch point
414
+ //at ``-1/e``. In some corner cases, `lambertw` might currently
415
+ //fail to converge, or can end up on the wrong branch.
416
+ var d_lambertw = function (z, tol, principal) {
417
+ if (tol === undefined) tol = 1e-10;
418
+ if (principal === undefined) principal = true;
419
+ z = new PowiainaNum(z);
420
+ var w;
421
+ if (!z.isFinite()) return z;
422
+ if (principal) {
423
+ if (z.eq(PowiainaNum.ZERO)) return z;
424
+ if (z.eq(PowiainaNum.ONE)) return new PowiainaNum(OMEGA);
425
+ w = PowiainaNum.ln(z);
426
+ } else {
427
+ if (z.eq(PowiainaNum.ZERO)) return PowiainaNum.NEGATIVE_INFINITY.clone();
428
+ w = PowiainaNum.ln(z.neg());
429
+ }
430
+ for (var i = 0; i < 100; ++i) {
431
+ var ew = w.neg().exp();
432
+ var wewz = w.sub(z.mul(ew));
433
+ var dd = w.add(PowiainaNum.ONE).sub(w.add(2).mul(wewz).div(PowiainaNum.mul(2, w).add(2)));
434
+ if (dd.eq(PowiainaNum.ZERO)) return w;
435
+ var wn = w.sub(wewz.div(dd));
436
+ if (PowiainaNum.abs(wn.sub(w)).lt(PowiainaNum.abs(wn).mul(tol))) return wn;
437
+ w = wn;
438
+ }
439
+ throw Error("Iteration failed to converge: " + z);
440
+ };
441
+ //The Lambert W function, also called the omega function or product logarithm, is the solution W(x) === x*e^x.
442
+ //https://en.wikipedia.org/wiki/Lambert_W_function
443
+ //Some special values, for testing: https://en.wikipedia.org/wiki/Lambert_W_function#Special_values
444
+ P.lambertw = function (principal) {
445
+ if (principal === undefined) principal = true;
446
+ var x = this.clone();
447
+ if (x.isNaN()) return x;
448
+ if (x.lt(-0.3678794411710499)) return PowiainaNum.NaN.clone();
449
+ if (principal) {
450
+ if (x.gt(PowiainaNum.TETRATED_MAX_SAFE_INTEGER)) return x;
451
+ if (x.gt(PowiainaNum.EE_MAX_SAFE_INTEGER)) {
452
+ x.operator(1, x.operator(1) - 1);
453
+ return x;
454
+ }
455
+ if (x.gt(PowiainaNum.MAX_SAFE_INTEGER)) return d_lambertw(x);
456
+ else return new PowiainaNum(f_lambertw(x.sign * x.operator(0)));
457
+ } else {
458
+ if (x.ispos()) return PowiainaNum.NaN.clone();
459
+ if (x.abs().gt(PowiainaNum.EE_MAX_SAFE_INTEGER)) return x.neg().recip().lambertw().neg();
460
+ if (x.abs().gt(PowiainaNum.MAX_SAFE_INTEGER)) return d_lambertw(x, 1e-10, false);
461
+ else return new PowiainaNum(f_lambertw(x.sign * x.operator(0), 1e-10, false));
462
+ }
463
+ };
464
+ Q.lambertw = function (x, principal) {
465
+ return new PowiainaNum(x).lambertw(principal);
466
+ };
467
+ //end break_eternity.js excerpt
468
+ //#endregion
469
+
470
+ //#region tetrate, sroot, slog, pentate, expansion
471
+
472
+ P.tetrate = P.tetr = function (other, payload) {
473
+ if (payload === undefined) payload = PowiainaNum.ONE;
474
+ var t = this.clone();
475
+ other = new PowiainaNum(other);
476
+ payload = new PowiainaNum(payload);
477
+ if (payload.neq(PowiainaNum.ONE)) other = other.add(payload.slog(t));
478
+ if (PowiainaNum.debug >= PowiainaNum.NORMAL) console.log(t + "^^" + other);
479
+ var negln;
480
+ if (t.isNaN() || other.isNaN() || payload.isNaN()) return PowiainaNum.NaN.clone();
481
+ if (other.isInfinite() && other.sign > 0) {
482
+ if (t.gte(1.4446678610091994)) return PowiainaNum.POSITIVE_INFINITY.clone();
483
+ //Formula for infinite height power tower.
484
+ if (t.eq(1.444667861009196)) return PowiainaNum(2.718277846521643)
485
+ negln = t.ln().neg();
486
+ return negln.lambertw().div(negln);
487
+ }
488
+ if (other.lt(-2)) return PowiainaNum.NaN.clone();
489
+ if (t.eq(PowiainaNum.ZERO)) {
490
+ if (other.eq(PowiainaNum.ZERO)) return PowiainaNum.NaN.clone();
491
+ if (other.mod(2).eq(PowiainaNum.ZERO)) return PowiainaNum.ZERO.clone();
492
+ return PowiainaNum.ONE.clone();
493
+ }
494
+ if (t.eq(PowiainaNum.ONE)) {
495
+ if (other.eq(PowiainaNum.ONE.neg())) return PowiainaNum.NaN.clone();
496
+ return PowiainaNum.ONE.clone();
497
+ }
498
+ if (other.eq(PowiainaNum.ONE.neg())) return PowiainaNum.ZERO.clone();
499
+ if (other.eq(PowiainaNum.ZERO)) return PowiainaNum.ONE.clone();
500
+ if (other.eq(PowiainaNum.ONE)) return t.clone();
501
+ if (other.eq(2)) return t.pow(t);
502
+ if (t.eq(2)) {
503
+ if (other.eq(3)) return new PowiainaNum(16);
504
+ if (other.eq(4)) return new PowiainaNum(65536);
505
+ }
506
+ var m = t.max(other);
507
+ if (m.gt(PowiainaNum.PENTATED_MAX_SAFE_INTEGER)) return m;
508
+ if (m.gt(PowiainaNum.TETRATED_MAX_SAFE_INTEGER) || other.gt(PowiainaNum.MAX_SAFE_INTEGER)) {
509
+ if (this.lt(Math.exp(1 / Math.E))) {
510
+ negln = t.ln().neg();
511
+ return negln.lambertw().div(negln);
512
+ }
513
+ var j = t.slog(10).add(other);
514
+ j.operatorE(2, (j.operatorE(2) || 0) + 1);
515
+ j.normalize();
516
+ return j;
517
+ }
518
+ var y = other.toNumber();
519
+ var f = Math.floor(y);
520
+ var r = t.pow(y - f);
521
+ var l = PowiainaNum.NaN;
522
+ for (var i = 0, w = new PowiainaNum(PowiainaNum.E_MAX_SAFE_INTEGER); f !== 0 && r.lt(w) && i < 100; ++i) {
523
+ if (f > 0) {
524
+ r = t.pow(r);
525
+ if (l.eq(r)) {
526
+ f = 0;
527
+ break;
528
+ }
529
+ l = r;
530
+ --f;
531
+ } else {
532
+ r = r.logBase(t);
533
+ if (l.eq(r)) {
534
+ f = 0;
535
+ break;
536
+ }
537
+ l = r;
538
+ ++f;
539
+ }
540
+ }
541
+ if (i == 100 || this.lt(Math.exp(1 / Math.E))) f = 0;
542
+ r.operatorE(1, (r.operatorE(1) + f) || f);
543
+ r.normalize();
544
+ return r;
545
+ }; // P.tetrate from ExpantaNum.js
546
+ Q.tetrate = Q.tetr = function (x, y, payload) {
547
+ return new PowiainaNum(x).tetr(y, payload);
548
+ };
549
+
550
+ //All of these are from Patashu's break_eternity.js
551
+ //The super square-root function - what number, tetrated to height 2, equals this?
552
+ //Other sroots are possible to calculate probably through guess and check methods, this one is easy though.
553
+ //https://en.wikipedia.org/wiki/Tetration#Super-root
554
+ P.ssqrt = P.ssrt = function () {
555
+ var x = this.clone();
556
+ if (x.lt(Math.exp(-1 / Math.E))) return PowiainaNum.NaN.clone();
557
+ if (!x.isFinite()) return x;
558
+ if (x.gt(PowiainaNum.TETRATED_MAX_SAFE_INTEGER)) return x;
559
+ if (x.gt(PowiainaNum.EE_MAX_SAFE_INTEGER)) {
560
+ x.operator(1, x.operator(1) - 1);
561
+ return x;
562
+ }
563
+ var l = x.ln();
564
+ return l.div(l.lambertw());
565
+ };
566
+ Q.ssqrt = Q.ssrt = function (x) {
567
+ return new PowiainaNum(x).ssqrt();
568
+ };
569
+ //Super-logarithm, one of tetration's inverses, tells you what size power tower you'd have to tetrate base to to get number. By definition, will never be higher than 1.8e308 in break_eternity.js, since a power tower 1.8e308 numbers tall is the largest representable number.
570
+ //Uses linear approximation
571
+ //https://en.wikipedia.org/wiki/Super-logarithm
572
+ P.slog = function (base) {
573
+ if (base === undefined) base = 10;
574
+ var x = new PowiainaNum(this);
575
+ base = new PowiainaNum(base);
576
+ if (x.isNaN() || base.isNaN() || x.isInfinite() && base.isInfinite()) return PowiainaNum.NaN.clone();
577
+ if (x.isInfinite()) return x;
578
+ if (base.isInfinite()) return PowiainaNum.ZERO.clone();
579
+ if (x.lt(PowiainaNum.ZERO)) return PowiainaNum.ONE.neg();
580
+ if (x.eq(PowiainaNum.ONE)) return PowiainaNum.ZERO.clone();
581
+ if (x.eq(base)) return PowiainaNum.ONE.clone();
582
+ if (base.lt(Math.exp(1 / Math.E))) {
583
+ var a = PowiainaNum.tetr(base, Infinity);
584
+ if (x.eq(a)) return PowiainaNum.POSITIVE_INFINITY.clone();
585
+ if (x.gt(a)) return PowiainaNum.NaN.clone();
586
+ }
587
+ if (x.max(base).gt("10^^^" + MAX_SAFE_INTEGER)) {
588
+ if (x.gt(base)) return x;
589
+ return PowiainaNum.ZERO.clone();
590
+ }
591
+ if (x.max(base).gt(PowiainaNum.TETRATED_MAX_SAFE_INTEGER)) {
592
+ if (x.gt(base)) {
593
+ x.operatorE(2, x.operatorE(2) - 1);
594
+ x.normalize();
595
+ return x.sub(x.operatorE(1));
596
+ }
597
+ return PowiainaNum.ZERO.clone();
598
+ }
599
+ var r = 0;
600
+ var t = (x.operatorE(1) || 0) - (base.operatorE(1) || 0);
601
+ if (t > 3) {
602
+ var l = t - 3;
603
+ r += l;
604
+ x.operatorE(1, x.operatorE(1) - l);
605
+ }
606
+ for (var i = 0; i < 100; ++i) {
607
+ if (x.lt(PowiainaNum.ZERO)) {
608
+ x = PowiainaNum.pow(base, x);
609
+ --r;
610
+ } else if (x.lte(1)) {
611
+ return new PowiainaNum(r + x.toNumber() - 1);
612
+ } else {
613
+ ++r;
614
+ x = PowiainaNum.logBase(x, base);
615
+ }
616
+ }
617
+ if (x.gt(10))
618
+ return new PowiainaNum(r);
619
+ };
620
+ Q.slog = function (x, y) {
621
+ return new PowiainaNum(x).slog(y);
622
+ };
623
+
624
+ //#region expansion multiexpansion powerexpansion
625
+ P.expansion = P.eps = function (other) {
626
+ var t = this.clone();
627
+ other = new PowiainaNum(other);
628
+ var r;
629
+ if (PowiainaNum.debug >= PowiainaNum.NORMAL) console.log("{" + t + "," + other + ",1,2}");
630
+ if (other.lte(PowiainaNum.ZERO) || !other.isint()) return PowiainaNum.NaN.clone();
631
+ if (other.eq(PowiainaNum.ONE)) return t.clone();
632
+ if (!t.isint()) return PowiainaNum.NaN.clone();
633
+ if (t.eq(2)) return new PowiainaNum(4);
634
+ if (t.neq(10) && other.lt(MAX_SAFE_INTEGER)) {
635
+ var f = other.toNumber() - 1;
636
+ r = t;
637
+ for (var i = 0; f !== 0 && r.lt(PowiainaNum.MAX_SAFE_INTEGER) && i < 100; ++i) {
638
+ if (f > 0) {
639
+ r = t.arrow(r)(t);
640
+ --f;
641
+ }
642
+ }
643
+ if (i == 100) f = 0;
644
+ r.array.push(["x", f, 1, 1]);
645
+ r.normalize();
646
+ return r;
647
+ //throw Error(powiainaNumError + "I can't handle that base is not 1, 2, or10")
648
+ }
649
+ if (other.gt(PowiainaNum.MULTIEXPANSIONED_MAX_SAFE_INTEGER)) {
650
+ return other.clone();
651
+ }
652
+ else if (other.gt(PowiainaNum.MAX_SAFE_INTEGER)) {
653
+
654
+ r = PowiainaNum(0)
655
+
656
+ r.array = []
657
+ r.array.push(...other.array);
658
+ r.array.push([1, 1, 2, 1]);
659
+
660
+ } else {
661
+ r = PowiainaNum()
662
+
663
+ r.array = [10]
664
+ r.array.push(["x", other.toNumber() - 1, 1, 1]);
665
+
666
+ }
667
+ return r.normalize();
668
+ };
669
+ Q.expansion = Q.eps = function (x, other) {
670
+ return PowiainaNum(x).expansion(other)
671
+ }
672
+
673
+ P.multiExpansion = P.mulEps = function (other) {
674
+ var t = this.clone();
675
+ other = new PowiainaNum(other);
676
+ var r;
677
+ if (PowiainaNum.debug >= PowiainaNum.NORMAL) console.log("{" + t + "," + other + ",2,2}");
678
+ if (other.lte(PowiainaNum.ZERO) || !other.isint()) return PowiainaNum.NaN.clone();
679
+ if (other.eq(PowiainaNum.ONE)) return t.clone();
680
+ if (!t.isint()) return PowiainaNum.NaN.clone();
681
+ if (t.eq(2)) return new PowiainaNum(4);
682
+ if (t.neq(10)) throw Error(powiainaNumError + "I can't handle that base is not 1, 2, or10")
683
+
684
+ if (other.gt(PowiainaNum.POWEREXPANSIONED_MAX_SAFE_INTEGER)) {
685
+ return other.clone();
686
+ }
687
+ else if (other.gt(PowiainaNum.MAX_SAFE_INTEGER)) {
688
+ r = PowiainaNum()
689
+
690
+ r.array = []
691
+ r.array.push(...other.array);
692
+ r.array.push([2, 1, 2, 1]);
693
+
694
+ } else {
695
+ r = PowiainaNum()
696
+
697
+ r.array = [10]
698
+ r.array.push([1, other.toNumber() - 1, 2, 1]);
699
+
700
+ }
701
+ return r.normalize();
702
+ }
703
+ Q.expansionArrow = Q.epsArrow = function (x, arrow) {
704
+ other = new PowiainaNum(x)
705
+ arrow = new PowiainaNum(arrow)
706
+ if (other.lte(PowiainaNum.ZERO) || !other.isint()) return PowiainaNum.NaN.clone();
707
+ if (!arrow.isint() || arrow.lt(1)) return PowiainaNum.NaN.clone();
708
+ if (other.eq(PowiainaNum.ONE)) return t.clone();
709
+ var r;
710
+ if (arrow.lt(MAX_SAFE_INTEGER)) {
711
+ if (other.gt("l0 s1 a[10000000000,[" + (arrow.toNumber().toString()) + "," + (MAX_SAFE_INTEGER - 1).toString() + ",1,1]]")) {
712
+ return other.clone();
713
+ }
714
+ else if (other.gt(PowiainaNum.MAX_SAFE_INTEGER)) {
715
+ r = PowiainaNum()
716
+
717
+ r.array = []
718
+ r.array.push(...other.array);
719
+ r.array.push([arrow.toNumber(), 1, 2, 1]);
720
+
721
+ } else {
722
+ r = PowiainaNum()
723
+
724
+ r.array = [10]
725
+ r.array.push([arrow.toNumber() - 1, other.toNumber() - 1, 2, 1]);
726
+
727
+ }
728
+ } else {
729
+ r = PowiainaNum(arrow)
730
+ r.array.push(["x", 1, 2, 1]);
731
+ }
732
+ return r.normalize();
733
+ }
734
+ Q.multiExpansion = Q.mulEps = function (x, other) {
735
+ return PowiainaNum(x).multiExpansion(other);
736
+ }
737
+ P.powerExpansion = P.powEps = function (other) {
738
+ var t = this.clone();
739
+ other = new PowiainaNum(other);
740
+ var r;
741
+ if (PowiainaNum.debug >= PowiainaNum.NORMAL) console.log("{" + t + "," + other + ",3,2}");
742
+ if (other.lte(PowiainaNum.ZERO) || !other.isint()) return PowiainaNum.NaN.clone();
743
+ if (other.eq(PowiainaNum.ONE)) return t.clone();
744
+ if (!t.isint()) return PowiainaNum.NaN.clone();
745
+ if (t.eq(2)) return new PowiainaNum(4);
746
+ if (t.neq(10)) throw Error(powiainaNumError + "I can't handle that base is not 1, 2, or10")
747
+ if (other.gt(PowiainaNum.MAX_SAFE_INTEGER)) {
748
+ r = PowiainaNum()
749
+
750
+ r.array = []
751
+ r.array.push(...other.array);
752
+ r.array.push([3, 1, 2, 1]);
753
+
754
+ } else {
755
+ r = PowiainaNum()
756
+
757
+ r.array = [10]
758
+ r.array.push([2, other.toNumber() - 1, 2, 1]);
759
+
760
+ }
761
+ return r.normalize();
762
+ }
763
+ Q.powerExpansion = Q.powEps = function (x, other) {
764
+ return PowiainaNum(x).powerExpansion(other);
765
+ }
766
+
767
+ //#endregion
768
+
769
+ //#region explosion
770
+ P.explosion = P.els = function (other) {
771
+ var t = this.clone();
772
+ other = new PowiainaNum(other);
773
+ var r;
774
+ if (PowiainaNum.debug >= PowiainaNum.NORMAL) console.log("{" + t + "," + other + ",2,2}");
775
+ if (other.lte(PowiainaNum.ZERO) || !other.isint()) return PowiainaNum.NaN.clone();
776
+ if (other.eq(PowiainaNum.ONE)) return t.clone();
777
+ if (!t.isint()) return PowiainaNum.NaN.clone();
778
+ if (t.eq(2)) return new PowiainaNum(4);
779
+ if (t.neq(10)) throw Error(powiainaNumError + "I can't handle that base is not 1, 2, or10")
780
+ if (other.gt(PowiainaNum.MAX_SAFE_INTEGER)) {
781
+ r = PowiainaNum()
782
+
783
+ r.array = []
784
+ r.array.push(...other.array);
785
+ r.array.push([1, 1, 3, 1]);
786
+
787
+ } else {
788
+ r = PowiainaNum()
789
+
790
+ r.array = [10]
791
+ r.array.push(["x", other.toNumber() - 1, 2, 1]);
792
+
793
+ }
794
+ return r.normalize();
795
+ };
796
+
797
+ Q.explosion = P.els = function (x, other) {
798
+ return PowiainaNum(x).explosion(other);
799
+ }
800
+
801
+ //#endregion
802
+ //#endregion
803
+
804
+ //#region arrow, pentate
805
+ P.pent = P.pentate = function (other) {
806
+ return this.arrow(3)(other);
807
+ }
808
+ Q.pent = Q.pentate = function (x, other) {
809
+ return PowiainaNum.arrow(x, 3, other);
810
+ }
811
+ /**
812
+ *
813
+ * @param {Number|PowiainaNum|String} other arrow count
814
+ * @returns {Function} a function can be called(x), this function returns {this,other,x}
815
+ */
816
+ P.arrow = function (other) {
817
+ var t = this.clone();
818
+ var arrows = new PowiainaNum(other);
819
+ if (!arrows.isint() || arrows.lt(PowiainaNum.ZERO)) return function (other) {
820
+ return PowiainaNum.NaN.clone();
821
+ };
822
+ if (arrows.eq(PowiainaNum.ZERO)) return function (other) {
823
+ return t.mul(other);
824
+ };
825
+ if (arrows.eq(PowiainaNum.ONE)) return function (other) {
826
+ return t.pow(other);
827
+ };
828
+ if (arrows.eq(2)) return function (other) {
829
+ return t.tetr(other);
830
+ };
831
+ return function (other) {
832
+ var depth;
833
+ if (arguments.length == 2) depth = arguments[1]; //must hide
834
+ else depth = 0;
835
+ other = new PowiainaNum(other);
836
+ var r;
837
+ if (PowiainaNum.debug >= PowiainaNum.NORMAL) console.log(t + "{" + arrows + "}" + other);
838
+ if (t.isNaN() || other.isNaN()) return PowiainaNum.NaN.clone();
839
+ if (other.lt(PowiainaNum.ZERO)) return PowiainaNum.NaN.clone();
840
+ if (t.eq(PowiainaNum.ZERO)) {
841
+ if (other.eq(PowiainaNum.ONE)) return PowiainaNum.ZERO.clone();
842
+ return PowiainaNum.NaN.clone();
843
+ }
844
+ if (t.eq(PowiainaNum.ONE)) return PowiainaNum.ONE.clone();
845
+ if (other.eq(PowiainaNum.ZERO)) return PowiainaNum.ONE.clone();
846
+ if (other.eq(PowiainaNum.ONE)) return t.clone();
847
+
848
+ // arrow > 9e15, that using 10{x}, x=arrow;
849
+ if (arrows.gt(PowiainaNum.MAX_SAFE_INTEGER)) {
850
+ r = PowiainaNum(arrows);
851
+ r.array.push(["x", 1, 1, 1]);
852
+ r.normalize();
853
+ return r;
854
+ }
855
+
856
+ let arrowsNum = arrows.toNumber();
857
+ // arrow < 9e15
858
+
859
+ // 10{x}2 = 10{x-1}10
860
+ if (other.eq(2)) return t.arrow(arrowsNum - 1)(t, depth + 1);
861
+
862
+ // 我不到啊
863
+ if (t.max(other).gt("10{" + (arrowsNum + 1) + "}" + MAX_SAFE_INTEGER)) return t.max(other);
864
+
865
+ // this = 10{1919810}1e16 > 10{1919810}9.007e15
866
+ // or t{arrow}other, other>9.007e15
867
+ if (t.gt("10{" + arrowsNum + "}" + MAX_SAFE_INTEGER) || other.gt(PowiainaNum.MAX_SAFE_INTEGER)) {
868
+
869
+ // this = 10{1919810}1e16 > 10{1919810}9.007e15
870
+ if (t.gt("10{" + arrowsNum + "}" + MAX_SAFE_INTEGER)) {
871
+ r = t.clone();
872
+ r.operatorE(arrowsNum, r.operatorE(arrowsNum) - 1);
873
+ r.normalize();
874
+ } else if (t.gt("10{" + (arrowsNum - 1) + "}" + MAX_SAFE_INTEGER)) {
875
+ r = new PowiainaNum(t.operatorE(arrowsNum - 1));
876
+ } else {
877
+ r = PowiainaNum.ZERO;
878
+ }
879
+ var j = r.add(other);
880
+ j.operatorE(arrowsNum, (j.operatorE(arrowsNum) || 0) + 1);
881
+ j.normalize();
882
+ return j;
883
+ }
884
+ if (depth >= PowiainaNum.maxOps + 40) {
885
+ r = PowiainaNum(10);
886
+ r.array = [10, [arrowsNum, 1, 1, 1]];
887
+ return r;
888
+ }
889
+ var y = other.toNumber();
890
+ var f = Math.floor(y);
891
+ var arrows_m1 = arrows.sub(PowiainaNum.ONE);
892
+ r = t.arrow(arrows_m1)(y - f, depth + 1);
893
+ for (var i = 0,
894
+ m = new PowiainaNum("10{" + (arrowsNum - 1) + "}" + MAX_SAFE_INTEGER);
895
+ f !== 0 && r.lt(m) && i < 100;
896
+ ++i) {
897
+ if (f > 0) {
898
+ r = t.arrow(arrows_m1)(r, depth + 1);
899
+ --f;
900
+ }
901
+ }
902
+ if (i == 100) f = 0;
903
+ r.operatorE(arrowsNum - 1, (r.operatorE(arrowsNum - 1) + f) || f);
904
+ r.normalize();
905
+ return r;
906
+ }
907
+ }
908
+ Q.arrow = function (x, z, y) {
909
+ return new PowiainaNum(x).arrow(z)(y);
910
+ };
911
+ P.add1J = function () {
912
+ return PowiainaNum.chain(10, 10, this);
913
+ }
914
+
915
+ P.chain = function (other, arrows) {
916
+ return this.arrow(arrows)(other);
917
+ };
918
+ Q.chain = function (x, y, z) {
919
+ return new PowiainaNum(x).arrow(z)(y);
920
+ };
921
+ Q.hyper = function (z) {
922
+ z = new PowiainaNum(z);
923
+ if (z.eq(PowiainaNum.ZERO)) return function (x, y) { return new PowiainaNum(y).eq(PowiainaNum.ZERO) ? new PowiainaNum(x) : new PowiainaNum(x).add(PowiainaNum.ONE); };
924
+ if (z.eq(PowiainaNum.ONE)) return function (x, y) { return PowiainaNum.add(x, y); };
925
+ return function (x, y) { return new PowiainaNum(x).arrow(z.sub(2))(y); };
926
+ };
927
+ //#endregion
928
+
929
+ //#endregion
930
+
931
+ //#region compareTo
932
+ P.compareTo = P.cmp = function (other) {
933
+ if (PowiainaNum.debug > PowiainaNum.NORMAL) console.log("cmpare", this, other)
934
+ if (!(other instanceof PowiainaNum)) other = new PowiainaNum(other);
935
+ if (isNaN(this.array[0]) || isNaN(other.array[0])) return NaN
936
+ if (this.array[0] == Infinity && other.array[0] != Infinity) return this.sign;
937
+ if (this.array[0] != Infinity && other.array[0] == Infinity) return -other.sign;
938
+ if (this.sign != other.sign) return this.sign;
939
+ if (this.array.length == 1 && other.array.length == 1 && this.array[0] == other.array[0]) return 0;
940
+ if (other.array[0] == 0) {
941
+ if (this.sign > 0) {
942
+ return 1
943
+ } else if (this.sign < 0) {
944
+ return -1
945
+ }
946
+ }
947
+ var m = this.sign;
948
+ var r;
949
+ if (this.layer > other.layer) r = 1
950
+ else if (this.layer < other.layer) r = -1;
951
+ else {
952
+ var e, f;
953
+ var i = 1;
954
+ var l = Math.min(this.array.length, other.array.length);
955
+
956
+
957
+ do {
958
+ var g = this.array[this.array.length - i];
959
+ var h = other.array[other.array.length - i];
960
+ if (typeof g == "number") e = [0, g, 1, 1];
961
+ else e = g;
962
+ if (typeof h == "number") f = [0, h, 1, 1];
963
+ else f = h;
964
+ if (e[3] > f[3] || (e[3] == f[3] && (
965
+ (e[2] == "x" && f[2] != "x") || e[2] > f[2] || (e[2] == f[2] && (
966
+ (e[0] == "x" && f[0] != "x") || e[0] > f[0] || (e[0] == f[0] && (
967
+ e[1] > f[1]
968
+ ))
969
+ ))
970
+ ))) { r = 1; break; }
971
+ if (e[3] < f[3] || (e[3] == f[3] && (
972
+ (e[2] != "x" && f[2] == "x") || e[2] < f[2] || (e[2] == f[2] && (
973
+ (e[0] != "x" && f[0] == "x") || e[0] < f[0] || (e[0] == f[0] && (
974
+ e[1] < f[1]
975
+ ))
976
+ ))
977
+ ))) { r = -1; break; }
978
+ i++
979
+ } while (i < l)
980
+ if (r === undefined) {
981
+ if (this.array.length == other.array.length) {
982
+ if (this.array[0] > other.array[0]) {
983
+ r = 1
984
+ } else if (this.array[0] < other.array[0]) {
985
+ r = -1
986
+ } else {
987
+
988
+ r = 0;
989
+ }
990
+ } else if (this.array.length > other.array.length) {
991
+ e = this.array[this.array.length - l];
992
+ if (e[0] >= 1 || e[1] > 10) {
993
+ r = 1;
994
+ } else {
995
+ r = -1;
996
+ }
997
+ } else {
998
+ e = other.array[other.array.length - l];
999
+ if (e[0] >= 1 || e[1] > 10) {
1000
+ r = -1;
1001
+ } else {
1002
+ r = 1;
1003
+ }
1004
+ }
1005
+ }
1006
+ }
1007
+ return r * m;
1008
+ }
1009
+
1010
+ Q.compare = Q.cmp = function (x, y) {
1011
+ return new PowiainaNum(x).cmp(y);
1012
+ };
1013
+ //#endregion
1014
+
1015
+ //#region floorceil
1016
+ P.isInteger = P.isint = function () {
1017
+ if (this.sign == -1) return this.abs().isint();
1018
+ if (this.gt(PowiainaNum.MAX_SAFE_INTEGER)) return true;
1019
+ return Number.isInteger(this.toNumber());
1020
+ };
1021
+ Q.isInteger = Q.isint = function (x) {
1022
+ return new PowiainaNum(x).isint();
1023
+ };
1024
+ P.floor = function () {
1025
+ if (this.isInteger()) return this.clone();
1026
+ return new PowiainaNum(Math.floor(this.toNumber()));
1027
+ };
1028
+ Q.floor = function (x) {
1029
+ return new PowiainaNum(x).floor();
1030
+ };
1031
+ P.ceiling = P.ceil = function () {
1032
+ if (this.isInteger()) return this.clone();
1033
+ return new PowiainaNum(Math.ceil(this.toNumber()));
1034
+ };
1035
+ Q.ceiling = Q.ceil = function (x) {
1036
+ return new PowiainaNum(x).ceil();
1037
+ };
1038
+ P.round = function () {
1039
+ if (this.isInteger()) return this.clone();
1040
+ return new PowiainaNum(Math.round(this.toNumber()));
1041
+ };
1042
+ Q.round = function (x) {
1043
+ return new PowiainaNum(x).round();
1044
+ };
1045
+ //#endregion
1046
+
1047
+ //#region from ExpantaNum.js comparing
1048
+
1049
+ P.greaterThan = P.gt = function (other) {
1050
+ return this.cmp(other) > 0;
1051
+ };
1052
+ Q.greaterThan = Q.gt = function (x, y) {
1053
+ return new PowiainaNum(x).gt(y);
1054
+ };
1055
+ P.greaterThanOrEqualTo = P.gte = function (other) {
1056
+ return this.cmp(other) >= 0;
1057
+ };
1058
+ Q.greaterThanOrEqualTo = Q.gte = function (x, y) {
1059
+ return new PowiainaNum(x).gte(y);
1060
+ };
1061
+ P.lessThan = P.lt = function (other) {
1062
+ return this.cmp(other) < 0;
1063
+ };
1064
+ Q.lessThan = Q.lt = function (x, y) {
1065
+ return new PowiainaNum(x).lt(y);
1066
+ };
1067
+ P.lessThanOrEqualTo = P.lte = function (other) {
1068
+ return this.cmp(other) <= 0;
1069
+ };
1070
+ Q.lessThanOrEqualTo = Q.lte = function (x, y) {
1071
+ return new PowiainaNum(x).lte(y);
1072
+ };
1073
+ P.equalsTo = P.equal = P.eq = function (other) {
1074
+ return this.cmp(other) === 0;
1075
+ };
1076
+ Q.equalsTo = Q.equal = Q.eq = function (x, y) {
1077
+ return new PowiainaNum(x).eq(y);
1078
+ };
1079
+ P.notEqualsTo = P.notEqual = P.neq = function (other) {
1080
+ return this.cmp(other) !== 0;
1081
+ };
1082
+ Q.notEqualsTo = Q.notEqual = Q.neq = function (x, y) {
1083
+ return new PowiainaNum(x).neq(y);
1084
+ };
1085
+ P.minimum = P.min = function (other) {
1086
+ return this.lt(other) ? this.clone() : new PowiainaNum(other);
1087
+ };
1088
+ Q.minimum = Q.min = function (x, y) {
1089
+ return new PowiainaNum(x).min(y);
1090
+ };
1091
+ P.maximum = P.max = function (other) {
1092
+ return this.gt(other) ? this.clone() : new PowiainaNum(other);
1093
+ };
1094
+ Q.maximum = Q.max = function (x, y) {
1095
+ return new PowiainaNum(x).max(y);
1096
+ };
1097
+ P.isPositive = P.ispos = function () {
1098
+ return this.gt(PowiainaNum.ZERO);
1099
+ };
1100
+ Q.isPositive = Q.ispos = function (x) {
1101
+ return new PowiainaNum(x).ispos();
1102
+ };
1103
+ P.isNegative = P.isneg = function () {
1104
+ return this.lt(PowiainaNum.ZERO);
1105
+ };
1106
+ Q.isNegative = Q.isneg = function (x) {
1107
+ return new PowiainaNum(x).isneg();
1108
+ };
1109
+
1110
+ //#endregion
1111
+
1112
+ //#region from ExpantaNum.js operator
1113
+ P.getOperatorIndex = function (i, j = 1, k = 1) {
1114
+ /**
1115
+
1116
+ PS:
1117
+ in ExpantaNum.js, P.operator need only 1 parameter (arrow count?)
1118
+
1119
+ but in PowiainaNum.js, P.operator need 3 parameters
1120
+ (arrowcount, {{}}brace count(1), [[]]brace count(1))
1121
+
1122
+ such as 10{1145141919810}229028, in ExpantaNum.toString,
1123
+ it equals to (10{1145141919809})^229026 (10{1145141919808})^8 (10{1145141919807})^8 ... (10{1145141918813})^8 (10{1145141918812})^8 10
1124
+ so ExpantaNum("10{1145141919810}229028").operator(1145141919809) equals 229026;
1125
+
1126
+ that P.operator(i,j,k) returns the repeat count of ({10,x,i,j,k}, x= ...)^repeatcount has
1127
+
1128
+
1129
+
1130
+ We back to the method P.getOperatorIndex;
1131
+
1132
+ sometime it returns NON-INTEGER(such as 4.5, -0.5), 4.5 means this number's max arrows used.
1133
+ (but i don't know how -0.5 means.)
1134
+
1135
+ such as a = ExpantaNum("10{5}100")
1136
+ a.toString() = (10^^^^)^98 (10^^^)^8 (10^^)^8 (10^)^8 10000000000
1137
+
1138
+ a.getOperatorIndex(2) = 2
1139
+ so a.array[a.getOperatorIndex(2)] == [2,8]
1140
+
1141
+ such^2 as b = ExpantaNum("(10{6})^5 (10^^^)^5 10")
1142
+ b.toString() = (10{6})^5 (10^^^)^4 (10^^)^8 (10^)^8 10000000000;
1143
+
1144
+ b.getOperatorIndex(6) = 4
1145
+ so b.array[b.getOperatorIndex(6)] == [6,5]
1146
+ */
1147
+ if (i != "x" && typeof i != "number") i = Number(i);
1148
+ if (i != "x" && !isFinite(i)) throw Error(invalidArgument + "Index i out of range.");
1149
+ if (j != "x" && typeof j != "number") j = Number(j);
1150
+ if (j != "x" && !isFinite(j)) throw Error(invalidArgument + "Index j out of range.");
1151
+ if (typeof k != "number") k = Number(k);
1152
+ if (!isFinite(k)) throw Error(invalidArgument + "Index k out of range.");
1153
+ var a = this.array;
1154
+ var min = 0, max = a.length - 1;
1155
+ if (a.length == 1) {
1156
+ if (0 > i) return -0.5
1157
+ if (0 == i) return 0
1158
+ if (0 < i) return 0.5
1159
+ }
1160
+ if (a[max][3] < k) return max + 0.5
1161
+ if (a[max][3] > k) return -0.5
1162
+ if (a[max][2] != "x" && j == "x") return max + 0.5
1163
+ if (a[max][2] == "x" && j != "x") return -0.5
1164
+ if (a[max][0] < i) return max + 0.5
1165
+ if (a.length > 1 && 0 > i) return -0.5
1166
+ else if (0 > i) return -0.5
1167
+
1168
+ var repeatcount = 500
1169
+ while (min != max) {
1170
+ if (a[min][3] == k && a[min][2] == j && a[min][0] == i) return min;
1171
+ if (a[max][3] == k && a[max][2] == j && a[max][0] == i) return max;
1172
+
1173
+ var mid = Math.floor((min + max) / 2);
1174
+
1175
+ if (min == mid || (a[mid][0] == i && a[mid][2] == j && a[mid][3] == k)) {
1176
+ min = mid;
1177
+ break;
1178
+ }
1179
+ if (a[mid][3] < k || a[mid][2] < j || a[mid][0] < i) min = mid;
1180
+ if (a[mid][3] > k || a[mid][2] > j || a[mid][0] > i) max = mid;
1181
+ if (--repeatcount <= 0) break;
1182
+ }
1183
+ if (min == 0 && i == 0) {
1184
+ return min;
1185
+ } else {
1186
+ return a[min][0] == i ? min : min + 0.5;
1187
+ }
1188
+ }
1189
+ P.getOperator = function (i, j = 1, k = 1) {
1190
+ if (i != "x" && typeof i != "number") i = Number(i);
1191
+ if (i != "x" && !isFinite(i)) throw Error(invalidArgument + "Index i out of range.");
1192
+ if (j != "x" && typeof j != "number") j = Number(j);
1193
+ if (j != "x" && !isFinite(j)) throw Error(invalidArgument + "Index j out of range.");
1194
+ if (typeof k != "number") k = Number(k);
1195
+ if (!isFinite(k)) throw Error(invalidArgument + "Index k out of range.");
1196
+ var ai = this.getOperatorIndex(i, j, k);
1197
+ if (Number.isInteger(ai) && ai != 0) return this.array[ai][1];
1198
+ else if (ai == 0) return this.array[0]
1199
+ else return i === 0 ? 10 : 0;
1200
+
1201
+ }
1202
+ P.setOperator = function (i, j = 1, k = 1, value) {
1203
+ if (i != "x" && typeof i != "number") i = Number(i);
1204
+ if (i != "x" && !isFinite(i)) throw Error(invalidArgument + "Index i out of range.");
1205
+ if (j != "x" && typeof j != "number") j = Number(j);
1206
+ if (j != "x" && !isFinite(j)) throw Error(invalidArgument + "Index j out of range.");
1207
+ if (typeof k != "number") k = Number(k);
1208
+ if (!isFinite(k)) throw Error(invalidArgument + "Index k out of range.");
1209
+ var ai = this.getOperatorIndex(i, j, k);
1210
+ if (Number.isInteger(ai)) this.array[ai][1] = value;
1211
+ else {
1212
+ ai = Math.ceil(ai);
1213
+ this.array.splice(ai, 0, [i, value]);
1214
+ }
1215
+ this.normalize();
1216
+ };
1217
+ P.operator = function (i, j = 1, k = 1, value) {
1218
+
1219
+ if (i != "x" && typeof i != "number") i = Number(i);
1220
+ if (i != "x" && !isFinite(i)) throw Error(invalidArgument + "Index i out of range.");
1221
+ if (j != "x" && typeof j != "number") j = Number(j);
1222
+ if (j != "x" && !isFinite(j)) throw Error(invalidArgument + "Index j out of range.");
1223
+ if (typeof k != "number") k = Number(k);
1224
+ if (!isFinite(k)) throw Error(invalidArgument + "Index k out of range.");
1225
+
1226
+ if (value === undefined) return this.getOperator(i, j, k);
1227
+ else this.setOperator(i, j, k, value)
1228
+ }
1229
+
1230
+ P.operatorE = function (i, value) {
1231
+
1232
+ if (i != "x" && typeof i != "number") i = Number(i);
1233
+ if (i != "x" && !isFinite(i)) throw Error(invalidArgument + "Index i out of range.");
1234
+
1235
+ if (value === undefined) return this.getOperator(i, 1, 1);
1236
+ else this.setOperator(i, 1, 1, value)
1237
+
1238
+ }
1239
+ //#endregion
1240
+ //#region operator
1241
+ P.getMaxFirstOperatorIndex = function (j = 1, k = 1) {
1242
+ // returns [x,anything,j,k] that x is max.
1243
+ for (let i = this.array.length - 1; i >= 1; i--) {
1244
+ if (this.array[i][2] == j && this.array[i][3] == k) {
1245
+ return i;
1246
+ }
1247
+ }
1248
+ // can not find this operator with [x,anything,j,k]
1249
+ return -1
1250
+ }
1251
+ //#endregion
1252
+ //#region isFinite isInfinite isNaN
1253
+
1254
+
1255
+ P.isFinite = function () {
1256
+ return isFinite(this.array[0]);
1257
+ };
1258
+ Q.isFinite = function (x) {
1259
+ return new PowiainaNum(x).isFinite();
1260
+ };
1261
+ P.isInfinite = function () {
1262
+ return this.array[0] == Infinity;
1263
+ };
1264
+ Q.isInfinite = function (x) {
1265
+ return new PowiainaNum(x).isInfinite();
1266
+ };
1267
+ P.isNaN = function () {
1268
+ return isNaN(this.array[0])
1269
+ }
1270
+ Q.isNaN = function (x) {
1271
+ return new PowiainaNum(x).isNaN();
1272
+ }
1273
+ //#endregion
1274
+
1275
+ //#region absoluteValue negative
1276
+ P.absoluteValue = P.abs = function () {
1277
+ var x = this.clone();
1278
+ x.sign = 1;
1279
+ return x;
1280
+ };
1281
+ Q.absoluteValue = Q.abs = function (x) {
1282
+ return new PowiainaNum(x).abs();
1283
+ };
1284
+ P.negate = P.neg = function () {
1285
+ var x = this.clone();
1286
+ x.sign = x.sign * -1;
1287
+ return x;
1288
+ };
1289
+ Q.negate = Q.neg = function (x) {
1290
+ return new PowiainaNum(x).neg();
1291
+ };
1292
+ //#endregion
1293
+
1294
+
1295
+
1296
+ P.normalize = function () {
1297
+ var b;
1298
+ var x = this;
1299
+ if (PowiainaNum.debug >= PowiainaNum.ALL) console.log(x.toString())
1300
+ if (!x.array.length) {
1301
+ x.array = [0]
1302
+ }
1303
+ if (x.array[0] == Infinity) {
1304
+ x.array = [Infinity];
1305
+ return x;
1306
+ }
1307
+ if (x.sign != 1 && x.sign != -1) {
1308
+ if (typeof x.sign != "number") x.sign = Number(x.sign);
1309
+ x.sign = x.sign < 0 ? -1 : 1;
1310
+ }
1311
+ if (x.layer > MAX_SAFE_INTEGER) {
1312
+ x.array = [Infinity]
1313
+ x.layer = 0;
1314
+ return x;
1315
+ }
1316
+ if (Number.isInteger(x.layer)) x.layer = Math.floor(x.layer);
1317
+ var maxWhileTime = 500;
1318
+ var whileTimeRuns = 0
1319
+ for (var i = 1; i < x.array.length; ++i) {
1320
+ var e = x.array[i]
1321
+ if (e[0] === null || e[0] === undefined) {
1322
+ e[0] = 0
1323
+ }
1324
+ if (e[2] === null || e[3] === undefined) {
1325
+ e[2] = 1
1326
+ }
1327
+ if (e[3] === null || e[3] === undefined) {
1328
+ e[3] = 1
1329
+ }
1330
+ if ((isNaN(e[0]) && e[0] != "x") || isNaN(e[1]) || (isNaN(e[2]) && e[2] != "x") || isNaN(e[3])) {
1331
+ x.array = [NaN];
1332
+ return x;
1333
+ }
1334
+ if ((!isFinite(e[0]) && e[0] != "x") || !isFinite(e[1]) || (!isFinite(e[2]) && e[2] != "x") || !isFinite(e[2])) {
1335
+ x.array = [Infinity];
1336
+ return x;
1337
+ }
1338
+ if (e[0] != "x" && !Number.isInteger(e[0])) e[0] = Math.floor(e[0]);
1339
+ if (!Number.isInteger(e[1])) e[1] = Math.floor(e[1]);
1340
+ if (e[2] != "x" && !Number.isInteger(e[2])) e[1] = Math.floor(e[2]);
1341
+ if (!Number.isInteger(e[3])) e[1] = Math.floor(e[3]);
1342
+
1343
+ }
1344
+ do {
1345
+ if (PowiainaNum.debug >= PowiainaNum.ALL) console.log(x.toString());
1346
+ b = false;
1347
+
1348
+ // sort array
1349
+ x.array.sort(function (a, b) {
1350
+ return (function (a, b) {
1351
+ if (typeof a == 'number') {
1352
+ return 1
1353
+ } else if (a[3] > b[3]) {
1354
+ return 1
1355
+ } else if (a[3] < b[3]) {
1356
+ return -1
1357
+ }
1358
+ else if (a[2] == "x" && b[2] != "x") {
1359
+ return 1
1360
+ } else if (a[2] != "x" && b[2] == "x") {
1361
+ return -1
1362
+ } else if (a[2] == "x" && b[2] == "x") {
1363
+ return 1
1364
+ } else if (a[2] > b[2]) {
1365
+ return 1
1366
+ } else if (a[2] < b[2]) {
1367
+ return -1
1368
+ } else if (a[0] == "x" && b[0] != "x") {
1369
+ return 1
1370
+ } else if (a[0] != "x" && b[0] == "x") {
1371
+ return -1
1372
+ } else if (a[0] == "x" && b[0] == "x") {
1373
+ return 1
1374
+ }
1375
+ else if (a[0] > b[0]) {
1376
+ return 1
1377
+ }
1378
+ else if (a[0] < b[0]) {
1379
+ return -1
1380
+ } else if (a[1] > b[1]) {
1381
+ return 1
1382
+ } else if (a[1] < b[1]) {
1383
+ return -1
1384
+ }
1385
+
1386
+
1387
+ return 1
1388
+ })(a, b)
1389
+ })
1390
+ for (i = 1; i < x.array.length; ++i) {
1391
+ // check 0 repeat count
1392
+ if (x.array[i][0] !== 0 && (x.array[i][1] === 0 || x.array[i][1] === null || x.array[i][1] === undefined)) {
1393
+ x.array.splice(i, 1);
1394
+ --i;
1395
+ continue;
1396
+ }
1397
+ // check arrow 0 and brace count >=2
1398
+ if (x.array[i][0] == 0 && x.array[i][2] >= 2) {
1399
+ x.array[i][0] = "x"
1400
+ x.array[i][2] = x.array[i][2] - 1
1401
+ }
1402
+
1403
+ }
1404
+
1405
+ if (!x.array.length) x.array = [0]; // if no array set zero
1406
+ if (x.array.length > PowiainaNum.maxOps) x.array.splice(1, x.array.length - PowiainaNum.maxOps); // max operators check
1407
+
1408
+ if (x.array[0] > MAX_SAFE_INTEGER) { // check >9e15 first number
1409
+ if (x.array.length >= 2 && x.array[1][0] == 1 && x.array[1][2] == 1 && x.array[1][3] == 1) {
1410
+ x.array[1][1]++;
1411
+ } else {
1412
+ x.array.splice(1, 0, [1, 1, 1, 1]);
1413
+ }
1414
+ x.array[0] = Math.log10(x.array[0])
1415
+ }
1416
+ if (x.array[0] < MAX_E && x.array[1] !== undefined &&
1417
+ x.array[1][0] == 1 && x.array[1][1] >= 1 &&
1418
+ x.array[1][2] == 1 && x.array[1][3] == 1) { // check < 9e15
1419
+
1420
+ x.array[0] = Math.pow(10, x.array[0]);
1421
+ if (x.array[1][1] > 1) {
1422
+ x.array[1][1]--;
1423
+ } else {
1424
+ x.array.splice(1, 1);
1425
+ }
1426
+ b = true;
1427
+ }
1428
+ if (x.array[x.array.length - 1][3] > MAX_SAFE_INTEGER) { // check layer++
1429
+ x.layer++;
1430
+ x.array = [
1431
+ x.array[x.array.length - 1][3]
1432
+ ];
1433
+ b = true;
1434
+ } else if (x.layer && x.array.length == 1) {
1435
+ x.layer--;
1436
+ x.array = [
1437
+ 10
1438
+ ];
1439
+ b = true;
1440
+ }
1441
+ while (x.array.length >= 2 && x.array[0] == 1 && x.array[1][1]) {
1442
+ // [1, [sth, 1, sth, sth]]
1443
+ if (x.array[1][1] > 1) {
1444
+ x.array[1][1]--;
1445
+ } else {
1446
+ x.array.splice(1, 1);
1447
+ }
1448
+ x.array[0] = 10;
1449
+ b = true;
1450
+ }
1451
+ if (x.array.length >= 2 && x.array[0] < MAX_SAFE_INTEGER && x.array[1][0] >= 2 && x.array[1][1] == 1) {
1452
+ // [1e9, [2, 1, sth, sth]]
1453
+ x.array.splice(1, 1, [x.array[1][0] - 1, x.array[0] - 1, x.array[1][2], x.array[1][3]]);
1454
+
1455
+ x.array[0] = 10;
1456
+ b = true
1457
+ }
1458
+ for (i = 1; i < x.array.length - 1; ++i) {
1459
+ if (x.array[i][0] == x.array[i + 1][0] && x.array[i][2] == x.array[i + 1][2] && x.array[i][3] == x.array[i + 1][3]) {
1460
+ // same array's merge
1461
+ x.array[i][1] += x.array[i + 1][1];
1462
+ x.array.splice(i + 1, 1);
1463
+ --i;
1464
+ b = true;
1465
+ }
1466
+
1467
+ }
1468
+ if (x.array.length >= 2 && x.array[1][0] != 1 && x.array[1][0] != "x" && x.array[1][0] < 1000) {
1469
+ // [sth, [2, sth, sth, sth]]
1470
+ if (x.array[0]) x.array.splice(1, 0, [x.array[1][0] - 1, x.array[0], x.array[1][2], x.array[1][3]]);
1471
+ x.array[0] = 1;
1472
+ if (x.array[2][1] > 1) {
1473
+ x.array[2][1]--;
1474
+ } else {
1475
+ x.array.splice(2, 1);
1476
+ } // && x.array[1][2] == 1 && x.array[1][3] == 1
1477
+ b = true;
1478
+ }
1479
+ if (x.array.length >= 2 && x.array[1][0] == 'x' && x.array[0] < MAX_SAFE_INTEGER) {
1480
+ // check [1000, ["x", y, 1, 1]] (like (10{x})^y sth<9e15)
1481
+
1482
+ // [10, [1000, 1, 1, 1], ["x", y-1, 1, 1]]
1483
+ if (x.array[1][1] == 1) {
1484
+ x.array[1][0] = x.array[0];
1485
+ x.array[0] = 10;
1486
+ } else {
1487
+ // [q, ["x", y, z, w]]
1488
+
1489
+ // Insert at index 1, insert [x.array[0], 1, z, w]
1490
+ // [q, [q, 1, z, w], ["x", y, z, w]]
1491
+ x.array.splice(1, 0, [x.array[0], 1, x.array[1][2], x.array[1][3]])
1492
+
1493
+ // [10, [q, 1, z, w], ["x", y-1, z, w]]
1494
+ x.array[0] = 10;
1495
+ x.array[2][1]--;
1496
+ }
1497
+ b = true;
1498
+ }
1499
+ if (x.array.length >= 2 && x.array[1][2] >= 2 && x.array[1][0] == 1 && x.array[0] < MAX_SAFE_INTEGER) {
1500
+ // [8e15, [1, sth, >=2, sth]]
1501
+ if (x.array[1][1] == 1) {
1502
+ x.array.splice(1, 1, ["x", x.array[0] - 1, x.array[1][2] - 1, x.array[1][3]])
1503
+ x.array[0] = 10
1504
+ b = true;
1505
+ } else if (x.array[1][1] > 1) {
1506
+ var temp = x.array[1][1];
1507
+ x.array.splice(1, 1, ["x", x.array[0] - 1, x.array[1][2] - 1, x.array[1][3]])
1508
+ x.array.push([1, temp - 1, 2, 1])
1509
+ x.array[0] = 10
1510
+ b = true;
1511
+ }
1512
+ }
1513
+ for (i = 1; i < x.array.length; ++i) {
1514
+ if (x.array[i][2] > MAX_SAFE_INTEGER) {
1515
+ // check {}brace >9e15
1516
+ if (i != x.array.length - 1 && x.array[i + 1][2] == "x") {
1517
+ x.array[i + 1][1]++;
1518
+ } else if (x.array[i][2] != "x") {
1519
+ x.array.splice(i + 1, 0, [1, 1, "x", x.array[i][3]]);
1520
+
1521
+ } else if (x.array[i][2] == "x") {
1522
+ x.array.splice(i + 1, 0, [1, 1, 1, x.array[i][3]] + 1);
1523
+
1524
+ }
1525
+ x.array[0] = x.array[i][2] + 1;
1526
+ x.array.splice(1, i);
1527
+ --i
1528
+ b = true;
1529
+ }
1530
+ if (i < 1) i = 1;
1531
+ if (x.array[i][1] > MAX_SAFE_INTEGER) {
1532
+ // check repeat count >9e15
1533
+ if (i != x.array.length - 1 && x.array[i + 1][0] == "x") {
1534
+ x.array[i + 1][1]++;
1535
+ } else if (x.array[i][0] != "x") {
1536
+ x.array.splice(i + 1, 0, [x.array[i][0] + 1, 1, x.array[i][2], x.array[i][3]]);
1537
+
1538
+ } else if (x.array[i][0] == "x") {
1539
+ x.array.splice(i + 1, 0, [1, 1, x.array[i][2] + 1, x.array[i][3]]);
1540
+
1541
+ }
1542
+ x.array[0] = x.array[i][1] + 1;
1543
+ x.array.splice(1, i);
1544
+ --i
1545
+ b = true;
1546
+ }
1547
+ if (i < 1) i = 1;/*
1548
+ if (x.array[i][2] > MAX_SAFE_INTEGER) {
1549
+ x.array[0] = x.array[i][2] + 1;
1550
+
1551
+ if (i != x.array.length -1 && x.array[i+1][2] == "x"){
1552
+ x.array[i+1][1]++;
1553
+ x.array.splice(i,1)
1554
+ }else if (x.array[i][2] != "x"){
1555
+ x.array.push([1, 1, "x", x.array[i][3]])
1556
+ x.array.splice(i,1)
1557
+ }
1558
+ --i
1559
+ b = true;
1560
+ }*/
1561
+ if (i < 1) i = 1;
1562
+ if (x.array[i][0] > MAX_SAFE_INTEGER) {
1563
+ x.array[0] = x.array[i][0] + 1;
1564
+ if (i != x.array.length - 1 && x.array[i + 1][0] == "x") {
1565
+ x.array[i + 1][1]++;
1566
+ } else {
1567
+ x.array.splice(i + 1, 0, ["x", 1, x.array[i][2], x.array[i][3]]);
1568
+ }
1569
+ x.array.splice(1, i);
1570
+
1571
+ b = true;
1572
+ }
1573
+
1574
+ }
1575
+ whileTimeRuns++
1576
+ } while (b && whileTimeRuns < maxWhileTime)
1577
+ return x;
1578
+ }
1579
+ P.overflow = function (start, power, meta = 1) {
1580
+ var number = this.clone()
1581
+ power = PowiainaNum(power)
1582
+ meta = PowiainaNum(meta)
1583
+ start = PowiainaNum(start)
1584
+ if (number.gte(start)) {
1585
+ var s = start.iteratedlog(10, meta)
1586
+ number = PowiainaNum(10).iteratedexp(meta, number.iteratedlog(10, meta).div(s).pow(power).mul(s));
1587
+ }
1588
+ return number
1589
+ }
1590
+ Q.overflow = function (num, start, power, meta = 1) {
1591
+ return PowiainaNum(num).overflow(start, power, meta)
1592
+ }
1593
+
1594
+ P.toJSON = function () {
1595
+ if (PowiainaNum.serializeMode == PowiainaNum.JSON) {
1596
+ var a = [];
1597
+ a.push(this.array[0])
1598
+ for (var i = 1; i < this.array.length; ++i) a.push([this.array[i][0], this.array[i][1], this.array[i][2], this.array[i][3]]);
1599
+ return {
1600
+ array: a,
1601
+ layer: this.layer,
1602
+ sign: this.sign
1603
+ };
1604
+ } else if (PowiainaNum.serializeMode == PowiainaNum.STRING) {
1605
+ return this.toString(1);
1606
+ }
1607
+
1608
+ }
1609
+
1610
+
1611
+ P.toString = function (formatType = 0) {
1612
+ if (isNaN(this.array[0])) return "NaN";
1613
+ if (!isFinite(this.array[0])) return "Infinity";
1614
+ var s = "";
1615
+ if (formatType == 1) {
1616
+ s = "l" + (this.layer.toString()) + " s" + (this.sign.toString()) + " a" + JSON.stringify(this.array)
1617
+ } else if (formatType == 0) {
1618
+ for (let i = this.array.length - 1; i > 0; i--) {
1619
+ /*if (this.array[i][0] == "x" && this.array[i][2] == 1 && this.array[i][3] == 1){
1620
+ if (this.array[i][1] >= 5)
1621
+ s += "J^"+this.array[i][1] + " "
1622
+ else
1623
+ s += "J".repeat(this.array[i][1]) + " "
1624
+ }
1625
+
1626
+ else */if (this.array[i][3] == 1 && this.array[i][2] == 1) s += "(" + "10{" + this.array[i][0] + "})" + "^" + this.array[i][1] + " "
1627
+ else if (this.array[i][3] == 1) s += "(" + "eps" + this.array[i][2] + ",10{" + this.array[i][0] + "})" + "^" + this.array[i][1] + " "
1628
+ else s += "(" + "meg" + this.array[i][3] + ",eps" + this.array[i][2] + ",10{" + this.array[i][0] + "})" + "^" + this.array[i][1] + " "
1629
+ }
1630
+ s += this.array[0]
1631
+ s = (this.sign < 0 ? "-" : "") + (this.layer > 0 ? `lay${this.layer.toFixed(0)} ` : "") + s
1632
+ }
1633
+ return s
1634
+ }
1635
+ Q.fromNumber = function (input) {
1636
+ if (typeof input != 'number') throw Error(invalidArgument + "Expected Number");
1637
+ var x = new PowiainaNum();
1638
+ x.array[0] = Math.abs(input);
1639
+ x.sign = input < 0 ? -1 : 1;
1640
+ x.normalize();
1641
+ return x;
1642
+ }
1643
+
1644
+ var LONG_STRING_MIN_LENGTH = 17;
1645
+ var log10LongString = function log10LongString(str) {
1646
+ return Math.log10(Number(str.substring(0, LONG_STRING_MIN_LENGTH))) + (str.length - LONG_STRING_MIN_LENGTH);
1647
+ }
1648
+ Q.fromString = function (input, nocache=false) {
1649
+ originalInput = input;
1650
+ if (typeof input != "string") throw Error(invalidArgument + "Expected String");
1651
+ if (C[input] && !nocache) {
1652
+ var x = new PowiainaNum();
1653
+ deepCopyProps(C[input].array, x.array)
1654
+ x.sign = C[input].sign
1655
+ x.layer = C[input].layer
1656
+ return x
1657
+ } else if (input[0] == "l") {
1658
+ let temp1 = input.indexOf("l");
1659
+ let temp2 = input.indexOf("s");
1660
+ let temp3 = input.indexOf("a");
1661
+ if (temp2 == -1 || temp3 == -1) {
1662
+ console.warn(powiainaNumError + "Malformed input: " + input);
1663
+ return PowiainaNum.NaN.clone();
1664
+ }
1665
+ let layer = Number(input.slice(temp1 + 1, temp2 - 1))
1666
+ let sign = Number(input.slice(temp2 + 1, temp3 - 1))
1667
+ let array = JSON.parse(input.slice(temp3 + 1, input.length))
1668
+
1669
+ var x = PowiainaNum();
1670
+ x.layer = layer;
1671
+ x.sign = sign;
1672
+ x.array = array;
1673
+ x.normalize();
1674
+ return x;
1675
+ } else {
1676
+ // from ExpantaNum.js
1677
+ var x = new PowiainaNum();
1678
+ var negateIt = false;
1679
+ if (input[0] == "-" || input[0] == "+") {
1680
+ var numSigns = input.search(/[^-\+]/);
1681
+ var signs = input.substring(0, numSigns);
1682
+ negateIt = signs.match(/-/g).length % 2 == 1;
1683
+ input = input.substring(numSigns);
1684
+ }
1685
+ if (input == "NaN") x.array = [
1686
+ NaN
1687
+ ];
1688
+ else if (input == "Infinity") x.array = [
1689
+ Infinity
1690
+ ];
1691
+ else {
1692
+ x.array = [
1693
+ 0
1694
+ ];
1695
+ var a, b, c, d, i;
1696
+
1697
+ var Klayer = 0;
1698
+ if (input[0] == "J") {
1699
+ if (input[1] == "^") {
1700
+ a = input.substring(2).search(/[^0-9]/) + 2;
1701
+ Klayer = Number(input.substring(2, a));
1702
+ input = input.substring(a + 1);
1703
+ } else {
1704
+ a = input.search(/[^J]/);
1705
+ Klayer = a;
1706
+ input = input.substring(a);
1707
+ }
1708
+ }
1709
+ while (input) {
1710
+ if (/^\(?10[\^\{]/.test(input)) {
1711
+ if (input[0] == "(") {
1712
+ input = input.substring(1);
1713
+ }
1714
+ var arrows;
1715
+ if (input[2] == "^") {
1716
+ a = input.substring(2).search(/[^\^]/);
1717
+ arrows = a;
1718
+ b = a + 2;
1719
+ } else {
1720
+ a = input.indexOf("}");
1721
+ arrows = Number(input.substring(3, a));
1722
+ b = a + 1;
1723
+ }
1724
+ input = input.substring(b);
1725
+ if (input[0] == ")") {
1726
+ a = input.indexOf(" ");
1727
+ c = Number(input.substring(2, a));
1728
+ input = input.substring(a + 1);
1729
+ } else {
1730
+ c = 1;
1731
+ }
1732
+ if (arrows == 1) {
1733
+ if (x.array.length >= 2 && x.array[1][0] == 1) {
1734
+ x.array[1][1] += c;
1735
+ } else {
1736
+ x.array.splice(1, 0, [1, c, 1, 1]);
1737
+ }
1738
+ } else if (arrows == 2) {
1739
+ a = x.array.length >= 2 && x.array[1][0] == 1 ? x.array[1][1] : 0;
1740
+ b = x.array[0];
1741
+ if (b >= 1e10) ++a;
1742
+ if (b >= 10) ++a;
1743
+ x.array[0] = a;
1744
+ if (x.array.length >= 2 && x.array[1][0] == 1) x.array.splice(1, 1);
1745
+ d = x.getOperatorIndex(2);
1746
+ if (Number.isInteger(d)) x.array[d][1] += c;
1747
+ else x.array.splice(Math.ceil(d), 0, [2, c, 1, 1]);
1748
+ } else {
1749
+ a = x.operatorE(arrows - 1);
1750
+ b = x.operatorE(arrows - 2);
1751
+ if (b >= 10) ++a;
1752
+ d = x.getOperatorIndex(arrows);
1753
+ x.array.splice(1, Math.ceil(d) - 1);
1754
+ x.array[0] = a;
1755
+ if (Number.isInteger(d)) x.array[1][1] += c;
1756
+ else x.array.splice(1, 0, [arrows, c, 1, 1]);
1757
+
1758
+ }
1759
+ } else {
1760
+ break;
1761
+ }
1762
+ }
1763
+ a = input.split(/[Ee]/);
1764
+ b = [x.array[0], 0];
1765
+ c = 1;
1766
+ for (let i = a.length - 1; i >= 0; --i) {
1767
+ //The things that are already there
1768
+ if (b[0] < MAX_E && b[1] === 0) {
1769
+ b[0] = Math.pow(10, c * b[0]);
1770
+ } else if (c == -1) {
1771
+ if (b[1] === 0) {
1772
+ b[0] = Math.pow(10, c * b[0]);
1773
+ } else if (b[1] == 1 && b[0] <= Math.log10(Number.MAX_VALUE)) {
1774
+ b[0] = Math.pow(10, c * Math.pow(10, b[0]));
1775
+ } else {
1776
+ b[0] = 0;
1777
+ }
1778
+ b[1] = 0;
1779
+ } else {
1780
+ b[1]++;
1781
+ }
1782
+ //Multiplying coefficient
1783
+ var decimalPointPos = a[i].indexOf(".");
1784
+ var intPartLen = decimalPointPos == -1 ? a[i].length : decimalPointPos;
1785
+ if (b[1] === 0) {
1786
+ if (intPartLen >= LONG_STRING_MIN_LENGTH) b[0] = Math.log10(b[0]) + log10LongString(a[i].substring(0, intPartLen)), b[1] = 1;
1787
+ else if (a[i]) b[0] *= Number(a[i]);
1788
+ } else {
1789
+ d = intPartLen >= LONG_STRING_MIN_LENGTH ? log10LongString(a[i].substring(0, intPartLen)) : a[i] ? Math.log10(Number(a[i])) : 0;
1790
+ if (b[1] == 1) {
1791
+ b[0] += d;
1792
+ } else if (b[1] == 2 && b[0] < MAX_E + Math.log10(d)) {
1793
+ b[0] += Math.log10(1 + Math.pow(10, Math.log10(d) - b[0]));
1794
+ }
1795
+ }
1796
+ //Carrying
1797
+ if (b[0] < MAX_E && b[1]) {
1798
+ b[0] = Math.pow(10, b[0]);
1799
+ b[1]--;
1800
+ } else if (b[0] > MAX_SAFE_INTEGER) {
1801
+ b[0] = Math.log10(b[0]);
1802
+ b[1]++;
1803
+ }
1804
+ }
1805
+ x.array[0] = b[0];
1806
+ if (b[1]) {
1807
+ if (x.array.length >= 2 && x.array[1][0] == 1) x.array[1][1] += b[1];
1808
+ else x.array.splice(1, 0, [1, b[1], 1, 1]);
1809
+ }
1810
+ if (Klayer > 0) {
1811
+ x.array.push(["x", Klayer, 1, 1])
1812
+ }
1813
+ }
1814
+ if (negateIt) x.sign *= -1;
1815
+ x.normalize();
1816
+ C[originalInput] = {}
1817
+ deepCopyProps({
1818
+ array: x.array,
1819
+ layer: x.layer,
1820
+ sign: x.sign
1821
+ }, C[originalInput])
1822
+ return x;
1823
+
1824
+ }
1825
+ }
1826
+ Q.fromArray = function (input) {
1827
+ if (!Array.isArray(input)) {
1828
+ throw Error("[" + powiainaNumError + "] Unexcepted array")
1829
+ }
1830
+ let x = new PowiainaNum(0);
1831
+ x.array = input;
1832
+ return x;
1833
+ }
1834
+ P.toNumber = function () {
1835
+ if (this.sign == -1) return -1 * (this.abs().toNumber());
1836
+ if (this.layer > 0) return Infinity;
1837
+ if (this.array.length >= 2 &&
1838
+ (
1839
+ this.array[1][3] >= 2 ||
1840
+ this.array[1][2] == "x" ||
1841
+ this.array[1][2] >= 2 ||
1842
+ this.array[1][0] == "x" ||
1843
+ this.array[1][0] >= 2 ||
1844
+ this.array[1][1] >= 2 ||
1845
+ this.array[1][1] == 1 && this.array[0][1] > Math.log10(Number.MAX_VALUE)
1846
+ )
1847
+ ) return Infinity;
1848
+ if (this.array.length >= 2 && this.array[1][1] == 1) return Math.pow(10, this.array[0]);
1849
+ return this.array[0];
1850
+ };
1851
+ P.clone = function () {
1852
+ var temp = new PowiainaNum();
1853
+ var array = [];
1854
+ array.push(this.array[0]);
1855
+ for (var i = 1; i < this.array.length; ++i)
1856
+ array.push([this.array[i][0], this.array[i][1], this.array[i][2], this.array[i][3]]);
1857
+
1858
+ temp.array = array;
1859
+ temp.sign = this.sign;
1860
+ temp.layer = this.layer;
1861
+ return temp;
1862
+ };
1863
+
1864
+
1865
+ //#region toGlobalScope
1866
+ function clone(obj) {
1867
+ var i, p, ps;
1868
+ function PowiainaNum(input) { // constructor of this function
1869
+ var x = this;
1870
+ if (!(x instanceof PowiainaNum)) return new PowiainaNum(input);
1871
+ x.constructor = PowiainaNum;
1872
+
1873
+ var temp, temp2, temp3;
1874
+
1875
+ if (typeof input == "number") {
1876
+ temp = PowiainaNum.fromNumber(input);
1877
+ } else if (typeof input == "string") {
1878
+ temp = PowiainaNum.fromString(input);
1879
+ } else if (typeof input == "object" && input instanceof PowiainaNum) {
1880
+ temp = input.clone();
1881
+
1882
+ } else if (typeof input == "object" && Array.isArray(input)) {
1883
+ temp = PowiainaNum.fromArray(input);
1884
+
1885
+ }
1886
+ else {
1887
+ temp = [NaN];
1888
+ temp2 = 1;
1889
+ temp3 = 0;
1890
+ }
1891
+ if (typeof temp2 == "undefined") {
1892
+ x.array = temp.array;
1893
+ x.sign = temp.sign;
1894
+ x.layer = temp.layer;
1895
+ } else {
1896
+ x.array = temp;
1897
+ x.sign = temp2;
1898
+ x.layer = temp3
1899
+ }
1900
+ }
1901
+ PowiainaNum.prototype = P;
1902
+
1903
+ PowiainaNum.JSON = 0;
1904
+ PowiainaNum.STRING = 1;
1905
+
1906
+ PowiainaNum.NONE = 0;
1907
+ PowiainaNum.NORMAL = 1;
1908
+ PowiainaNum.ALL = 2;
1909
+ PowiainaNum.clone = clone;
1910
+ PowiainaNum.config = PowiainaNum.set = config
1911
+ for (var prop in Q) {
1912
+ //set static methods
1913
+ if (Q.hasOwnProperty(prop)) {
1914
+ PowiainaNum[prop] = Q[prop];
1915
+ }
1916
+ }
1917
+ if (obj === void 0) obj = {};
1918
+ if (obj) {
1919
+ ps = ['maxOps', 'serializeMode', 'debug'];
1920
+ for (i = 0; i < ps.length;)
1921
+ if (!obj.hasOwnProperty(p = ps[i++]))
1922
+ obj[p] = this[p];
1923
+ }
1924
+
1925
+ PowiainaNum.config(obj);
1926
+ return PowiainaNum;
1927
+ }
1928
+ function defineConstants(obj) {
1929
+ for (var prop in R) {
1930
+ if (R.hasOwnProperty(prop)) {
1931
+ if (Object.defineProperty) {
1932
+ Object.defineProperty(obj, prop, {
1933
+ configurable: false,
1934
+ enumerable: true,
1935
+ writable: false,
1936
+ value: new PowiainaNum(R[prop])
1937
+ });
1938
+ } else {
1939
+ obj[prop] = new PowiainaNum(R[prop]);
1940
+ }
1941
+ }
1942
+ }
1943
+ return obj;
1944
+ }
1945
+ function config(obj) {
1946
+ if (!obj || typeof obj !== 'object') {
1947
+ throw Error(powiainaNumError + 'Object expected');
1948
+ }
1949
+ var i, p, v,
1950
+ ps = [
1951
+ 'maxOps', 1, Number.MAX_SAFE_INTEGER,
1952
+ 'serializeMode', 0, 1,
1953
+ 'debug', 0, 2
1954
+ ];
1955
+ for (i = 0; i < ps.length; i += 3) {
1956
+ if ((v = obj[p = ps[i]]) !== void 0) {
1957
+ if (Math.floor(v) === v && v >= ps[i + 1] && v <= ps[i + 2]) this[p] = v;
1958
+ else throw Error(invalidArgument + p + ': ' + v);
1959
+ }
1960
+ }
1961
+
1962
+ return this;
1963
+ }
1964
+
1965
+ PowiainaNum = clone(PowiainaNum);
1966
+
1967
+ PowiainaNum = defineConstants(PowiainaNum);
1968
+
1969
+ PowiainaNum.default = PowiainaNum.PowiainaNum = PowiainaNum;
1970
+
1971
+ return PowiainaNum;
1972
+ //#endregion
1973
+
1974
+ })(this);