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