powiaina_num.js 0.1.12 → 0.2.0-alpha.2.1

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