powiaina_num.js 0.1.12 → 0.2.0-alpha.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 DELETED
@@ -1,2041 +0,0 @@
1
- //Code snippets and templates from ExpantaNum.js
2
- //Limit {10, 9e15, 1, 1, 1, 2}
3
-
4
-
5
- ; (function (globalScope) {
6
- "use strict";
7
-
8
- function deepCopyProps(source, target) {
9
- for (let key in source) {
10
- if (source.hasOwnProperty(key)) {
11
- // 如果源对象的属性是对象或数组,则递归复制
12
- if ((typeof source[key] === 'object' && !(source[key] instanceof PowiainaNum)) && source[key] !== null) {
13
- // 如果目标对象没有这个属性,或者属性是null,则创建一个新的
14
- if (!target.hasOwnProperty(key) || target[key] == null || Array.isArray(source[key]) !== Array.isArray(target[key])) {
15
- target[key] = Array.isArray(source[key]) ? [] : {};
16
- }
17
- // 递归复制属性
18
- deepCopyProps(source[key], target[key]);
19
- } else {
20
- // 如果属性不是对象或数组,则直接复制
21
- target[key] = source[key];
22
- }
23
- }
24
- }
25
- }
26
-
27
- var PowiainaNum = {
28
- maxOps: 10,
29
-
30
- // Specify what format is used when serializing for JSON.stringify
31
- //
32
- // JSON 0 JSON object
33
- // STRING 1 String
34
- serializeMode: 1,
35
-
36
- // Deprecated
37
- // Level of debug information printed in console
38
- //
39
- // NONE 0 Show no information.
40
- // NORMAL 1 Show operations.
41
- // ALL 2 Show everything.
42
- debug: 0,
43
-
44
-
45
- },
46
- external = true,
47
-
48
- powiainaNumError = "[PowiainaNumError] ",
49
- invalidArgument = powiainaNumError + "Invalid argument: ",
50
-
51
-
52
- MAX_SAFE_INTEGER = 9007199254740991,
53
- MAX_E = Math.log10(MAX_SAFE_INTEGER), //15.954589770191003
54
-
55
- //PowiainaNum string cache object
56
- C = {
57
- "0" : {array: [0], layer: 0, sign: 0},
58
- "1" : {array: [1], layer: 0, sign: 1},
59
- "2" : {array: [2], layer: 0, sign: 1},
60
- "3" : {array: [3], layer: 0, sign: 1},
61
- "4" : {array: [4], layer: 0, sign: 1},
62
- "6" : {array: [6], layer: 0, sign: 1},
63
- "7" : {array: [7], layer: 0, sign: 1},
64
- "8" : {array: [8], layer: 0, sign: 1},
65
- "9" : {array: [9], layer: 0, sign: 1},
66
- "10" : {array: [10], layer: 0, sign: 0},
67
- },
68
- //PowiainaNum.prototype object
69
- P = {},
70
-
71
- //PowiainaNum static object
72
- Q = {},
73
-
74
- R = {};
75
-
76
- //#region PowiainaNum constants
77
- R.ZERO = 0;
78
- R.ONE = 1;
79
- R.NaN = NaN;
80
-
81
- R.E = Math.E;
82
- R.LN2 = Math.LN2;
83
- R.LN10 = Math.LN10;
84
- R.LOG2E = Math.LOG2E;
85
- R.LOG10E = Math.LOG10E;
86
- R.PI = Math.PI;
87
- R.SQRT1_2 = Math.SQRT1_2;
88
- R.SQRT2 = Math.SQRT2;
89
- R.MAX_SAFE_INTEGER = MAX_SAFE_INTEGER;
90
- R.MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER;
91
- R.NEGATIVE_INFINITY = Number.NEGATIVE_INFINITY;
92
- R.POSITIVE_INFINITY = Number.POSITIVE_INFINITY;
93
- R.E_MAX_SAFE_INTEGER = "l0 s1 a[" + MAX_SAFE_INTEGER + ",[1,1,1,1]]";
94
- R.EE_MAX_SAFE_INTEGER = "l0 s1 a[" + MAX_SAFE_INTEGER + ",[1,2,1,1]]";
95
- 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 + "]]";
96
- R.TETRATED_MAX_SAFE_INTEGER = "l0 s1 a[10000000000,[1," + (MAX_SAFE_INTEGER - 1).toString() + ",1,1]]";
97
- R.PENTATED_MAX_SAFE_INTEGER = "l0 s1 a[10000000000,[2," + (MAX_SAFE_INTEGER - 1).toString() + ",1,1]]";
98
- R.MULTIEXPANSIONED_MAX_SAFE_INTEGER = "l0 s1 a[10000000000,[1," + (MAX_SAFE_INTEGER - 1).toString() + ",2,1]]";
99
- R.POWEREXPANSIONED_MAX_SAFE_INTEGER = "l0 s1 a[10000000000,[2," + (MAX_SAFE_INTEGER - 1).toString() + ",2,1]]";
100
- R.GRAHAMS_NUMBER = "l0 s1 a[3638334640023.7783,[1,7625597484984,1,1],[3,1,1,1],[\"x\",63,1,1]]"
101
-
102
- // sample, Throotriadekol == {100, 100, 100, 99, 12}
103
- // At there 100 => 10, {10, 10, 10, 99, 12}
104
- R.THROOTRIADEKOL = "l0 s1 a [10,[10,1,99,12]]"
105
- //#endregion
106
-
107
- /*
108
- isPowiainaNum is a regex to judge string is a valid PowiainaNum string source.
109
- */
110
- 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+))$/;
111
- 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+)?\])*\]$/;
112
- //#region calculating
113
-
114
-
115
- //#region four basic calculating
116
- //#region plus
117
- P.plus = P.add = function (other) {
118
- var x = this.clone();
119
- other = new PowiainaNum(other);
120
- if (PowiainaNum.debug >= PowiainaNum.NORMAL) {
121
- console.log(this, "calculate add", other);
122
- }
123
- if (x.sign == -1) return x.neg().add(other.neg()).neg();
124
- if (other.sign == -1) return x.sub(other.neg());
125
-
126
- if (x.eq(PowiainaNum.ZERO)) return other; // 0+x=x
127
- if (other.eq(PowiainaNum.ZERO)) return x; // x+0=x
128
- if (x.isNaN() || other.isNaN() || x.isInfinite() && other.isInfinite() && x.eq(other.neg())) return PowiainaNum.NaN.clone();
129
- if (x.isInfinite()) return x;
130
- if (other.isInfinite()) return other;
131
-
132
- var p = x.min(other); // who is smallest
133
- var q = x.max(other); // who is biggest
134
- var op0 = q.operatorE(0);
135
- var op1 = q.operatorE(1);
136
-
137
- var t;
138
-
139
- if (q.gt(PowiainaNum.E_MAX_SAFE_INTEGER) || q.div(p).gt(PowiainaNum.MAX_SAFE_INTEGER)) {
140
- t = q; // q>e9e15, plus calculating is 'meaningless', q/p>9e15, q+p = q because floating loss,
141
- } else if (!op1) {
142
- // thereis not 10^ signs, add directly
143
- t = new PowiainaNum(x.toNumber() + other.toNumber());
144
- } else if (op1 == 1) {
145
- var a = p.operator(1) ? p.operator(0) : Math.log10(p.operator(0)); // a is a variable: if has 10^ operator, use x from 10^x, else use log10(x)
146
- // a is p's log10
147
-
148
- // It calculates the final results of log10
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 PowiainaNum.div(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.multiExpansion = Q.mulEps = function (x, other) {
705
- return PowiainaNum(x).multiExpansion(other);
706
- }
707
- P.powerExpansion = P.powEps = function (other) {
708
- var t = this.clone();
709
- other = new PowiainaNum(other);
710
- var r;
711
- if (PowiainaNum.debug >= PowiainaNum.NORMAL) console.log("{" + t + "," + other + ",3,2}");
712
- if (other.lte(PowiainaNum.ZERO) || !other.isint()) return PowiainaNum.NaN.clone();
713
- if (other.eq(PowiainaNum.ONE)) return t.clone();
714
- if (!t.isint()) return PowiainaNum.NaN.clone();
715
- if (t.eq(2)) return new PowiainaNum(4);
716
- if (t.neq(10)) throw Error(powiainaNumError + "I can't handle that base is not 1, 2, or10")
717
- if (other.gt(PowiainaNum.MAX_SAFE_INTEGER)) {
718
- r = PowiainaNum()
719
-
720
- r.array = []
721
- r.array.push(...other.array);
722
- r.array.push([3, 1, 2, 1]);
723
-
724
- } else {
725
- r = PowiainaNum()
726
-
727
- r.array = [10]
728
- r.array.push([2, other.toNumber() - 1, 2, 1]);
729
-
730
- }
731
- return r.normalize();
732
- }
733
- Q.powerExpansion = Q.powEps = function (x, other) {
734
- return PowiainaNum(x).powerExpansion(other);
735
- }
736
- Q.expansionArrow = Q.epsArrow = function (x, arrow) {
737
- var other = new PowiainaNum(x)
738
- var arrow = new PowiainaNum(arrow)
739
- if (other.lte(PowiainaNum.ZERO) || !other.isint()) return PowiainaNum.NaN.clone();
740
- if (!arrow.isint() || arrow.lt(1)) return PowiainaNum.NaN.clone();
741
- if (other.eq(PowiainaNum.ONE)) return t.clone();
742
- var r;
743
- if (arrow.lt(MAX_SAFE_INTEGER)) {
744
- if (other.gt("l0 s1 a[10000000000,[" + (arrow.toNumber().toString()) + "," + (MAX_SAFE_INTEGER - 1).toString() + ",2,1]]")) {
745
- return other.clone();
746
- }
747
- else if (other.gt(PowiainaNum.MAX_SAFE_INTEGER)) {
748
- r = PowiainaNum()
749
-
750
- r.array = []
751
- r.array.push(...other.array);
752
- r.array.push([arrow.toNumber(), 1, 2, 1]);
753
-
754
- } else {
755
- r = PowiainaNum()
756
-
757
- r.array = [10]
758
- r.array.push([arrow.toNumber() - 1, other.toNumber() - 1, 2, 1]);
759
-
760
- }
761
- } else {
762
- r = PowiainaNum(arrow)
763
- r.array.push(["x", 1, 2, 1]);
764
- }
765
- return r.normalize();
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
- Q.explosionArrow = Q.elsArrow = function (x, arrow) {
802
- var other = new PowiainaNum(x)
803
- var arrow = new PowiainaNum(arrow)
804
- if (other.lte(PowiainaNum.ZERO) || !other.isint()) return PowiainaNum.NaN.clone();
805
- if (!arrow.isint() || arrow.lt(1)) return PowiainaNum.NaN.clone();
806
- if (other.eq(PowiainaNum.ONE)) return t.clone();
807
- var r;
808
- if (arrow.lt(MAX_SAFE_INTEGER)) {
809
- if (other.gt("l0 s1 a[10000000000,[" + (arrow.toNumber().toString()) + "," + (MAX_SAFE_INTEGER - 1).toString() + ",3,1]]")) {
810
- return other.clone();
811
- }
812
- else if (other.gt(PowiainaNum.MAX_SAFE_INTEGER)) {
813
- r = PowiainaNum()
814
-
815
- r.array = []
816
- r.array.push(...other.array);
817
- r.array.push([arrow.toNumber(), 1, 3, 1]);
818
-
819
- } else {
820
- r = PowiainaNum()
821
-
822
- r.array = [10]
823
- r.array.push([arrow.toNumber() - 1, other.toNumber() - 1, 3, 1]);
824
-
825
- }
826
- } else {
827
- r = PowiainaNum(arrow)
828
- r.array.push(["x", 1, 3, 1]);
829
- }
830
- return r.normalize();
831
- }
832
-
833
- //#endregion
834
- //#endregion
835
-
836
- //#region arrow, pentate
837
- P.pent = P.pentate = function (other) {
838
- return this.arrow(3)(other);
839
- }
840
- Q.pent = Q.pentate = function (x, other) {
841
- return PowiainaNum.arrow(x, 3, other);
842
- }
843
-
844
-
845
- P.overflow = function(start, power, meta = 1) {
846
- var number = this.clone()
847
- var power1 = new PowiainaNum(power)
848
- var meta1 = new PowiainaNum(meta)
849
- var start1 = new PowiainaNum(start)
850
- if(number.gte(start1)) {
851
- var s = start1.iteratedlog(10, meta1)
852
- number = E(10).iteratedexp(meta1, number.iteratedlog(10, meta1).div(s).pow(power1).mul(s));
853
- }
854
- return number
855
- }
856
-
857
- /**
858
- *
859
- * @param {Number|PowiainaNum|String} other arrow count
860
- * @returns {Function} a function can be called(x), this function returns {this,other,x}
861
- */
862
- P.arrow = function (other) {
863
- var t = this.clone();
864
- var arrows = new PowiainaNum(other);
865
- if (!arrows.isint() || arrows.lt(PowiainaNum.ZERO)) return function (other) {
866
- return PowiainaNum.NaN.clone();
867
- };
868
- if (arrows.eq(PowiainaNum.ZERO)) return function (other) {
869
- return t.mul(other);
870
- };
871
- if (arrows.eq(PowiainaNum.ONE)) return function (other) {
872
- return t.pow(other);
873
- };
874
- if (arrows.eq(2)) return function (other) {
875
- return t.tetr(other);
876
- };
877
- return function (other) {
878
- var depth;
879
- if (arguments.length == 2) depth = arguments[1]; //must hide
880
- else depth = 0;
881
- other = new PowiainaNum(other);
882
- var r;
883
- if (PowiainaNum.debug >= PowiainaNum.NORMAL) console.log(t + "{" + arrows + "}" + other);
884
- if (t.isNaN() || other.isNaN()) return PowiainaNum.NaN.clone();
885
- if (other.lt(PowiainaNum.ZERO)) return PowiainaNum.NaN.clone();
886
- if (t.eq(PowiainaNum.ZERO)) {
887
- if (other.eq(PowiainaNum.ONE)) return PowiainaNum.ZERO.clone();
888
- return PowiainaNum.NaN.clone();
889
- }
890
- if (t.eq(PowiainaNum.ONE)) return PowiainaNum.ONE.clone();
891
- if (other.eq(PowiainaNum.ZERO)) return PowiainaNum.ONE.clone();
892
- if (other.eq(PowiainaNum.ONE)) return t.clone();
893
-
894
- // arrow > 9e15, that using 10{x}, x=arrow;
895
- if (arrows.gt(PowiainaNum.MAX_SAFE_INTEGER)) {
896
- r = PowiainaNum(arrows);
897
- r.array.push(["x", 1, 1, 1]);
898
- r.normalize();
899
- return r;
900
- }
901
-
902
- let arrowsNum = arrows.toNumber();
903
- // arrow < 9e15
904
-
905
- // 10{x}2 = 10{x-1}10
906
- if (other.eq(2)) return t.arrow(arrowsNum - 1)(t, depth + 1);
907
-
908
- // 我不到啊
909
- if (t.max(other).gt("10{" + (arrowsNum + 1) + "}" + MAX_SAFE_INTEGER)) return t.max(other);
910
-
911
- // this = 10{1919810}1e16 > 10{1919810}9.007e15
912
- // or t{arrow}other, other>9.007e15
913
- if (t.gt("10{" + arrowsNum + "}" + MAX_SAFE_INTEGER) || other.gt(PowiainaNum.MAX_SAFE_INTEGER)) {
914
-
915
- // this = 10{1919810}1e16 > 10{1919810}9.007e15
916
- if (t.gt("10{" + arrowsNum + "}" + MAX_SAFE_INTEGER)) {
917
- r = t.clone();
918
- r.operatorE(arrowsNum, r.operatorE(arrowsNum) - 1);
919
- r.normalize();
920
- } else if (t.gt("10{" + (arrowsNum - 1) + "}" + MAX_SAFE_INTEGER)) {
921
- r = new PowiainaNum(t.operatorE(arrowsNum - 1));
922
- } else {
923
- r = PowiainaNum.ZERO;
924
- }
925
- var j = r.add(other);
926
- j.operatorE(arrowsNum, (j.operatorE(arrowsNum) || 0) + 1);
927
- j.normalize();
928
- return j;
929
- }
930
- if (depth >= PowiainaNum.maxOps + 40) {
931
- r = PowiainaNum(10);
932
- r.array = [10, [arrowsNum, 1, 1, 1]];
933
- return r;
934
- }
935
- var y = other.toNumber();
936
- var f = Math.floor(y);
937
- var arrows_m1 = arrows.sub(PowiainaNum.ONE);
938
- r = t.arrow(arrows_m1)(y - f, depth + 1);
939
- for (var i = 0,
940
- m = new PowiainaNum("10{" + (arrowsNum - 1) + "}" + MAX_SAFE_INTEGER);
941
- f !== 0 && r.lt(m) && i < 100;
942
- ++i) {
943
- if (f > 0) {
944
- r = t.arrow(arrows_m1)(r, depth + 1);
945
- --f;
946
- }
947
- }
948
- if (i == 100) f = 0;
949
- r.operatorE(arrowsNum - 1, (r.operatorE(arrowsNum - 1) + f) || f);
950
- r.normalize();
951
- return r;
952
- }
953
- }
954
- Q.arrow = function (x, z, y) {
955
- return new PowiainaNum(x).arrow(z)(y);
956
- };
957
- P.add1J = function () {
958
- return PowiainaNum.chain(10, 10, this);
959
- }
960
-
961
- P.chain = function (other, arrows) {
962
- return this.arrow(arrows)(other);
963
- };
964
- Q.chain = function (x, y, z) {
965
- return new PowiainaNum(x).arrow(z)(y);
966
- };
967
- Q.hyper = function (z) {
968
- z = new PowiainaNum(z);
969
- 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); };
970
- if (z.eq(PowiainaNum.ONE)) return function (x, y) { return PowiainaNum.add(x, y); };
971
- return function (x, y) { return new PowiainaNum(x).arrow(z.sub(2))(y); };
972
- };
973
- //#endregion
974
-
975
- //#endregion
976
-
977
- //#region compareTo
978
- P.compareTo = P.cmp = function (other) {
979
- if (PowiainaNum.debug > PowiainaNum.NORMAL) console.log("cmpare", this, other)
980
- if (!(other instanceof PowiainaNum)) other = new PowiainaNum(other);
981
- if (isNaN(this.array[0]) || isNaN(other.array[0])) return NaN
982
- if (this.array[0] == Infinity && other.array[0] != Infinity) return this.sign;
983
- if (this.array[0] != Infinity && other.array[0] == Infinity) return -other.sign;
984
- if (this.sign != other.sign) return this.sign;
985
- if (this.array.length == 1 && other.array.length == 1 && this.array[0] == other.array[0]) return 0;
986
- if (other.array[0] == 0) {
987
- if (this.sign > 0) {
988
- return 1
989
- } else if (this.sign < 0) {
990
- return -1
991
- }
992
- }
993
- var m = this.sign;
994
- var r;
995
- if (this.layer > other.layer) r = 1
996
- else if (this.layer < other.layer) r = -1;
997
- else {
998
- var e, f;
999
- var i = 1;
1000
- var l = Math.min(this.array.length, other.array.length);
1001
-
1002
-
1003
- do {
1004
- var g = this.array[this.array.length - i];
1005
- var h = other.array[other.array.length - i];
1006
- if (typeof g == "number") e = [0, g, 1, 1];
1007
- else e = g;
1008
- if (typeof h == "number") f = [0, h, 1, 1];
1009
- else f = h;
1010
- if (e[3] > f[3] || (e[3] == f[3] && (
1011
- (e[2] == "x" && f[2] != "x") || e[2] > f[2] || (e[2] == f[2] && (
1012
- (e[0] == "x" && f[0] != "x") || e[0] > f[0] || (e[0] == f[0] && (
1013
- e[1] > f[1]
1014
- ))
1015
- ))
1016
- ))) { r = 1; break; }
1017
- if (e[3] < f[3] || (e[3] == f[3] && (
1018
- (e[2] != "x" && f[2] == "x") || e[2] < f[2] || (e[2] == f[2] && (
1019
- (e[0] != "x" && f[0] == "x") || e[0] < f[0] || (e[0] == f[0] && (
1020
- e[1] < f[1]
1021
- ))
1022
- ))
1023
- ))) { r = -1; break; }
1024
- i++
1025
- } while (i < l)
1026
- if (r === undefined) {
1027
- if (this.array.length == other.array.length) {
1028
- if (this.array[0] > other.array[0]) {
1029
- r = 1
1030
- } else if (this.array[0] < other.array[0]) {
1031
- r = -1
1032
- } else {
1033
-
1034
- r = 0;
1035
- }
1036
- } else if (this.array.length > other.array.length) {
1037
- e = this.array[this.array.length - l];
1038
- if (e[0] >= 1 || e[1] > 10) {
1039
- r = 1;
1040
- } else {
1041
- r = -1;
1042
- }
1043
- } else {
1044
- e = other.array[other.array.length - l];
1045
- if (e[0] >= 1 || e[1] > 10) {
1046
- r = -1;
1047
- } else {
1048
- r = 1;
1049
- }
1050
- }
1051
- }
1052
- }
1053
- return r * m;
1054
- }
1055
-
1056
- Q.compare = Q.cmp = function (x, y) {
1057
- return new PowiainaNum(x).cmp(y);
1058
- };
1059
- //#endregion
1060
-
1061
- //#region floorceil
1062
- P.isInteger = P.isint = function () {
1063
- if (this.sign == -1) return this.abs().isint();
1064
- if (this.gt(PowiainaNum.MAX_SAFE_INTEGER)) return true;
1065
- return Number.isInteger(this.toNumber());
1066
- };
1067
- Q.isInteger = Q.isint = function (x) {
1068
- return new PowiainaNum(x).isint();
1069
- };
1070
- P.floor = function () {
1071
- if (this.isInteger()) return this.clone();
1072
- return new PowiainaNum(Math.floor(this.toNumber()));
1073
- };
1074
- Q.floor = function (x) {
1075
- return new PowiainaNum(x).floor();
1076
- };
1077
- P.ceiling = P.ceil = function () {
1078
- if (this.isInteger()) return this.clone();
1079
- return new PowiainaNum(Math.ceil(this.toNumber()));
1080
- };
1081
- Q.ceiling = Q.ceil = function (x) {
1082
- return new PowiainaNum(x).ceil();
1083
- };
1084
- P.round = function () {
1085
- if (this.isInteger()) return this.clone();
1086
- return new PowiainaNum(Math.round(this.toNumber()));
1087
- };
1088
- Q.round = function (x) {
1089
- return new PowiainaNum(x).round();
1090
- };
1091
- //#endregion
1092
-
1093
- //#region from ExpantaNum.js comparing
1094
-
1095
- P.greaterThan = P.gt = function (other) {
1096
- return this.cmp(other) > 0;
1097
- };
1098
- Q.greaterThan = Q.gt = function (x, y) {
1099
- return new PowiainaNum(x).gt(y);
1100
- };
1101
- P.greaterThanOrEqualTo = P.gte = function (other) {
1102
- return this.cmp(other) >= 0;
1103
- };
1104
- Q.greaterThanOrEqualTo = Q.gte = function (x, y) {
1105
- return new PowiainaNum(x).gte(y);
1106
- };
1107
- P.lessThan = P.lt = function (other) {
1108
- return this.cmp(other) < 0;
1109
- };
1110
- Q.lessThan = Q.lt = function (x, y) {
1111
- return new PowiainaNum(x).lt(y);
1112
- };
1113
- P.lessThanOrEqualTo = P.lte = function (other) {
1114
- return this.cmp(other) <= 0;
1115
- };
1116
- Q.lessThanOrEqualTo = Q.lte = function (x, y) {
1117
- return new PowiainaNum(x).lte(y);
1118
- };
1119
- P.equalsTo = P.equal = P.eq = function (other) {
1120
- return this.cmp(other) === 0;
1121
- };
1122
- Q.equalsTo = Q.equal = Q.eq = function (x, y) {
1123
- return new PowiainaNum(x).eq(y);
1124
- };
1125
- P.notEqualsTo = P.notEqual = P.neq = function (other) {
1126
- return this.cmp(other) !== 0;
1127
- };
1128
- Q.notEqualsTo = Q.notEqual = Q.neq = function (x, y) {
1129
- return new PowiainaNum(x).neq(y);
1130
- };
1131
- P.minimum = P.min = function (other) {
1132
- return this.lt(other) ? this.clone() : new PowiainaNum(other);
1133
- };
1134
- Q.minimum = Q.min = function (x, y) {
1135
- return new PowiainaNum(x).min(y);
1136
- };
1137
- P.maximum = P.max = function (other) {
1138
- return this.gt(other) ? this.clone() : new PowiainaNum(other);
1139
- };
1140
- Q.maximum = Q.max = function (x, y) {
1141
- return new PowiainaNum(x).max(y);
1142
- };
1143
- P.isPositive = P.ispos = function () {
1144
- return this.gt(PowiainaNum.ZERO);
1145
- };
1146
- Q.isPositive = Q.ispos = function (x) {
1147
- return new PowiainaNum(x).ispos();
1148
- };
1149
- P.isNegative = P.isneg = function () {
1150
- return this.lt(PowiainaNum.ZERO);
1151
- };
1152
- Q.isNegative = Q.isneg = function (x) {
1153
- return new PowiainaNum(x).isneg();
1154
- };
1155
-
1156
- //#endregion
1157
-
1158
- //#region from ExpantaNum.js operator
1159
- P.getOperatorIndex = function (i, j = 1, k = 1) {
1160
- /**
1161
-
1162
- PS:
1163
- in ExpantaNum.js, P.operator need only 1 parameter (arrow count?)
1164
-
1165
- but in PowiainaNum.js, P.operator need 3 parameters
1166
- (arrowcount, {{}}brace count(1), [[]]brace count(1))
1167
-
1168
- such as 10{1145141919810}229028, in ExpantaNum.toString,
1169
- it equals to (10{1145141919809})^229026 (10{1145141919808})^8 (10{1145141919807})^8 ... (10{1145141918813})^8 (10{1145141918812})^8 10
1170
- so ExpantaNum("10{1145141919810}229028").operator(1145141919809) equals 229026;
1171
-
1172
- that P.operator(i,j,k) returns the repeat count of ({10,x,i,j,k}, x= ...)^repeatcount has
1173
-
1174
-
1175
-
1176
- We back to the method P.getOperatorIndex;
1177
-
1178
- sometime it returns NON-INTEGER(such as 4.5, -0.5), 4.5 means this number's max arrows used.
1179
- (but i don't know how -0.5 means.)
1180
-
1181
- such as a = ExpantaNum("10{5}100")
1182
- a.toString() = (10^^^^)^98 (10^^^)^8 (10^^)^8 (10^)^8 10000000000
1183
-
1184
- a.getOperatorIndex(2) = 2
1185
- so a.array[a.getOperatorIndex(2)] == [2,8]
1186
-
1187
- such^2 as b = ExpantaNum("(10{6})^5 (10^^^)^5 10")
1188
- b.toString() = (10{6})^5 (10^^^)^4 (10^^)^8 (10^)^8 10000000000;
1189
-
1190
- b.getOperatorIndex(6) = 4
1191
- so b.array[b.getOperatorIndex(6)] == [6,5]
1192
- */
1193
- if (i != "x" && typeof i != "number") i = Number(i);
1194
- if (i != "x" && !isFinite(i)) throw Error(invalidArgument + "Index i out of range.");
1195
- if (j != "x" && typeof j != "number") j = Number(j);
1196
- if (j != "x" && !isFinite(j)) throw Error(invalidArgument + "Index j out of range.");
1197
- if (typeof k != "number") k = Number(k);
1198
- if (!isFinite(k)) throw Error(invalidArgument + "Index k out of range.");
1199
- var a = this.array;
1200
- var min = 0, max = a.length - 1;
1201
- if (a.length == 1) {
1202
- if (0 > i) return -0.5
1203
- if (0 == i) return 0
1204
- if (0 < i) return 0.5
1205
- }
1206
- if (a[max][3] < k) return max + 0.5
1207
- if (a[max][3] > k) return -0.5
1208
- if (a[max][2] != "x" && j == "x") return max + 0.5
1209
- if (a[max][2] == "x" && j != "x") return -0.5
1210
- if (a[max][0] < i) return max + 0.5
1211
- if (a.length > 1 && 0 > i) return -0.5
1212
- else if (0 > i) return -0.5
1213
-
1214
- var repeatcount = 500
1215
- while (min != max) {
1216
- if (a[min][3] == k && a[min][2] == j && a[min][0] == i) return min;
1217
- if (a[max][3] == k && a[max][2] == j && a[max][0] == i) return max;
1218
-
1219
- var mid = Math.floor((min + max) / 2);
1220
-
1221
- if (min == mid || (a[mid][0] == i && a[mid][2] == j && a[mid][3] == k)) {
1222
- min = mid;
1223
- break;
1224
- }
1225
- if (a[mid][3] < k || a[mid][2] < j || a[mid][0] < i) min = mid;
1226
- if (a[mid][3] > k || a[mid][2] > j || a[mid][0] > i) max = mid;
1227
- if (--repeatcount <= 0) break;
1228
- }
1229
- if (min == 0 && i == 0) {
1230
- return min;
1231
- } else {
1232
- return a[min][0] == i ? min : min + 0.5;
1233
- }
1234
- }
1235
- P.getOperator = function (i, j = 1, k = 1) {
1236
- if (i != "x" && typeof i != "number") i = Number(i);
1237
- if (i != "x" && !isFinite(i)) throw Error(invalidArgument + "Index i out of range.");
1238
- if (j != "x" && typeof j != "number") j = Number(j);
1239
- if (j != "x" && !isFinite(j)) throw Error(invalidArgument + "Index j out of range.");
1240
- if (typeof k != "number") k = Number(k);
1241
- if (!isFinite(k)) throw Error(invalidArgument + "Index k out of range.");
1242
- var ai = this.getOperatorIndex(i, j, k);
1243
- if (Number.isInteger(ai) && ai != 0) return this.array[ai][1];
1244
- else if (ai == 0) return this.array[0]
1245
- else return i === 0 ? 10 : 0;
1246
-
1247
- }
1248
- P.setOperator = function (i, j = 1, k = 1, value) {
1249
- if (i != "x" && typeof i != "number") i = Number(i);
1250
- if (i != "x" && !isFinite(i)) throw Error(invalidArgument + "Index i out of range.");
1251
- if (j != "x" && typeof j != "number") j = Number(j);
1252
- if (j != "x" && !isFinite(j)) throw Error(invalidArgument + "Index j out of range.");
1253
- if (typeof k != "number") k = Number(k);
1254
- if (!isFinite(k)) throw Error(invalidArgument + "Index k out of range.");
1255
- var ai = this.getOperatorIndex(i, j, k);
1256
- if (Number.isInteger(ai)) this.array[ai][1] = value;
1257
- else {
1258
- ai = Math.ceil(ai);
1259
- this.array.splice(ai, 0, [i, value]);
1260
- }
1261
- this.normalize();
1262
- };
1263
- P.operator = function (i, j = 1, k = 1, value) {
1264
-
1265
- if (i != "x" && typeof i != "number") i = Number(i);
1266
- if (i != "x" && !isFinite(i)) throw Error(invalidArgument + "Index i out of range.");
1267
- if (j != "x" && typeof j != "number") j = Number(j);
1268
- if (j != "x" && !isFinite(j)) throw Error(invalidArgument + "Index j out of range.");
1269
- if (typeof k != "number") k = Number(k);
1270
- if (!isFinite(k)) throw Error(invalidArgument + "Index k out of range.");
1271
-
1272
- if (value === undefined) return this.getOperator(i, j, k);
1273
- else this.setOperator(i, j, k, value)
1274
- }
1275
-
1276
- P.operatorE = function (i, value) {
1277
-
1278
- if (i != "x" && typeof i != "number") i = Number(i);
1279
- if (i != "x" && !isFinite(i)) throw Error(invalidArgument + "Index i out of range.");
1280
-
1281
- if (value === undefined) return this.getOperator(i, 1, 1);
1282
- else this.setOperator(i, 1, 1, value)
1283
-
1284
- }
1285
- //#endregion
1286
- //#region operator
1287
- P.getMaxFirstOperatorIndex = function (j = 1, k = 1) {
1288
- // returns [x,anything,j,k] that x is max.
1289
- for (let i = this.array.length - 1; i >= 1; i--) {
1290
- if (this.array[i][2] == j && this.array[i][3] == k) {
1291
- return i;
1292
- }
1293
- }
1294
- // can not find this operator with [x,anything,j,k]
1295
- return -1
1296
- }
1297
- //#endregion
1298
- //#region isFinite isInfinite isNaN
1299
-
1300
-
1301
- P.isFinite = function () {
1302
- return isFinite(this.array[0]);
1303
- };
1304
- Q.isFinite = function (x) {
1305
- return new PowiainaNum(x).isFinite();
1306
- };
1307
- P.isInfinite = function () {
1308
- return this.array[0] == Infinity;
1309
- };
1310
- Q.isInfinite = function (x) {
1311
- return new PowiainaNum(x).isInfinite();
1312
- };
1313
- P.isNaN = function () {
1314
- return isNaN(this.array[0])
1315
- }
1316
- Q.isNaN = function (x) {
1317
- return new PowiainaNum(x).isNaN();
1318
- }
1319
- //#endregion
1320
-
1321
- //#region absoluteValue negative
1322
- P.absoluteValue = P.abs = function () {
1323
- var x = this.clone();
1324
- x.sign = 1;
1325
- return x;
1326
- };
1327
- Q.absoluteValue = Q.abs = function (x) {
1328
- return new PowiainaNum(x).abs();
1329
- };
1330
- P.negate = P.neg = function () {
1331
- var x = this.clone();
1332
- x.sign = x.sign * -1;
1333
- return x;
1334
- };
1335
- Q.negate = Q.neg = function (x) {
1336
- return new PowiainaNum(x).neg();
1337
- };
1338
- //#endregion
1339
-
1340
-
1341
-
1342
- P.normalize = function () {
1343
- var b;
1344
- var x = this;
1345
- if (PowiainaNum.debug >= PowiainaNum.ALL) console.log(x.toString())
1346
- if (!x.array.length) {
1347
- x.array = [0]
1348
- }
1349
- if (x.array[0] == Infinity) {
1350
- x.array = [Infinity];
1351
- return x;
1352
- }
1353
- if (x.sign != 1 && x.sign != -1) {
1354
- if (typeof x.sign != "number") x.sign = Number(x.sign);
1355
- x.sign = x.sign < 0 ? -1 : 1;
1356
- }
1357
- if (x.layer > MAX_SAFE_INTEGER) {
1358
- x.array = [Infinity]
1359
- x.layer = 0;
1360
- return x;
1361
- }
1362
- if (Number.isInteger(x.layer)) x.layer = Math.floor(x.layer);
1363
- var maxWhileTime = 500;
1364
- var whileTimeRuns = 0
1365
- for (var i = 1; i < x.array.length; ++i) {
1366
- var e = x.array[i]
1367
- if (e[0] === null || e[0] === undefined) {
1368
- e[0] = 0
1369
- }
1370
- if (e[2] === null || e[3] === undefined) {
1371
- e[2] = 1
1372
- }
1373
- if (e[3] === null || e[3] === undefined) {
1374
- e[3] = 1
1375
- }
1376
- if ((isNaN(e[0]) && e[0] != "x") || isNaN(e[1]) || (isNaN(e[2]) && e[2] != "x") || isNaN(e[3])) {
1377
- x.array = [NaN];
1378
- return x;
1379
- }
1380
- if ((!isFinite(e[0]) && e[0] != "x") || !isFinite(e[1]) || (!isFinite(e[2]) && e[2] != "x") || !isFinite(e[2])) {
1381
- x.array = [Infinity];
1382
- return x;
1383
- }
1384
- if (e[0] != "x" && !Number.isInteger(e[0])) e[0] = Math.floor(e[0]);
1385
- if (!Number.isInteger(e[1])) e[1] = Math.floor(e[1]);
1386
- if (e[2] != "x" && !Number.isInteger(e[2])) e[1] = Math.floor(e[2]);
1387
- if (!Number.isInteger(e[3])) e[1] = Math.floor(e[3]);
1388
-
1389
- }
1390
- do {
1391
- if (PowiainaNum.debug >= PowiainaNum.ALL) console.log(x.toString());
1392
- b = false;
1393
-
1394
- // sort array
1395
- x.array.sort(function (a, b) {
1396
- return (function (a, b) {
1397
- if (typeof a == 'number') {
1398
- return 1
1399
- } else if (a[3] > b[3]) {
1400
- return 1
1401
- } else if (a[3] < b[3]) {
1402
- return -1
1403
- }
1404
- else if (a[2] == "x" && b[2] != "x") {
1405
- return 1
1406
- } else if (a[2] != "x" && b[2] == "x") {
1407
- return -1
1408
- } else if (a[2] == "x" && b[2] == "x") {
1409
- return 1
1410
- } else if (a[2] > b[2]) {
1411
- return 1
1412
- } else if (a[2] < b[2]) {
1413
- return -1
1414
- } else if (a[0] == "x" && b[0] != "x") {
1415
- return 1
1416
- } else if (a[0] != "x" && b[0] == "x") {
1417
- return -1
1418
- } else if (a[0] == "x" && b[0] == "x") {
1419
- return 1
1420
- }
1421
- else if (a[0] > b[0]) {
1422
- return 1
1423
- }
1424
- else if (a[0] < b[0]) {
1425
- return -1
1426
- } else if (a[1] > b[1]) {
1427
- return 1
1428
- } else if (a[1] < b[1]) {
1429
- return -1
1430
- }
1431
-
1432
-
1433
- return 1
1434
- })(a, b)
1435
- })
1436
- for (i = 1; i < x.array.length; ++i) {
1437
- // check 0 repeat count
1438
- if (x.array[i][0] !== 0 && (x.array[i][1] === 0 || x.array[i][1] === null || x.array[i][1] === undefined)) {
1439
- x.array.splice(i, 1);
1440
- --i;
1441
- continue;
1442
- }
1443
- // check arrow 0 and brace count >=2
1444
- if (x.array[i][0] == 0 && x.array[i][2] >= 2) {
1445
- x.array[i][0] = "x"
1446
- x.array[i][2] = x.array[i][2] - 1
1447
- }
1448
-
1449
- }
1450
-
1451
- if (!x.array.length) x.array = [0]; // if no array set zero
1452
- if (x.array.length > PowiainaNum.maxOps) x.array.splice(1, x.array.length - PowiainaNum.maxOps); // max operators check
1453
-
1454
- if (x.array[0] > MAX_SAFE_INTEGER) { // check >9e15 first number
1455
- if (x.array.length >= 2 && x.array[1][0] == 1 && x.array[1][2] == 1 && x.array[1][3] == 1) {
1456
- x.array[1][1]++;
1457
- } else {
1458
- x.array.splice(1, 0, [1, 1, 1, 1]);
1459
- }
1460
- x.array[0] = Math.log10(x.array[0])
1461
- }
1462
- if (x.array[0] < MAX_E && x.array[1] !== undefined &&
1463
- x.array[1][0] == 1 && x.array[1][1] >= 1 &&
1464
- x.array[1][2] == 1 && x.array[1][3] == 1) { // check < 9e15
1465
-
1466
- x.array[0] = Math.pow(10, x.array[0]);
1467
- if (x.array[1][1] > 1) {
1468
- x.array[1][1]--;
1469
- } else {
1470
- x.array.splice(1, 1);
1471
- }
1472
- b = true;
1473
- }
1474
- if (x.array[x.array.length - 1][3] > MAX_SAFE_INTEGER) { // check layer++
1475
- x.layer++;
1476
- x.array = [
1477
- x.array[x.array.length - 1][3]
1478
- ];
1479
- b = true;
1480
- } else if (x.layer && x.array.length == 1) {
1481
- x.layer--;
1482
- x.array = [
1483
- 10
1484
- ];
1485
- b = true;
1486
- }
1487
- while (x.array.length >= 2 && x.array[0] == 1 && x.array[1][1]) {
1488
- // [1, [sth, 1, sth, sth]]
1489
- if (x.array[1][1] > 1) {
1490
- x.array[1][1]--;
1491
- } else {
1492
- x.array.splice(1, 1);
1493
- }
1494
- x.array[0] = 10;
1495
- b = true;
1496
- }
1497
- if (x.array.length >= 2 && x.array[0] < MAX_SAFE_INTEGER && x.array[1][0] >= 2 && x.array[1][1] == 1) {
1498
- // [1e9, [2, 1, sth, sth]]
1499
- x.array.splice(1, 1, [x.array[1][0] - 1, x.array[0] - 1, x.array[1][2], x.array[1][3]]);
1500
-
1501
- x.array[0] = 10;
1502
- b = true
1503
- }
1504
- for (i = 1; i < x.array.length - 1; ++i) {
1505
- 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]) {
1506
- // same array's merge
1507
- x.array[i][1] += x.array[i + 1][1];
1508
- x.array.splice(i + 1, 1);
1509
- --i;
1510
- b = true;
1511
- }
1512
-
1513
- }
1514
- if (x.array.length >= 2 && x.array[1][0] != 1 && x.array[1][0] != "x" && x.array[1][0] < 1000) {
1515
- // [sth, [2, sth, sth, sth]]
1516
- 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]]);
1517
- x.array[0] = 1;
1518
- if (x.array[2][1] > 1) {
1519
- x.array[2][1]--;
1520
- } else {
1521
- x.array.splice(2, 1);
1522
- } // && x.array[1][2] == 1 && x.array[1][3] == 1
1523
- b = true;
1524
- }
1525
- if (x.array.length >= 2 && x.array[1][0] == 'x' && x.array[0] < MAX_SAFE_INTEGER) {
1526
- // check [1000, ["x", y, 1, 1]] (like (10{x})^y sth<9e15)
1527
-
1528
- // [10, [1000, 1, 1, 1], ["x", y-1, 1, 1]]
1529
- if (x.array[1][1] == 1) {
1530
- x.array[1][0] = x.array[0];
1531
- x.array[0] = 10;
1532
- } else {
1533
- // [q, ["x", y, z, w]]
1534
-
1535
- // Insert at index 1, insert [x.array[0], 1, z, w]
1536
- // [q, [q, 1, z, w], ["x", y, z, w]]
1537
- x.array.splice(1, 0, [x.array[0], 1, x.array[1][2], x.array[1][3]])
1538
-
1539
- // [10, [q, 1, z, w], ["x", y-1, z, w]]
1540
- x.array[0] = 10;
1541
- x.array[2][1]--;
1542
- }
1543
- b = true;
1544
- }
1545
- if (x.array.length >= 2 && x.array[1][2] >= 2 && x.array[1][0] == 1 && x.array[0] < MAX_SAFE_INTEGER) {
1546
- // [8e15, [1, sth, >=2, sth]]
1547
- if (x.array[1][1] == 1) {
1548
- x.array.splice(1, 1, ["x", x.array[0] - 1, x.array[1][2] - 1, x.array[1][3]])
1549
- x.array[0] = 10
1550
- b = true;
1551
- } else if (x.array[1][1] > 1) {
1552
- var temp = x.array[1][1];
1553
- x.array.splice(1, 1, ["x", x.array[0] - 1, x.array[1][2] - 1, x.array[1][3]])
1554
- x.array.push([1, temp - 1, 2, 1])
1555
- x.array[0] = 10
1556
- b = true;
1557
- }
1558
- }
1559
- for (i = 1; i < x.array.length; ++i) {
1560
- if (x.array[i][2] > MAX_SAFE_INTEGER) {
1561
- // check {}brace >9e15
1562
- if (i != x.array.length - 1 && x.array[i + 1][2] == "x") {
1563
- x.array[i + 1][1]++;
1564
- } else if (x.array[i][2] != "x") {
1565
- x.array.splice(i + 1, 0, [1, 1, "x", x.array[i][3]]);
1566
-
1567
- } else if (x.array[i][2] == "x") {
1568
- x.array.splice(i + 1, 0, [1, 1, 1, x.array[i][3]] + 1);
1569
-
1570
- }
1571
- x.array[0] = x.array[i][2] + 1;
1572
- x.array.splice(1, i);
1573
- --i
1574
- b = true;
1575
- }
1576
- if (i < 1) i = 1;
1577
- if (x.array[i][1] > MAX_SAFE_INTEGER) {
1578
- // check repeat count >9e15
1579
- if (i != x.array.length - 1 && x.array[i + 1][0] == "x") {
1580
- x.array[i + 1][1]++;
1581
- } else if (x.array[i][0] != "x") {
1582
- x.array.splice(i + 1, 0, [x.array[i][0] + 1, 1, x.array[i][2], x.array[i][3]]);
1583
-
1584
- } else if (x.array[i][0] == "x") {
1585
- x.array.splice(i + 1, 0, [1, 1, x.array[i][2] + 1, x.array[i][3]]);
1586
-
1587
- }
1588
- x.array[0] = x.array[i][1] + 1;
1589
- x.array.splice(1, i);
1590
- --i
1591
- b = true;
1592
- }
1593
- if (i < 1) i = 1;/*
1594
- if (x.array[i][2] > MAX_SAFE_INTEGER) {
1595
- x.array[0] = x.array[i][2] + 1;
1596
-
1597
- if (i != x.array.length -1 && x.array[i+1][2] == "x"){
1598
- x.array[i+1][1]++;
1599
- x.array.splice(i,1)
1600
- }else if (x.array[i][2] != "x"){
1601
- x.array.push([1, 1, "x", x.array[i][3]])
1602
- x.array.splice(i,1)
1603
- }
1604
- --i
1605
- b = true;
1606
- }*/
1607
- if (i < 1) i = 1;
1608
- if (x.array[i][0] > MAX_SAFE_INTEGER) {
1609
- x.array[0] = x.array[i][0] + 1;
1610
- if (i != x.array.length - 1 && x.array[i + 1][0] == "x") {
1611
- x.array[i + 1][1]++;
1612
- } else {
1613
- x.array.splice(i + 1, 0, ["x", 1, x.array[i][2], x.array[i][3]]);
1614
- }
1615
- x.array.splice(1, i);
1616
-
1617
- b = true;
1618
- }
1619
-
1620
- }
1621
- whileTimeRuns++
1622
- } while (b && whileTimeRuns < maxWhileTime)
1623
- return x;
1624
- }
1625
- P.overflow = function (start, power, meta = 1) {
1626
- var number = this.clone()
1627
- power = PowiainaNum(power)
1628
- meta = PowiainaNum(meta)
1629
- start = PowiainaNum(start)
1630
- if (number.gte(start)) {
1631
- var s = start.iteratedlog(10, meta)
1632
- number = PowiainaNum(10).iteratedexp(meta, number.iteratedlog(10, meta).div(s).pow(power).mul(s));
1633
- }
1634
- return number
1635
- }
1636
- Q.overflow = function (num, start, power, meta = 1) {
1637
- return PowiainaNum(num).overflow(start, power, meta)
1638
- }
1639
-
1640
- P.toJSON = function () {
1641
- if (PowiainaNum.serializeMode == PowiainaNum.JSON) {
1642
- var a = [];
1643
- a.push(this.array[0])
1644
- 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]]);
1645
- return {
1646
- array: a,
1647
- layer: this.layer,
1648
- sign: this.sign
1649
- };
1650
- } else if (PowiainaNum.serializeMode == PowiainaNum.STRING) {
1651
- return this.toString(1);
1652
- }
1653
-
1654
- }
1655
-
1656
-
1657
- P.toString = function (formatType = 0) {
1658
- if (isNaN(this.array[0])) return "NaN";
1659
- if (!isFinite(this.array[0])) return "Infinity";
1660
- var s = "";
1661
- if (formatType == 1) {
1662
- s = "l" + (this.layer.toString()) + " s" + (this.sign.toString()) + " a" + JSON.stringify(this.array)
1663
- } else if (formatType == 0) {
1664
- for (let i = this.array.length - 1; i > 0; i--) {
1665
- /*if (this.array[i][0] == "x" && this.array[i][2] == 1 && this.array[i][3] == 1){
1666
- if (this.array[i][1] >= 5)
1667
- s += "J^"+this.array[i][1] + " "
1668
- else
1669
- s += "J".repeat(this.array[i][1]) + " "
1670
- }
1671
-
1672
- else */if (this.array[i][3] == 1 && this.array[i][2] == 1) s += "(" + "10{" + this.array[i][0] + "})" + "^" + this.array[i][1] + " "
1673
- else if (this.array[i][3] == 1) s += "(" + "eps" + this.array[i][2] + ",10{" + this.array[i][0] + "})" + "^" + this.array[i][1] + " "
1674
- else s += "(" + "meg" + this.array[i][3] + ",eps" + this.array[i][2] + ",10{" + this.array[i][0] + "})" + "^" + this.array[i][1] + " "
1675
- }
1676
- s += this.array[0]
1677
- s = (this.sign < 0 ? "-" : "") + (this.layer > 0 ? `lay${this.layer.toFixed(0)} ` : "") + s
1678
- }
1679
- return s
1680
- }
1681
- Q.fromNumber = function (input) {
1682
- if (typeof input != 'number') throw Error(invalidArgument + "Expected Number");
1683
- var x = new PowiainaNum();
1684
- x.array[0] = Math.abs(input);
1685
- x.sign = input < 0 ? -1 : 1;
1686
- x.normalize();
1687
- return x;
1688
- }
1689
-
1690
- var LONG_STRING_MIN_LENGTH = 17;
1691
- var log10LongString = function log10LongString(str) {
1692
- return Math.log10(Number(str.substring(0, LONG_STRING_MIN_LENGTH))) + (str.length - LONG_STRING_MIN_LENGTH);
1693
- }
1694
- Q.fromString = function (input, nocache=false) {
1695
- var originalInput = input;
1696
- if (typeof input != "string") throw Error(invalidArgument + "Expected String");
1697
- if (C[input] && !nocache) {
1698
- var x = new PowiainaNum();
1699
- deepCopyProps(C[input].array, x.array)
1700
- x.sign = C[input].sign
1701
- x.layer = C[input].layer
1702
- return x
1703
- } else if (input[0] == "l") {
1704
- let temp1 = input.indexOf("l");
1705
- let temp2 = input.indexOf("s");
1706
- let temp3 = input.indexOf("a");
1707
- if (temp2 == -1 || temp3 == -1) {
1708
- console.warn(powiainaNumError + "Malformed input: " + input);
1709
- return PowiainaNum.NaN.clone();
1710
- }
1711
- let layer = Number(input.slice(temp1 + 1, temp2 - 1))
1712
- let sign = Number(input.slice(temp2 + 1, temp3 - 1))
1713
- let array = JSON.parse(input.slice(temp3 + 1, input.length))
1714
-
1715
- var x = PowiainaNum();
1716
- x.layer = layer;
1717
- x.sign = sign;
1718
- x.array = array;
1719
- x.normalize();
1720
- return x;
1721
- } else {
1722
- // from ExpantaNum.js
1723
- var x = new PowiainaNum();
1724
- var negateIt = false;
1725
- if (input[0] == "-" || input[0] == "+") {
1726
- var numSigns = input.search(/[^-\+]/);
1727
- var signs = input.substring(0, numSigns);
1728
- negateIt = signs.match(/-/g).length % 2 == 1;
1729
- input = input.substring(numSigns);
1730
- }
1731
- if (input == "NaN") x.array = [
1732
- NaN
1733
- ];
1734
- else if (input == "Infinity") x.array = [
1735
- Infinity
1736
- ];
1737
- else {
1738
- x.array = [
1739
- 0
1740
- ];
1741
- var a, b, c, d, i;
1742
-
1743
- var Klayer = 0;
1744
- if (input[0] == "J") {
1745
- if (input[1] == "^") {
1746
- a = input.substring(2).search(/[^0-9]/) + 2;
1747
- Klayer = Number(input.substring(2, a));
1748
- input = input.substring(a + 1);
1749
- } else {
1750
- a = input.search(/[^J]/);
1751
- Klayer = a;
1752
- input = input.substring(a);
1753
- }
1754
- }
1755
- while (input) {
1756
- if (/^\(?10[\^\{]/.test(input)) {
1757
- if (input[0] == "(") {
1758
- input = input.substring(1);
1759
- }
1760
- var arrows;
1761
- if (input[2] == "^") {
1762
- a = input.substring(2).search(/[^\^]/);
1763
- arrows = a;
1764
- b = a + 2;
1765
- } else {
1766
- a = input.indexOf("}");
1767
- arrows = Number(input.substring(3, a));
1768
- b = a + 1;
1769
- }
1770
- input = input.substring(b);
1771
- if (input[0] == ")") {
1772
- a = input.indexOf(" ");
1773
- c = Number(input.substring(2, a));
1774
- input = input.substring(a + 1);
1775
- } else {
1776
- c = 1;
1777
- }
1778
- if (arrows == 1) {
1779
- if (x.array.length >= 2 && x.array[1][0] == 1) {
1780
- x.array[1][1] += c;
1781
- } else {
1782
- x.array.splice(1, 0, [1, c, 1, 1]);
1783
- }
1784
- } else if (arrows == 2) {
1785
- a = x.array.length >= 2 && x.array[1][0] == 1 ? x.array[1][1] : 0;
1786
- b = x.array[0];
1787
- if (b >= 1e10) ++a;
1788
- if (b >= 10) ++a;
1789
- x.array[0] = a;
1790
- if (x.array.length >= 2 && x.array[1][0] == 1) x.array.splice(1, 1);
1791
- d = x.getOperatorIndex(2);
1792
- if (Number.isInteger(d)) x.array[d][1] += c;
1793
- else x.array.splice(Math.ceil(d), 0, [2, c, 1, 1]);
1794
- } else {
1795
- a = x.operatorE(arrows - 1);
1796
- b = x.operatorE(arrows - 2);
1797
- if (b >= 10) ++a;
1798
- d = x.getOperatorIndex(arrows);
1799
- x.array.splice(1, Math.ceil(d) - 1);
1800
- x.array[0] = a;
1801
- if (Number.isInteger(d)) x.array[1][1] += c;
1802
- else x.array.splice(1, 0, [arrows, c, 1, 1]);
1803
-
1804
- }
1805
- } else {
1806
- break;
1807
- }
1808
- }
1809
- a = input.split(/[Ee]/);
1810
- b = [x.array[0], 0];
1811
- c = 1;
1812
- for (let i = a.length - 1; i >= 0; --i) {
1813
- //The things that are already there
1814
- if (b[0] < MAX_E && b[1] === 0) {
1815
- b[0] = Math.pow(10, c * b[0]);
1816
- } else if (c == -1) {
1817
- if (b[1] === 0) {
1818
- b[0] = Math.pow(10, c * b[0]);
1819
- } else if (b[1] == 1 && b[0] <= Math.log10(Number.MAX_VALUE)) {
1820
- b[0] = Math.pow(10, c * Math.pow(10, b[0]));
1821
- } else {
1822
- b[0] = 0;
1823
- }
1824
- b[1] = 0;
1825
- } else {
1826
- b[1]++;
1827
- }
1828
- //Multiplying coefficient
1829
- var decimalPointPos = a[i].indexOf(".");
1830
- var intPartLen = decimalPointPos == -1 ? a[i].length : decimalPointPos;
1831
- if (b[1] === 0) {
1832
- if (intPartLen >= LONG_STRING_MIN_LENGTH) b[0] = Math.log10(b[0]) + log10LongString(a[i].substring(0, intPartLen)), b[1] = 1;
1833
- else if (a[i]) b[0] *= Number(a[i]);
1834
- } else {
1835
- d = intPartLen >= LONG_STRING_MIN_LENGTH ? log10LongString(a[i].substring(0, intPartLen)) : a[i] ? Math.log10(Number(a[i])) : 0;
1836
- if (b[1] == 1) {
1837
- b[0] += d;
1838
- } else if (b[1] == 2 && b[0] < MAX_E + Math.log10(d)) {
1839
- b[0] += Math.log10(1 + Math.pow(10, Math.log10(d) - b[0]));
1840
- }
1841
- }
1842
- //Carrying
1843
- if (b[0] < MAX_E && b[1]) {
1844
- b[0] = Math.pow(10, b[0]);
1845
- b[1]--;
1846
- } else if (b[0] > MAX_SAFE_INTEGER) {
1847
- b[0] = Math.log10(b[0]);
1848
- b[1]++;
1849
- }
1850
- }
1851
- x.array[0] = b[0];
1852
- if (b[1]) {
1853
- if (x.array.length >= 2 && x.array[1][0] == 1) x.array[1][1] += b[1];
1854
- else x.array.splice(1, 0, [1, b[1], 1, 1]);
1855
- }
1856
- if (Klayer > 0) {
1857
- x.array.push(["x", Klayer, 1, 1])
1858
- }
1859
- }
1860
- if (negateIt) x.sign *= -1;
1861
- x.normalize();
1862
- C[originalInput] = {}
1863
- deepCopyProps({
1864
- array: x.array,
1865
- layer: x.layer,
1866
- sign: x.sign
1867
- }, C[originalInput])
1868
- return x;
1869
-
1870
- }
1871
- }
1872
- Q.fromArray = function (input) {
1873
- if (!Array.isArray(input)) {
1874
- throw Error("[" + powiainaNumError + "] Unexcepted array")
1875
- }
1876
- let x = new PowiainaNum(0);
1877
- x.array = input;
1878
- return x;
1879
- }
1880
- P.toNumber = function () {
1881
- if (this.sign == -1) return -1 * (this.abs().toNumber());
1882
- if (this.layer > 0) return Infinity;
1883
- if (this.array.length >= 2 &&
1884
- (
1885
- this.array[1][3] >= 2 ||
1886
- this.array[1][2] == "x" ||
1887
- this.array[1][2] >= 2 ||
1888
- this.array[1][0] == "x" ||
1889
- this.array[1][0] >= 2 ||
1890
- this.array[1][1] >= 2 ||
1891
- this.array[1][1] == 1 && this.array[0][1] > Math.log10(Number.MAX_VALUE)
1892
- )
1893
- ) return Infinity;
1894
- if (this.array.length >= 2 && this.array[1][1] == 1) return Math.pow(10, this.array[0]);
1895
- return this.array[0];
1896
- };
1897
- P.clone = function () {
1898
- var temp = new PowiainaNum();
1899
- var array = [];
1900
- array.push(this.array[0]);
1901
- for (var i = 1; i < this.array.length; ++i)
1902
- array.push([this.array[i][0], this.array[i][1], this.array[i][2], this.array[i][3]]);
1903
-
1904
- temp.array = array;
1905
- temp.sign = this.sign;
1906
- temp.layer = this.layer;
1907
- return temp;
1908
- };
1909
-
1910
-
1911
- //#region toGlobalScope
1912
- function clone(obj) {
1913
- var i, p, ps;
1914
- function PowiainaNum(input) { // constructor of this function
1915
- var x = this;
1916
- if (!(x instanceof PowiainaNum)) return new PowiainaNum(input);
1917
- x.constructor = PowiainaNum;
1918
-
1919
- var temp, temp2, temp3;
1920
-
1921
- if (typeof input == "number") {
1922
- temp = PowiainaNum.fromNumber(input);
1923
- } else if (typeof input == "string") {
1924
- temp = PowiainaNum.fromString(input);
1925
- } else if (typeof input == "object" && input instanceof PowiainaNum) {
1926
- temp = input.clone();
1927
-
1928
- } else if (typeof input == "object" && Array.isArray(input)) {
1929
- temp = PowiainaNum.fromArray(input);
1930
-
1931
- }
1932
- else {
1933
- temp = [NaN];
1934
- temp2 = 1;
1935
- temp3 = 0;
1936
- }
1937
- if (typeof temp2 == "undefined") {
1938
- x.array = temp.array;
1939
- x.sign = temp.sign;
1940
- x.layer = temp.layer;
1941
- } else {
1942
- x.array = temp;
1943
- x.sign = temp2;
1944
- x.layer = temp3
1945
- }
1946
- }
1947
- PowiainaNum.prototype = P;
1948
-
1949
- PowiainaNum.JSON = 0;
1950
- PowiainaNum.STRING = 1;
1951
-
1952
- PowiainaNum.NONE = 0;
1953
- PowiainaNum.NORMAL = 1;
1954
- PowiainaNum.ALL = 2;
1955
- PowiainaNum.clone = clone;
1956
- PowiainaNum.config = PowiainaNum.set = config
1957
- for (var prop in Q) {
1958
- //set static methods
1959
- if (Q.hasOwnProperty(prop)) {
1960
- PowiainaNum[prop] = Q[prop];
1961
- }
1962
- }
1963
- if (obj === void 0) obj = {};
1964
- if (obj) {
1965
- ps = ['maxOps', 'serializeMode', 'debug'];
1966
- for (i = 0; i < ps.length;)
1967
- if (!obj.hasOwnProperty(p = ps[i++]))
1968
- obj[p] = this[p];
1969
- }
1970
-
1971
- PowiainaNum.config(obj);
1972
- return PowiainaNum;
1973
- }
1974
- function defineConstants(obj) {
1975
- for (var prop in R) {
1976
- if (R.hasOwnProperty(prop)) {
1977
- if (Object.defineProperty) {
1978
- Object.defineProperty(obj, prop, {
1979
- configurable: false,
1980
- enumerable: true,
1981
- writable: false,
1982
- value: new PowiainaNum(R[prop])
1983
- });
1984
- } else {
1985
- obj[prop] = new PowiainaNum(R[prop]);
1986
- }
1987
- }
1988
- }
1989
- return obj;
1990
- }
1991
- function config(obj) {
1992
- if (!obj || typeof obj !== 'object') {
1993
- throw Error(powiainaNumError + 'Object expected');
1994
- }
1995
- var i, p, v,
1996
- ps = [
1997
- 'maxOps', 1, Number.MAX_SAFE_INTEGER,
1998
- 'serializeMode', 0, 1,
1999
- 'debug', 0, 2
2000
- ];
2001
- for (i = 0; i < ps.length; i += 3) {
2002
- if ((v = obj[p = ps[i]]) !== void 0) {
2003
- if (Math.floor(v) === v && v >= ps[i + 1] && v <= ps[i + 2]) this[p] = v;
2004
- else throw Error(invalidArgument + p + ': ' + v);
2005
- }
2006
- }
2007
-
2008
- return this;
2009
- }
2010
-
2011
- PowiainaNum = clone(PowiainaNum);
2012
-
2013
- PowiainaNum = defineConstants(PowiainaNum);
2014
-
2015
- PowiainaNum.default = PowiainaNum.PowiainaNum = PowiainaNum;
2016
-
2017
- // Export.
2018
-
2019
- // AMD. Asynchronous Module Definition
2020
-
2021
- if (typeof define == 'function' && define.amd) {
2022
- define(function () {
2023
- return PowiainaNum
2024
- })
2025
-
2026
-
2027
- // Node and other environments that support module.exports.
2028
- } else if (typeof module != 'undefined' && module.exports) {
2029
- module.exports = PowiainaNum;
2030
- // Browser
2031
- } else {
2032
- if (!globalScope) {
2033
- globalScope = typeof self != 'undefined' && self && self.self == self
2034
- ? self : Function('return this')();
2035
- }
2036
- globalScope.PowiainaNum = PowiainaNum;
2037
- }
2038
-
2039
- //#endregion
2040
-
2041
- })(this);