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.
@@ -0,0 +1,994 @@
1
+ 'use strict';
2
+
3
+ function _classCallCheck(a, n) {
4
+ if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
5
+ }
6
+ function _defineProperties(e, r) {
7
+ for (var t = 0; t < r.length; t++) {
8
+ var o = r[t];
9
+ o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
10
+ }
11
+ }
12
+ function _createClass(e, r, t) {
13
+ return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", {
14
+ writable: !1
15
+ }), e;
16
+ }
17
+ function _toPrimitive(t, r) {
18
+ if ("object" != typeof t || !t) return t;
19
+ var e = t[Symbol.toPrimitive];
20
+ if (void 0 !== e) {
21
+ var i = e.call(t, r || "default");
22
+ if ("object" != typeof i) return i;
23
+ throw new TypeError("@@toPrimitive must return a primitive value.");
24
+ }
25
+ return ("string" === r ? String : Number)(t);
26
+ }
27
+ function _toPropertyKey(t) {
28
+ var i = _toPrimitive(t, "string");
29
+ return "symbol" == typeof i ? i : i + "";
30
+ }
31
+ function _typeof(o) {
32
+ "@babel/helpers - typeof";
33
+
34
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
35
+ return typeof o;
36
+ } : function (o) {
37
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
38
+ }, _typeof(o);
39
+ }
40
+
41
+ var MSI = 9007199254740991;
42
+ var MSI_LOG10 = 15.954589770191003;
43
+ var MSI_REC = 1.1102230246251568e-16;
44
+ var LONG_STRING_MIN_LENGTH = 17;
45
+ function newOperator(r) {
46
+ var a = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
47
+ var e = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
48
+ var m = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1;
49
+ return {
50
+ repeat: r,
51
+ arrow: a,
52
+ expans: e,
53
+ megota: m,
54
+ valuereplaced: a == Infinity ? 0 : e == Infinity ? 1 : -1
55
+ };
56
+ }
57
+ function compareTuples() {
58
+ for (var _len = arguments.length, tuples = new Array(_len), _key = 0; _key < _len; _key++) {
59
+ tuples[_key] = arguments[_key];
60
+ }
61
+ for (var i = 0; i < Math.min(tuples[0].length, tuples[1].length); i++) {
62
+ var a = tuples[0][i];
63
+ var b = tuples[1][i];
64
+ if (a < b) return -1;
65
+ if (a > b) return 1;
66
+ }
67
+ return 0;
68
+ }
69
+ function log10LongString(str) {
70
+ return Math.log10(Number(str.substring(0, LONG_STRING_MIN_LENGTH))) + (str.length - LONG_STRING_MIN_LENGTH);
71
+ }
72
+ var PowiainaNum = /*#__PURE__*/function () {
73
+ function PowiainaNum(arg1) {
74
+ _classCallCheck(this, PowiainaNum);
75
+ this.array = [{
76
+ arrow: 0,
77
+ expans: 1,
78
+ megota: 1,
79
+ repeat: NaN
80
+ }];
81
+ this.small = false;
82
+ this.sign = 0;
83
+ this.layer = 0;
84
+ if (typeof arg1 == "undefined") ; else if (typeof arg1 == "number") {
85
+ var obj = PowiainaNum.fromNumber(arg1);
86
+ this.resetFromObject(obj);
87
+ } else if (_typeof(arg1) == "object") {
88
+ var _obj = PowiainaNum.fromObject(arg1);
89
+ this.resetFromObject(_obj);
90
+ } else if (typeof arg1 == "string") {
91
+ var _obj2 = PowiainaNum.fromString(arg1);
92
+ this.resetFromObject(_obj2);
93
+ } else ;
94
+ }
95
+ /**
96
+ * Addition
97
+ * @returns the sum of `this` and `other`
98
+ */
99
+ return _createClass(PowiainaNum, [{
100
+ key: "add",
101
+ value: function add(other) {
102
+ var x = this.clone();
103
+ var y = new PowiainaNum(other);
104
+ // inf + -inf = nan
105
+ if (x.eq(PowiainaNum.POSITIVE_INFINITY) && y.eq(PowiainaNum.NEGATIVE_INFINITY) || x.eq(PowiainaNum.NEGATIVE_INFINITY) && y.eq(PowiainaNum.POSITIVE_INFINITY)) return PowiainaNum.NaN.clone();
106
+ // inf & nan check
107
+ if (!x.isFinite()) return x.clone();
108
+ if (!y.isFinite()) return y.clone();
109
+ // if x or y = 0, return other.
110
+ if (x.isZero()) return y.clone();
111
+ if (y.isZero()) return x.clone();
112
+ // x+ -x = 0
113
+ if (x.sign == -y.sign && function () {
114
+ var a = x.abs();
115
+ var b = y.abs();
116
+ return a.eq(b);
117
+ }()) return PowiainaNum.ZERO.clone();
118
+ // calculate anything > e9e15 or <e-9e15, take absval bigger.
119
+ if (x.abs().lt(PowiainaNum.E_MSI_REC) || x.abs().gt(PowiainaNum.E_MSI) || y.abs().lt(PowiainaNum.E_MSI_REC) || y.abs().gt(PowiainaNum.E_MSI)) {
120
+ return x.maxabs(y);
121
+ }
122
+ if (x.sign == -1) {
123
+ return x.neg().add(y.neg()).neg();
124
+ }
125
+ // if ((x.sign==1&&y.sign==-1&&x.lt(y.abs())) ) return y.neg().add(x.neg()).neg();
126
+ var a, b; //a=bigger, b=smaller
127
+ if (x.cmpabs(y) > 0) {
128
+ a = x;
129
+ b = y;
130
+ } else {
131
+ b = x;
132
+ a = y;
133
+ }
134
+ var mult = 1;
135
+ if (!a.small && !b.small && !a.getOperator(1) && !b.getOperator(1) && a.sign == b.sign) {
136
+ return new PowiainaNum((a.getOperator(0) + b.getOperator(0)) * a.sign);
137
+ }
138
+ var alog10 = (a.small ? -1 : 1) * (a.getOperator(1) ? a.getOperator(0) : Math.log10(a.getOperator(0)));
139
+ var blog10 = (b.small ? -1 : 1) * (b.getOperator(1) ? b.getOperator(0) : Math.log10(b.getOperator(0)));
140
+ if (alog10 - blog10 > MSI_LOG10) return a;
141
+ var offset = -Math.floor(alog10); //a number can make a+off in [0,1)
142
+ var r,
143
+ l = 0,
144
+ t;
145
+ t = a.sign * Math.pow(10, alog10 + offset) + b.sign * Math.pow(10, blog10 + offset);
146
+ if (t > 0) l = Math.log10(t) - offset;
147
+ if (t < 0) {
148
+ l = Math.log10(-t) - offset;
149
+ mult *= -1;
150
+ }
151
+ if (t == 0) throw Error("Encounter a calculate error");
152
+ r = new PowiainaNum();
153
+ r.sign = 1;
154
+ if (l > MSI_LOG10 || l < -MSI_LOG10) {
155
+ r.setOperator(1, 1);
156
+ r.setOperator(Math.log10(Math.abs(l)), 1);
157
+ } else {
158
+ r.setOperator(Math.pow(10, Math.abs(l)), 0);
159
+ }
160
+ r.small = l < 0 ? true : false;
161
+ r.sign *= mult;
162
+ return r;
163
+ }
164
+ }, {
165
+ key: "sub",
166
+ value: function sub(a) {
167
+ return this.add(new PowiainaNum(a).neg());
168
+ }
169
+ }, {
170
+ key: "mul",
171
+ value: function mul(other) {
172
+ var x = this.clone();
173
+ var y = new PowiainaNum(other);
174
+ // inf * -inf = -inf
175
+ if (x.eq(PowiainaNum.POSITIVE_INFINITY) && y.eq(PowiainaNum.NEGATIVE_INFINITY) || y.eq(PowiainaNum.POSITIVE_INFINITY) && x.eq(PowiainaNum.NEGATIVE_INFINITY)) return PowiainaNum.NEGATIVE_INFINITY.clone();
176
+ if (x.isInfinite() && y.eq(PowiainaNum.ZERO) && y.isInfinite() && x.eq(PowiainaNum.ZERO)) return PowiainaNum.NaN.clone();
177
+ if (x.eq(PowiainaNum.NEGATIVE_INFINITY) && y.eq(PowiainaNum.NEGATIVE_INFINITY)) return PowiainaNum.POSITIVE_INFINITY.clone();
178
+ // inf & nan check
179
+ if (!x.isFinite()) return x.clone();
180
+ if (!y.isFinite()) return y.clone();
181
+ if (x.isZero() || y.isZero()) return PowiainaNum.ZERO.clone();
182
+ // x* x^-1 = 0
183
+ /* if (x.small==1-y.small&&(function(){
184
+ let a = x.abs();
185
+ let b = y.abs();
186
+ return a.eq(b)
187
+ })()) return (function () {
188
+ let a = new PowiainaNum(1);
189
+ a.sign = x.sign*y.sign as -1|0| 1;
190
+ return a;
191
+ })(); */
192
+ var r;
193
+ r = x.abs().log10().add(y.abs().log10()).pow10();
194
+ r.sign = x.sign * y.sign;
195
+ return r;
196
+ }
197
+ }, {
198
+ key: "div",
199
+ value: function div(other) {
200
+ var x = new PowiainaNum(other).rec();
201
+ return this.mul(x);
202
+ }
203
+ }, {
204
+ key: "pow10",
205
+ value: function pow10() {
206
+ var r = this.clone();
207
+ // inf & nan check
208
+ if (!this.isFinite()) return this.clone();
209
+ if (r.lt(0)) {
210
+ // 10^(-x) = 1/(10^x)
211
+ r.sign *= -1;
212
+ return r.pow10().rec();
213
+ }
214
+ if (r.small) {
215
+ if (r.lt(PowiainaNum.MSI_REC)) return PowiainaNum.ONE;
216
+ return new PowiainaNum(Math.pow(10, Math.pow(r.getOperator(0), -1)));
217
+ }
218
+ r.setOperator(r.getOperator(1) + 1, 1);
219
+ r.normalize();
220
+ return r;
221
+ }
222
+ }, {
223
+ key: "pow",
224
+ value: function pow(x) {
225
+ var other = new PowiainaNum(x);
226
+ if (!other.isFinite()) return other.clone();
227
+ if (!this.isFinite()) return this.clone();
228
+ if (this.eq(10)) return other.pow10();
229
+ if (this.lt(0)) {
230
+ if (!other.isInt()) return PowiainaNum.NaN.clone();
231
+ var r = this.abs().pow(other);
232
+ r.sign = function () {
233
+ var a = other.mod(2).round();
234
+ if (a.eq(0) || a.eq(2)) return 1;
235
+ return -1;
236
+ }();
237
+ return r;
238
+ }
239
+ // log10(a^b) = b log10(a)
240
+ return this.log10().mul(other).pow10();
241
+ }
242
+ }, {
243
+ key: "pow_base",
244
+ value: function pow_base(x) {
245
+ var a = new PowiainaNum(x);
246
+ return a.pow(this);
247
+ }
248
+ }, {
249
+ key: "root",
250
+ value: function root(x) {
251
+ var other = new PowiainaNum(x);
252
+ return this.pow(other.rec());
253
+ }
254
+ }, {
255
+ key: "sqrt",
256
+ value: function sqrt() {
257
+ return this.pow(0.5);
258
+ }
259
+ }, {
260
+ key: "cbrt",
261
+ value: function cbrt() {
262
+ return this.root(3);
263
+ }
264
+ }, {
265
+ key: "abs",
266
+ value: function abs() {
267
+ var obj = this.clone();
268
+ if (obj.sign < 0) obj.sign *= -1;
269
+ return obj;
270
+ }
271
+ }, {
272
+ key: "log10",
273
+ value: function log10() {
274
+ if (this.lt(0)) return PowiainaNum.NaN.clone();
275
+ if (this.isZero()) return PowiainaNum.NEGATIVE_INFINITY.clone();
276
+ if (this.small) {
277
+ var _x = this.clone();
278
+ _x.small = !_x.small;
279
+ return _x.log10().neg();
280
+ }
281
+ if (this.getOperator(1) == 0) return new PowiainaNum(Math.log10(this.getOperator(0)));
282
+ var x = this.clone();
283
+ x.setOperator(x.getOperator(1) - 1, 1);
284
+ x.normalize();
285
+ return x;
286
+ }
287
+ }, {
288
+ key: "log",
289
+ value: function log(base) {
290
+ // log_a b = log_x b / log_x a;
291
+ var other = new PowiainaNum(base);
292
+ return this.log10().div(other.log10());
293
+ }
294
+ }, {
295
+ key: "mod",
296
+ value: function mod(x) {
297
+ var other = new PowiainaNum(x);
298
+ var division = this.div(other);
299
+ return division.sub(division.floor()).mul(other);
300
+ }
301
+ }, {
302
+ key: "max",
303
+ value: function max(x) {
304
+ var other = new PowiainaNum(x);
305
+ return this.lt(other) ? other.clone() : this.clone();
306
+ }
307
+ }, {
308
+ key: "min",
309
+ value: function min(x) {
310
+ var other = new PowiainaNum(x);
311
+ return this.gte(other) ? other.clone() : this.clone();
312
+ }
313
+ }, {
314
+ key: "maxabs",
315
+ value: function maxabs(x) {
316
+ var other = new PowiainaNum(x).abs();
317
+ return this.abs().lt(other) ? other.clone() : this.clone();
318
+ }
319
+ }, {
320
+ key: "minabs",
321
+ value: function minabs(x) {
322
+ var other = new PowiainaNum(x).abs();
323
+ return this.abs().gt(other) ? other.clone() : this.clone();
324
+ }
325
+ }, {
326
+ key: "cmpabs",
327
+ value: function cmpabs(x) {
328
+ var other = new PowiainaNum(x).abs();
329
+ return this.abs().cmp(other);
330
+ }
331
+ }, {
332
+ key: "neg",
333
+ value: function neg() {
334
+ var a = this.clone();
335
+ a.sign *= -1;
336
+ a.normalize();
337
+ return a;
338
+ }
339
+ }, {
340
+ key: "rec",
341
+ value: function rec() {
342
+ var a = this.clone();
343
+ a.small = !a.small;
344
+ return a;
345
+ }
346
+ }, {
347
+ key: "floor",
348
+ value: function floor() {
349
+ if (this.isInt()) return this.clone();
350
+ if (this.small) {
351
+ if (this.sign == 1) return PowiainaNum.ZERO.clone();else return PowiainaNum.ONE.neg().clone();
352
+ }
353
+ var r = this.abs();
354
+ r.setOperator(Math[this.sign == 1 ? "floor" : "ceil"](r.getOperator(0)), 0);
355
+ return r;
356
+ }
357
+ }, {
358
+ key: "ceil",
359
+ value: function ceil() {
360
+ if (this.isInt()) return this.clone();
361
+ if (this.small) {
362
+ if (this.sign == 1) return PowiainaNum.ONE.clone();else return PowiainaNum.ZERO.clone();
363
+ }
364
+ var r = this.abs();
365
+ r.setOperator(Math[this.sign == 1 ? "ceil" : "floor"](r.getOperator(0)), 0);
366
+ r.sign = this.sign;
367
+ return r;
368
+ }
369
+ }, {
370
+ key: "round",
371
+ value: function round() {
372
+ if (this.isInt()) return this.clone();
373
+ if (this.small) {
374
+ if (this.sign == 1) {
375
+ if (this.rec().lte(2)) return PowiainaNum.ONE.clone();else return PowiainaNum.ZERO.clone();
376
+ } else {
377
+ if (this.abs().rec().lte(2)) return PowiainaNum.ZERO.clone();else return PowiainaNum.ONE.neg().clone();
378
+ }
379
+ }
380
+ var r = this.abs();
381
+ r.setOperator(Math.round(r.getOperator(0)), 0);
382
+ r.sign = this.sign;
383
+ return r;
384
+ }
385
+ /**
386
+ * @returns if this<other, return -1, if this=other, return 0, if this>other, return 1, if this!<=>, return 2
387
+ */
388
+ }, {
389
+ key: "compare",
390
+ value: function compare(x) {
391
+ var other = new PowiainaNum(x);
392
+ if (this.isNaN() || other.isNaN()) return 2;
393
+ if (this.sign < other.sign) return -1;
394
+ if (this.sign > other.sign) return 1;
395
+ //this.sign = other.sign
396
+ var allneg = this.sign == -1 && other.sign == -1;
397
+ if (this.small && !other.small) return -1 * (allneg ? -1 : 1);
398
+ if (other.small && !this.small) return 1 * (allneg ? -1 : 1);
399
+ var resultreverse = 1;
400
+ if (this.small && other.small) resultreverse *= -1;
401
+ if (allneg) resultreverse *= -1;
402
+ var result = 0;
403
+ for (var i = 0; this.array.length - 1 - i >= 0 && other.array.length - 1 - i >= 0; i++) {
404
+ var op1 = this.array[this.array.length - 1 - i];
405
+ var op2 = other.array[other.array.length - 1 - i];
406
+ var cmp = compareTuples([op1.megota, op1.expans, op1.arrow, op1.repeat], [op2.megota, op2.expans, op2.arrow, op2.repeat]);
407
+ if (cmp == 1) {
408
+ result = 1;
409
+ break;
410
+ } else if (cmp == -1) {
411
+ result = -1;
412
+ break;
413
+ }
414
+ }
415
+ return result * resultreverse + 1 - 1;
416
+ }
417
+ }, {
418
+ key: "cmp",
419
+ value: function cmp(other) {
420
+ return this.compare(other);
421
+ }
422
+ }, {
423
+ key: "eq",
424
+ value: function eq(other) {
425
+ return this.cmp(other) === 0;
426
+ }
427
+ }, {
428
+ key: "neq",
429
+ value: function neq(other) {
430
+ return this.cmp(other) !== 0;
431
+ }
432
+ }, {
433
+ key: "lt",
434
+ value: function lt(other) {
435
+ return this.cmp(other) === -1;
436
+ }
437
+ }, {
438
+ key: "lte",
439
+ value: function lte(other) {
440
+ return this.cmp(other) <= 0;
441
+ }
442
+ }, {
443
+ key: "gt",
444
+ value: function gt(other) {
445
+ return this.cmp(other) == 1;
446
+ }
447
+ }, {
448
+ key: "gte",
449
+ value: function gte(other) {
450
+ var t = this.cmp(other);
451
+ return t == 0 || t == 1;
452
+ }
453
+ }, {
454
+ key: "isNaN",
455
+ value: function (_isNaN) {
456
+ function isNaN() {
457
+ return _isNaN.apply(this, arguments);
458
+ }
459
+ isNaN.toString = function () {
460
+ return _isNaN.toString();
461
+ };
462
+ return isNaN;
463
+ }(function () {
464
+ return isNaN(this.getOperator(0));
465
+ })
466
+ }, {
467
+ key: "isZero",
468
+ value: function isZero() {
469
+ return Boolean(this.small && !isFinite(this.getOperator(0)));
470
+ }
471
+ }, {
472
+ key: "isFinite",
473
+ value: function (_isFinite) {
474
+ function isFinite() {
475
+ return _isFinite.apply(this, arguments);
476
+ }
477
+ isFinite.toString = function () {
478
+ return _isFinite.toString();
479
+ };
480
+ return isFinite;
481
+ }(function () {
482
+ return Boolean(this.small || isFinite(this.getOperator(0))) && !this.isNaN();
483
+ })
484
+ }, {
485
+ key: "isInfinite",
486
+ value: function isInfinite() {
487
+ return Boolean(!this.small && !isFinite(this.getOperator(0))) || this.isNaN();
488
+ }
489
+ }, {
490
+ key: "isInt",
491
+ value: function isInt() {
492
+ if (this.isZero()) return true;
493
+ if (!this.small && Number.isInteger(this.getOperator(0))) return true;
494
+ if (this.abs().gte(Math.pow(2, 52))) return true;
495
+ return false;
496
+ }
497
+ /**
498
+ * Normalize functions will make this number convert into standard format.(it also change `this`, like [].sort)
499
+ * @returns normalized number
500
+ */
501
+ }, {
502
+ key: "normalize",
503
+ value: function normalize() {
504
+ //TODO: normalize
505
+ var renormalize = true;
506
+ var x = this;
507
+ for (var _i = 0; _i < this.array.length; _i++) {
508
+ if (this.array[_i].repeat == Infinity) {
509
+ this.array = [{
510
+ arrow: 0,
511
+ expans: 1,
512
+ megota: 1,
513
+ repeat: Infinity
514
+ }];
515
+ this.layer = 0;
516
+ return this;
517
+ }
518
+ }
519
+ for (var i = 1; i < x.array.length; ++i) {
520
+ var e = x.array[i];
521
+ if (e.arrow === null || e.arrow === undefined) {
522
+ e.arrow = 0;
523
+ }
524
+ if (e.expans === null || e.expans === undefined) {
525
+ e.expans = 1;
526
+ }
527
+ if (e.megota === null || e.megota === undefined) {
528
+ e.megota = 1;
529
+ }
530
+ if (isNaN(e.arrow) || isNaN(e.repeat) || isNaN(e.expans) || isNaN(e.megota)) {
531
+ x.array = [newOperator(NaN, 0, 1, 1)];
532
+ return x;
533
+ }
534
+ if (!isFinite(e.repeat) || !isFinite(e.megota)) {
535
+ x.array = [newOperator(Infinity, 0, 1, 1)];
536
+ return x;
537
+ }
538
+ if (!Number.isInteger(e.arrow)) e.arrow = Math.floor(e.arrow);
539
+ if (!Number.isInteger(e.repeat)) e.repeat = Math.floor(e.repeat);
540
+ if (!Number.isInteger(e.expans)) e.expans = Math.floor(e.expans);
541
+ if (!Number.isInteger(e.megota)) e.megota = Math.floor(e.megota);
542
+ }
543
+ do {
544
+ renormalize = false;
545
+ this.array.sort(function (a, b) {
546
+ return compareTuples([a.megota, a.expans, a.arrow], [b.megota, b.expans, b.arrow]);
547
+ });
548
+ for (i = 1; i < x.array.length; ++i) {
549
+ // check 0 repeat count
550
+ if (x.array[i].arrow !== 0 && (x.array[i].repeat === 0 || x.array[i].repeat === null || x.array[i].repeat === undefined)) {
551
+ x.array.splice(i, 1);
552
+ --i;
553
+ continue;
554
+ }
555
+ // check arrow 0 and brace count >=2
556
+ if (x.array[i].arrow == 0 && x.array[i].expans >= 2) {
557
+ x.array[i].arrow = Infinity;
558
+ x.array[i].valuereplaced = 0;
559
+ x.array[i].expans = x.array[i].expans - 1;
560
+ }
561
+ }
562
+ for (var _i2 = 1; _i2 < this.array.length; _i2++) {
563
+ if (this.array[_i2].repeat == 0) {
564
+ this.array.splice(_i2, 1);
565
+ _i2--;
566
+ }
567
+ }
568
+ if (!x.array.length) {
569
+ x.small = !x.small;
570
+ x.array = [newOperator(Infinity)]; // if no array set zero
571
+ }
572
+ if (x.array.length > PowiainaNum.maxOps) x.array.splice(1, x.array.length - PowiainaNum.maxOps); // max operators check
573
+ if (this.getOperator(1) >= 1 && this.getOperator(0) < MSI_LOG10) {
574
+ console.log(this.array);
575
+ this.setOperator(this.getOperator(1) - 1, 1);
576
+ this.setOperator(Math.pow(10, this.getOperator(0)), 0);
577
+ renormalize = true;
578
+ }
579
+ if (this.getOperator(0) > MSI) {
580
+ this.setOperator(this.getOperator(1) + 1, 1);
581
+ this.setOperator(Math.log10(this.getOperator(0)), 0);
582
+ renormalize = true;
583
+ }
584
+ if (this.array.length == 1 && this.array[0].repeat < 1) {
585
+ this.array[0].repeat = 1 / this.array[0].repeat;
586
+ this.small = !this.small;
587
+ renormalize = true;
588
+ }
589
+ while (x.array.length >= 2 && x.array[0].repeat == 1 && x.array[1].repeat) {
590
+ // [1, [R=1, A=sth, E=sth, M=sth]]
591
+ if (x.array[1].repeat > 1) {
592
+ x.array[1].repeat--;
593
+ } else {
594
+ x.array.splice(1, 1);
595
+ }
596
+ x.array[0].repeat = 10;
597
+ renormalize = true;
598
+ }
599
+ if (x.array.length >= 2 && x.array[0].repeat < MSI && x.array[1].arrow >= 2 && x.array[1].repeat == 1) {
600
+ // [1e9, [R=1, A=2, sth, sth]]
601
+ x.array.splice(1, 1, newOperator(x.array[0].repeat, x.array[1].arrow - 1, x.array[1].expans, x.array[1].megota));
602
+ x.array[0].repeat = 10;
603
+ renormalize = true;
604
+ }
605
+ for (i = 1; i < x.array.length - 1; ++i) {
606
+ if (x.array[i].arrow == x.array[i + 1].arrow && x.array[i].expans == x.array[i + 1].expans && x.array[i].megota == x.array[i + 1].megota) {
607
+ // same array's merge
608
+ x.array[i].repeat += x.array[i + 1].repeat;
609
+ x.array.splice(i + 1, 1);
610
+ --i;
611
+ renormalize = true;
612
+ }
613
+ }
614
+ } while (renormalize);
615
+ return this;
616
+ }
617
+ /**
618
+ * @returns number will return the index of the operator in array. return as x.5 if it's between the xth and x+1th operators.
619
+ */
620
+ }, {
621
+ key: "getOperatorIndex",
622
+ value: function getOperatorIndex(arrow) {
623
+ var expans = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
624
+ var megota = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
625
+ for (var i = 0; i < this.array.length; i++) {
626
+ var cmp = compareTuples([this.array[i].megota, this.array[i].expans, this.array[i].arrow], [megota, expans, arrow]);
627
+ if (cmp == 0) return i; // I find it was [xx,xxx,*xxx*,xxx]!
628
+ if (cmp == 1) return i - 0.5; // It's between [xx, xx,xx*,?,*xx]!
629
+ }
630
+ return this.array.length - 0.5;
631
+ }
632
+ /**
633
+ * @returns number repeats of operators with given arguments.
634
+ */
635
+ }, {
636
+ key: "getOperator",
637
+ value: function getOperator(arrow) {
638
+ var expans = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
639
+ var megota = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
640
+ var index = this.getOperatorIndex(arrow, expans, megota);
641
+ if (!this.array[index]) return 0;
642
+ return this.array[index].repeat;
643
+ }
644
+ /**
645
+ * Modify the repeat of operator
646
+ * @param number val the repeat of operator will modify to array.
647
+ * @returns bool Is the operators array changed?
648
+ */
649
+ }, {
650
+ key: "setOperator",
651
+ value: function setOperator(val, arrow) {
652
+ var expans = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
653
+ var megota = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1;
654
+ // if (arrow!=0&&val==0) return false;
655
+ var index = this.getOperatorIndex(arrow, expans, megota);
656
+ if (!this.array[index]) {
657
+ this.array.splice(Math.ceil(index), 0, {
658
+ arrow: arrow,
659
+ expans: expans,
660
+ megota: megota,
661
+ valuereplaced: expans === Infinity ? 1 : arrow == Infinity ? 0 : -1,
662
+ repeat: val
663
+ });
664
+ return true;
665
+ }
666
+ this.array[index].repeat = val;
667
+ // this.normalize()
668
+ return false;
669
+ }
670
+ /**
671
+ * @returns PowiainaNum a PowiainaNum object which deep copied from `this` object.
672
+ */
673
+ }, {
674
+ key: "clone",
675
+ value: function clone() {
676
+ var obj = new PowiainaNum();
677
+ obj.resetFromObject(this);
678
+ return obj;
679
+ }
680
+ }, {
681
+ key: "resetFromObject",
682
+ value: function resetFromObject(powlikeObject) {
683
+ this.array = [];
684
+ for (var i = 0; i < powlikeObject.array.length; i++) {
685
+ this.array[i] = {
686
+ arrow: powlikeObject.array[i].arrow,
687
+ expans: powlikeObject.array[i].expans,
688
+ megota: powlikeObject.array[i].megota,
689
+ repeat: powlikeObject.array[i].repeat,
690
+ valuereplaced: powlikeObject.array[i].valuereplaced
691
+ };
692
+ }
693
+ this.small = powlikeObject.small;
694
+ this.sign = powlikeObject.sign;
695
+ this.layer = powlikeObject.layer;
696
+ return this;
697
+ }
698
+ }, {
699
+ key: "toNumber",
700
+ value: function toNumber() {
701
+ if (this.sign == -1) return -this.neg().toNumber();
702
+ if (this.small) return 1 / this.rec().toNumber();
703
+ if (this.getOperator(1) == 0) return this.getOperator(0);else if (this.getOperator(1) == 1) return Math.pow(10, this.getOperator(0));
704
+ return NaN;
705
+ }
706
+ }, {
707
+ key: "toString",
708
+ value: function toString() {
709
+ if (this.isNaN()) return "NaN";
710
+ if (this.sign == -1) return "-".concat(this.neg().toString());
711
+ if (this.small) {
712
+ if (this.eq(PowiainaNum.ZERO)) return "0";
713
+ return "/".concat(this.rec().toString());
714
+ }
715
+ if (this.isInfinite()) return "Infinity";
716
+ // O^a (10{arrow,expans,megota})^repeation base
717
+ var res = "";
718
+ for (var i = this.array.length - 1; i >= 0; i--) {
719
+ var oper = this.array[i];
720
+ var calc = "10{".concat(oper.arrow === Infinity ? "!" : oper.arrow).concat(oper.expans > 1 || oper.megota > 1 ? ",".concat(oper.expans === Infinity ? "!" : oper.expans) : "").concat(oper.megota > 1 ? ",".concat(oper.megota) : "", "}");
721
+ if (oper.arrow == 1 && oper.expans == 1 && oper.megota == 1 && oper.repeat < 5) {
722
+ calc = "e".repeat(oper.repeat);
723
+ } else if (oper.arrow == 0 && oper.expans == 1 && oper.megota == 1) {
724
+ calc = oper.repeat.toString();
725
+ } else if (oper.repeat > 1) {
726
+ calc = "(".concat(calc, ")^").concat(oper.repeat, " ");
727
+ } else {
728
+ calc = "".concat(calc, " ");
729
+ }
730
+ res += "".concat(calc);
731
+ }
732
+ return res;
733
+ }
734
+ }], [{
735
+ key: "fromNumber",
736
+ value: function fromNumber(x) {
737
+ var obj = new PowiainaNum(); // NaN
738
+ if (x < 0) obj.sign = -1; // negative
739
+ else if (x == 0) obj.sign = 0;else if (x > 0) obj.sign = 1;
740
+ var y = Math.abs(x);
741
+ if (y >= MSI_REC && y < 1) {
742
+ obj.small = true;
743
+ obj.setOperator(1 / y, 0);
744
+ } else if (y < MSI_REC) {
745
+ obj.small = true;
746
+ obj.setOperator(-Math.log10(y), 0);
747
+ obj.setOperator(1, 1);
748
+ } else {
749
+ obj.setOperator(y, 0);
750
+ }
751
+ obj.normalize();
752
+ return obj;
753
+ }
754
+ }, {
755
+ key: "fromString",
756
+ value: function fromString(input) {
757
+ var _a, _b, _c, _d, _e, _f;
758
+ var x = new PowiainaNum();
759
+ var negateIt = false;
760
+ var recipIt = false;
761
+ if (input[0] == "-" || input[0] == "+") {
762
+ var numSigns = input.search(/[^-\+]/);
763
+ var signs = input.substring(0, numSigns);
764
+ negateIt = ((_b = (_a = signs.match(/-/g)) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) % 2 == 1;
765
+ input = input.substring(numSigns);
766
+ }
767
+ if (input[0] == "/") {
768
+ var numSigns = input.search(/[^\/]/);
769
+ var signs = input.substring(0, numSigns);
770
+ recipIt = ((_d = (_c = signs.match(/\//g)) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) % 2 == 1;
771
+ input = input.substring(numSigns);
772
+ }
773
+ if (input == "NaN") x.array = [newOperator(NaN)];else if (input == "Infinity") x.array = [newOperator(Infinity)];else {
774
+ x.sign = 1;
775
+ x.array = [newOperator(0)];
776
+ var a, b, c, d;
777
+ while (input) {
778
+ if (/^(\(?10[\^\{])/.test(input)) {
779
+ /*
780
+ 10^ - 匹配
781
+ 10{ - 匹配
782
+ (10^ - 匹配
783
+ (10{ - 匹配
784
+ 10x - 不匹配(最后一个字符不是 ^ 或 {)
785
+ 110^ - 不匹配(不是以 10 开头)
786
+ */
787
+ if (input[0] == "(") input = input.substring(1);
788
+ //cutted, 10^.... or 10{....
789
+ var arrows, expans, megota;
790
+ if (input[2] == "^") {
791
+ a = input.substring(2).search(/[^\^]/);
792
+ //cut input to ^^...^^, and search how numbers
793
+ arrows = a;
794
+ // 10^^^
795
+ b = a + 2; // b points to after ^'s.
796
+ } else {
797
+ // 10{...}
798
+ a = input.indexOf("}");
799
+ // select contents between {...}
800
+ var tmp = input.substring(3, a).split(",");
801
+ arrows = Number(tmp[0] == "!" ? Infinity : tmp[0]);
802
+ expans = Number((_e = tmp[1] == "!" ? Infinity : tmp[1]) !== null && _e !== void 0 ? _e : 1);
803
+ megota = Number((_f = tmp[2]) !== null && _f !== void 0 ? _f : 1);
804
+ b = a + 1;
805
+ // b points to after }.
806
+ }
807
+ input = input.substring(b);
808
+ if (input[0] == ")") {
809
+ // )^....<Space>
810
+ a = input.indexOf(" ");
811
+ c = Number(input.substring(2, a)); // Select contents between )^....<Space>
812
+ input = input.substring(a + 1); // c points to after <Space>
813
+ } else {
814
+ c = 1; // There is only spaces, count as <ONE>
815
+ }
816
+ if (arrows == 1 && expans == 1 && megota == 1) {
817
+ if (x.array.length >= 2 && x.array[1].arrow == 1) {
818
+ x.array[1].repeat += c;
819
+ } else {
820
+ x.array.splice(1, 0, newOperator(c, 1, expans, megota));
821
+ }
822
+ } else if (arrows == 2 && expans == 1 && megota == 1) {
823
+ a = x.array.length >= 2 && x.array[1].arrow == 1 ? x.array[1].repeat : 0;
824
+ b = x.array[0].repeat;
825
+ if (b >= 1e10) ++a;
826
+ if (b >= 10) ++a;
827
+ x.array[0].repeat = a;
828
+ if (x.array.length >= 2 && x.array[1].arrow == 1) x.array.splice(1, 1);
829
+ d = x.getOperatorIndex(2);
830
+ if (Number.isInteger(d)) x.array[d].repeat += c;else x.array.splice(Math.ceil(d), 0, newOperator(c, 2, expans, megota));
831
+ } else if (isFinite(arrows)) {
832
+ a = x.getOperator(arrows - 1);
833
+ b = x.getOperator(arrows - 2);
834
+ if (b >= 10) ++a;
835
+ d = x.getOperatorIndex(arrows);
836
+ x.array.splice(1, Math.ceil(d) - 1);
837
+ x.array[0].repeat = a;
838
+ if (Number.isInteger(d)) x.array[1].repeat += c;else x.array.splice(1, 0, newOperator(c, arrows, expans, megota));
839
+ } else {
840
+ x.array.splice(1, 0, newOperator(c, arrows, expans, megota));
841
+ }
842
+ } else {
843
+ break;
844
+ }
845
+ }
846
+ a = input.split(/[Ee]/);
847
+ b = [x.array[0].repeat, 0];
848
+ c = 1;
849
+ for (var _i3 = a.length - 1; _i3 >= 0; --_i3) {
850
+ //The things that are already there
851
+ if (b[0] < MSI_LOG10 && b[1] === 0) {
852
+ b[0] = Math.pow(10, c * b[0]);
853
+ } else if (c == -1) {
854
+ if (b[1] === 0) {
855
+ b[0] = Math.pow(10, c * b[0]);
856
+ } else if (b[1] == 1 && b[0] <= Math.log10(Number.MAX_VALUE)) {
857
+ b[0] = Math.pow(10, c * Math.pow(10, b[0]));
858
+ } else {
859
+ b[0] = 0;
860
+ }
861
+ b[1] = 0;
862
+ } else {
863
+ b[1]++;
864
+ }
865
+ //Multiplying coefficient
866
+ var decimalPointPos = a[_i3].indexOf(".");
867
+ var intPartLen = decimalPointPos == -1 ? a[_i3].length : decimalPointPos;
868
+ if (b[1] === 0) {
869
+ if (intPartLen >= LONG_STRING_MIN_LENGTH) b[0] = Math.log10(b[0]) + log10LongString(a[_i3].substring(0, intPartLen)), b[1] = 1;else if (a[_i3]) b[0] *= Number(a[_i3]);
870
+ } else {
871
+ d = intPartLen >= LONG_STRING_MIN_LENGTH ? log10LongString(a[_i3].substring(0, intPartLen)) : a[_i3] ? Math.log10(Number(a[_i3])) : 0;
872
+ if (b[1] == 1) {
873
+ b[0] += d;
874
+ } else if (b[1] == 2 && b[0] < MSI_LOG10 + Math.log10(d)) {
875
+ b[0] += Math.log10(1 + Math.pow(10, Math.log10(d) - b[0]));
876
+ }
877
+ }
878
+ //Carrying
879
+ if (b[0] < MSI_LOG10 && b[1]) {
880
+ b[0] = Math.pow(10, b[0]);
881
+ b[1]--;
882
+ } else if (b[0] > MSI) {
883
+ b[0] = Math.log10(b[0]);
884
+ b[1]++;
885
+ }
886
+ }
887
+ x.array[0].repeat = b[0];
888
+ if (b[1]) {
889
+ if (x.array.length >= 2 && x.array[1].arrow == 1) x.array[1].repeat += b[1];else x.array.splice(1, 0, newOperator(b[1], 1, 1, 1));
890
+ }
891
+ }
892
+ if (negateIt) x.sign *= -1;
893
+ if (recipIt) x.small = !x.small;
894
+ x.normalize();
895
+ x.normalize();
896
+ return x;
897
+ }
898
+ }, {
899
+ key: "fromObject",
900
+ value: function fromObject(powlikeObject) {
901
+ var obj = new PowiainaNum();
902
+ obj.array = [];
903
+ for (var i = 0; i < powlikeObject.array.length; i++) {
904
+ obj.array[i] = {
905
+ arrow: powlikeObject.array[i].arrow,
906
+ expans: powlikeObject.array[i].expans,
907
+ megota: powlikeObject.array[i].megota,
908
+ repeat: powlikeObject.array[i].repeat,
909
+ valuereplaced: powlikeObject.array[i].valuereplaced
910
+ };
911
+ }
912
+ obj.small = powlikeObject.small;
913
+ obj.sign = powlikeObject.sign;
914
+ obj.layer = powlikeObject.layer;
915
+ return obj;
916
+ }
917
+ }]);
918
+ }();
919
+ PowiainaNum.ZERO = new PowiainaNum({
920
+ array: [{
921
+ arrow: 0,
922
+ expans: 1,
923
+ megota: 1,
924
+ repeat: Infinity
925
+ }],
926
+ small: true,
927
+ layer: 0,
928
+ sign: 0
929
+ });
930
+ PowiainaNum.ONE = new PowiainaNum({
931
+ array: [{
932
+ arrow: 0,
933
+ expans: 1,
934
+ megota: 1,
935
+ repeat: 1
936
+ }],
937
+ small: false,
938
+ layer: 0,
939
+ sign: 1
940
+ });
941
+ PowiainaNum.MSI = new PowiainaNum(MSI);
942
+ PowiainaNum.MSI_REC = function () {
943
+ var obj = new PowiainaNum(MSI);
944
+ obj.small = true;
945
+ return obj;
946
+ }();
947
+ PowiainaNum.E_MSI = new PowiainaNum({
948
+ array: [{
949
+ arrow: 0,
950
+ expans: 1,
951
+ megota: 1,
952
+ repeat: MSI
953
+ }, {
954
+ arrow: 1,
955
+ expans: 1,
956
+ megota: 1,
957
+ repeat: 1
958
+ }],
959
+ small: false,
960
+ layer: 0,
961
+ sign: 1
962
+ });
963
+ PowiainaNum.E_MSI_REC = new PowiainaNum({
964
+ array: [{
965
+ arrow: 0,
966
+ expans: 1,
967
+ megota: 1,
968
+ repeat: MSI
969
+ }, {
970
+ arrow: 1,
971
+ expans: 1,
972
+ megota: 1,
973
+ repeat: 1
974
+ }],
975
+ small: true,
976
+ layer: 0,
977
+ sign: 1
978
+ });
979
+ PowiainaNum.POSITIVE_INFINITY = new PowiainaNum(Infinity);
980
+ PowiainaNum.NEGATIVE_INFINITY = new PowiainaNum(-Infinity);
981
+ PowiainaNum.NaN = new PowiainaNum({
982
+ array: [{
983
+ arrow: 0,
984
+ expans: 1,
985
+ megota: 1,
986
+ repeat: NaN
987
+ }],
988
+ small: false,
989
+ layer: 0,
990
+ sign: 0
991
+ });
992
+ PowiainaNum.maxOps = 100;
993
+
994
+ module.exports = PowiainaNum;