powiaina_num.js 0.2.0-alpha.2.7 → 0.2.0-alpha.2.8

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/README.md CHANGED
@@ -16,7 +16,8 @@ The library exports a class,
16
16
  Create a PowiainaNum.js object like this:
17
17
 
18
18
  ```javascript
19
- import PowiainaNum from "powiaina_num.js";
19
+ import PowiainaNum from "powiaina_num.js"; // static import
20
+ const { default: PowiainaNum } = await import("powiaina_num.js") // dynamic import
20
21
 
21
22
  let a = new PowiainaNum(); // create PN.js number with NaN
22
23
  let b = new PowiainaNum(3); // create PN.js number with number 3
@@ -43,6 +43,7 @@ var MSI = 9007199254740991;
43
43
  var MSI_LOG10 = 15.954589770191003;
44
44
  var MSI_REC = 1.1102230246251568e-16;
45
45
  var LONG_STRING_MIN_LENGTH = 17;
46
+ var EXP_E_REC = 1.444667861009766;
46
47
  var isPowiainaNum = /^[-\+]*(Infinity|NaN|(10(\^+|\{([1-9]\d*|!)(,([1-9]\d*|!))?(,[1-9]\d*)?\})|\(10(\^+|\{([1-9]\d*|!)(,([1-9]\d*|!))?(,[1-9]\d*)?\})\)\^[1-9]\d* )*((\d+(\.\d*)?|\d*\.\d+)?([Ee][-\+]*))*(0|\d+(\.\d*)?|\d*\.\d+))$/;
47
48
  function newOperator(r) {
48
49
  var a = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
@@ -162,7 +163,7 @@ function d_lambertw(z) {
162
163
  var principal = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
163
164
  var w;
164
165
  var ew, wewz, wn;
165
- if (z.isInfinite()) return z;
166
+ if (z.isInfiNaN()) return z;
166
167
  if (principal) {
167
168
  if (z.eq(PowiainaNum.ZERO)) {
168
169
  return PowiainaNum.ZERO.clone();
@@ -294,7 +295,7 @@ var PowiainaNum = /*#__PURE__*/function () {
294
295
  var y = new PowiainaNum(other);
295
296
  // inf * -inf = -inf
296
297
  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();
297
- if (x.isInfinite() && y.eq(PowiainaNum.ZERO) && y.isInfinite() && x.eq(PowiainaNum.ZERO)) return PowiainaNum.NaN.clone();
298
+ if (x.isInfiNaN() && y.eq(PowiainaNum.ZERO) && y.isInfiNaN() && x.eq(PowiainaNum.ZERO)) return PowiainaNum.NaN.clone();
298
299
  if (x.eq(PowiainaNum.NEGATIVE_INFINITY) && y.eq(PowiainaNum.NEGATIVE_INFINITY)) return PowiainaNum.POSITIVE_INFINITY.clone();
299
300
  // inf & nan check
300
301
  if (!x.isFinite()) return x.clone();
@@ -393,6 +394,127 @@ var PowiainaNum = /*#__PURE__*/function () {
393
394
  value: function cbrt() {
394
395
  return this.root(3);
395
396
  }
397
+ }, {
398
+ key: "tetrate",
399
+ value:
400
+ // Code from ExpantaNum.js
401
+ function tetrate(other2) {
402
+ var payload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
403
+ var t = this.clone();
404
+ var other = new PowiainaNum(other2);
405
+ var payl = new PowiainaNum(payload);
406
+ if (t.isNaN() || other.isNaN() || payl.isNaN()) return PowiainaNum.NaN.clone();
407
+ if (payl.neq(PowiainaNum.ONE)) other = other.add(payl.slog(t));
408
+ var negln;
409
+ if (other.isInfi() && other.sign > 0) {
410
+ if (t.gte(EXP_E_REC)) return PowiainaNum.POSITIVE_INFINITY.clone();
411
+ negln = this.log().neg();
412
+ return negln.lambertw().div(negln);
413
+ }
414
+ if (other.lte(-2)) return PowiainaNum.NaN.clone();
415
+ if (t.isZero()) {
416
+ if (other.isZero()) return PowiainaNum.NaN.clone();
417
+ if (other.gte(MSI / 2) || other.toNumber() % 2 == 0) return PowiainaNum.ZERO.clone();
418
+ return PowiainaNum.ONE.clone();
419
+ }
420
+ if (t.eq(PowiainaNum.ONE)) {
421
+ if (other.eq(PowiainaNum.ONE.neg())) return PowiainaNum.NaN.clone();
422
+ return PowiainaNum.ONE.clone();
423
+ }
424
+ if (other.eq(PowiainaNum.ONE.neg())) return PowiainaNum.ZERO.clone();
425
+ if (other.eq(PowiainaNum.ZERO)) return PowiainaNum.ONE.clone();
426
+ if (other.eq(PowiainaNum.ONE)) return t;
427
+ if (other.eq(2)) return t.pow(t);
428
+ if (t.eq(2)) {
429
+ if (other.eq(3)) return PowiainaNum.fromNumber(16);
430
+ if (other.eq(4)) return PowiainaNum.fromNumber(65536);
431
+ }
432
+ var m = t.max(other);
433
+ if (m.gt(PowiainaNum.PENTATED_MSI)) return m;
434
+ if (m.gt(PowiainaNum.TETRATED_MSI) || other.gt(MSI)) {
435
+ if (this.lt(EXP_E_REC)) {
436
+ negln = t.ln().neg();
437
+ return negln.lambertw().div(negln);
438
+ }
439
+ var j = t.slog(10).add(other);
440
+ j.setOperator(j.getOperator(2) + 1, 2);
441
+ j.normalize();
442
+ return j;
443
+ }
444
+ var y = other.toNumber();
445
+ var f = Math.floor(y);
446
+ var r = t.pow(y - f);
447
+ var l = PowiainaNum.NaN;
448
+ var i = 0;
449
+ for (var w = PowiainaNum.E_MSI.clone(); f !== 0 && r.lt(w) && i < 100; ++i) {
450
+ if (f > 0) {
451
+ r = t.pow(r);
452
+ if (l.eq(r)) {
453
+ f = 0;
454
+ break;
455
+ }
456
+ l = r;
457
+ --f;
458
+ } else {
459
+ r = r.log(t);
460
+ if (l.eq(r)) {
461
+ f = 0;
462
+ break;
463
+ }
464
+ l = r;
465
+ ++f;
466
+ }
467
+ }
468
+ if (i == 100 || this.lt(EXP_E_REC)) f = 0;
469
+ r.setOperator(r.getOperator(1) + f, 1);
470
+ r.normalize();
471
+ return r;
472
+ }
473
+ // Code from ExpantaNum.js
474
+ }, {
475
+ key: "slog",
476
+ value: function slog() {
477
+ var base = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10;
478
+ var x = this.clone();
479
+ var b = new PowiainaNum(base);
480
+ if (x.isInfiNaN()) return x;
481
+ if (b.isNaN()) return b;
482
+ if (b.isInfi()) return PowiainaNum.ZERO.clone();
483
+ if (x.isZero()) return PowiainaNum.ONE.clone();
484
+ if (x.eq(PowiainaNum.ONE)) return PowiainaNum.ZERO.clone();
485
+ if (x.eq(b)) return PowiainaNum.ONE.clone();
486
+ if (b.lt(EXP_E_REC)) {
487
+ var a = b.tetrate(Infinity);
488
+ if (x.eq(a)) return PowiainaNum.POSITIVE_INFINITY.clone();
489
+ if (x.gt(a)) return PowiainaNum.NaN.clone();
490
+ }
491
+ if (x.max(b).gt(PowiainaNum.PENTATED_MSI)) {
492
+ if (x.gt(b)) return x;
493
+ return PowiainaNum.ZERO.clone();
494
+ }
495
+ if (x.max(b).gt(PowiainaNum.TETRATED_MSI)) {
496
+ if (x.gt(b)) {
497
+ x.setOperator(x.getOperator(2) - 1, 2);
498
+ x.normalize();
499
+ return x.sub(x.getOperator(1));
500
+ }
501
+ }
502
+ if (x.lt(PowiainaNum.ZERO.clone())) return b.pow(x).sub(2);
503
+ var r = 0;
504
+ var t = x.getOperator(1) - b.getOperator(1);
505
+ if (t > 3) {
506
+ var l = t - 3;
507
+ r += l;
508
+ x.setOperator(x.getOperator(1) - l, 1);
509
+ }
510
+ for (var i = 0; i < 100; ++i) {
511
+ if (x.lte(PowiainaNum.ONE)) return new PowiainaNum(r + x.toNumber());
512
+ ++r;
513
+ x = PowiainaNum.log(x, base);
514
+ }
515
+ console.warn("Failed to converage");
516
+ return PowiainaNum.NaN.clone();
517
+ }
396
518
  }, {
397
519
  key: "abs",
398
520
  value: function abs() {
@@ -424,6 +546,11 @@ var PowiainaNum = /*#__PURE__*/function () {
424
546
  var other = new PowiainaNum(base);
425
547
  return this.log10().div(other.log10());
426
548
  }
549
+ }, {
550
+ key: "ln",
551
+ value: function ln() {
552
+ return this.log();
553
+ }
427
554
  }, {
428
555
  key: "exp",
429
556
  value: function exp() {
@@ -630,6 +757,18 @@ var PowiainaNum = /*#__PURE__*/function () {
630
757
  r.sign = this.sign;
631
758
  return r;
632
759
  }
760
+ /**
761
+ * Work like `Math.trunc`,
762
+ *
763
+ * if `this > 0`, return `floor(this)`
764
+ *
765
+ * if `this < 0`, return `ceil(this)`
766
+ *
767
+ * @example
768
+ * new PowiainaNum(3.3).trunc() == new PowiainaNum(3)
769
+ * new PowiainaNum(-1.114514).trunc() == new PowiainaNum(-1)
770
+ * @returns
771
+ */
633
772
  }, {
634
773
  key: "trunc",
635
774
  value: function trunc() {
@@ -736,9 +875,14 @@ var PowiainaNum = /*#__PURE__*/function () {
736
875
  return Boolean(this.small || isFinite(this.getOperator(0))) && !this.isNaN();
737
876
  })
738
877
  }, {
739
- key: "isInfinite",
740
- value: function isInfinite() {
741
- return Boolean(!this.small && !isFinite(this.getOperator(0))) || this.isNaN();
878
+ key: "isInfi",
879
+ value: function isInfi() {
880
+ return this.rec().isZero();
881
+ }
882
+ }, {
883
+ key: "isInfiNaN",
884
+ value: function isInfiNaN() {
885
+ return this.isInfi() || this.isNaN();
742
886
  }
743
887
  }, {
744
888
  key: "isInt",
@@ -770,6 +914,7 @@ var PowiainaNum = /*#__PURE__*/function () {
770
914
  var renormalize = true;
771
915
  var x = this;
772
916
  for (var _i = 0; _i < this.array.length; _i++) {
917
+ // Check what is infinity
773
918
  if (this.array[_i].repeat == Infinity) {
774
919
  this.array = [{
775
920
  arrow: 0,
@@ -805,36 +950,42 @@ var PowiainaNum = /*#__PURE__*/function () {
805
950
  if (!Number.isInteger(e.expans)) e.expans = Math.floor(e.expans);
806
951
  if (!Number.isInteger(e.megota)) e.megota = Math.floor(e.megota);
807
952
  }
953
+ if (!x.array.length) {
954
+ x.small = !x.small;
955
+ x.array = [newOperator(Infinity)]; // if no array set zero
956
+ }
808
957
  do {
809
958
  renormalize = false;
959
+ // Sort arrays.
810
960
  this.array.sort(function (a, b) {
811
961
  return compareTuples([a.megota, a.expans, a.arrow], [b.megota, b.expans, b.arrow]);
812
962
  });
963
+ for (i = 1; i < x.array.length - 1; ++i) {
964
+ 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) {
965
+ // same array's merge
966
+ x.array[i].repeat += x.array[i + 1].repeat;
967
+ x.array.splice(i + 1, 1);
968
+ --i;
969
+ renormalize = true;
970
+ }
971
+ }
813
972
  for (i = 1; i < x.array.length; ++i) {
814
- // check 0 repeat count
973
+ // If there is a 0 repeat operator, remove it.
815
974
  if (x.array[i].arrow !== 0 && (x.array[i].repeat === 0 || x.array[i].repeat === null || x.array[i].repeat === undefined)) {
816
975
  x.array.splice(i, 1);
817
976
  --i;
818
977
  continue;
819
978
  }
820
- // check arrow 0 and brace count >=2
979
+ // If there is a operator which arrow 0 and brace count >=2
980
+ // replace it as arrow replacement operaotr
821
981
  if (x.array[i].arrow == 0 && x.array[i].expans >= 2) {
822
982
  x.array[i].arrow = Infinity;
823
983
  x.array[i].valuereplaced = 0;
824
984
  x.array[i].expans = x.array[i].expans - 1;
825
985
  }
826
986
  }
827
- for (var _i2 = 1; _i2 < this.array.length; _i2++) {
828
- if (this.array[_i2].repeat == 0) {
829
- this.array.splice(_i2, 1);
830
- _i2--;
831
- }
832
- }
833
- if (!x.array.length) {
834
- x.small = !x.small;
835
- x.array = [newOperator(Infinity)]; // if no array set zero
836
- }
837
987
  if (x.array.length > PowiainaNum.maxOps) x.array.splice(1, x.array.length - PowiainaNum.maxOps); // max operators check
988
+ // for any 10^a but a >log10(MSI), replace to regular 10^a
838
989
  if (this.getOperator(1) >= 1 && this.getOperator(0) < MSI_LOG10) {
839
990
  this.setOperator(this.getOperator(1) - 1, 1);
840
991
  this.setOperator(Math.pow(10, this.getOperator(0)), 0);
@@ -845,13 +996,15 @@ var PowiainaNum = /*#__PURE__*/function () {
845
996
  this.setOperator(Math.log10(this.getOperator(0)), 0);
846
997
  renormalize = true;
847
998
  }
999
+ // for a<1, turn into reciprocate
848
1000
  if (this.array.length == 1 && this.array[0].repeat < 1) {
849
1001
  this.array[0].repeat = 1 / this.array[0].repeat;
850
1002
  this.small = !this.small;
851
1003
  renormalize = true;
852
1004
  }
853
1005
  while (x.array.length >= 2 && x.array[0].repeat == 1 && x.array[1].repeat) {
854
- // [1, [R=1, A=sth, E=sth, M=sth]]
1006
+ // for any 10{X}10{X} 1, turn into 10{x}10
1007
+ // [1, [R=sth, A=sth, E=sth, M=sth]]
855
1008
  if (x.array[1].repeat > 1) {
856
1009
  x.array[1].repeat--;
857
1010
  } else {
@@ -861,19 +1014,18 @@ var PowiainaNum = /*#__PURE__*/function () {
861
1014
  renormalize = true;
862
1015
  }
863
1016
  if (x.array.length >= 2 && x.array[0].repeat < MSI && x.array[1].arrow >= 2 && x.array[1].repeat == 1) {
1017
+ // for any 10{A sample=2}1e9, turn into (10{A-1})^1e9-1 10
864
1018
  // [1e9, [R=1, A=2, sth, sth]]
865
1019
  x.array.splice(1, 1, newOperator(x.array[0].repeat, x.array[1].arrow - 1, x.array[1].expans, x.array[1].megota));
866
1020
  x.array[0].repeat = 10;
867
1021
  renormalize = true;
868
1022
  }
869
- for (i = 1; i < x.array.length - 1; ++i) {
870
- 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) {
871
- // same array's merge
872
- x.array[i].repeat += x.array[i + 1].repeat;
873
- x.array.splice(i + 1, 1);
874
- --i;
875
- renormalize = true;
876
- }
1023
+ // for any (10{A=2})^1e16 10, turn into (10{A+1}) 1e16
1024
+ if (x.array.length >= 2 && x.array[1].repeat > MSI) {
1025
+ x.array[1].arrow++;
1026
+ x.array[0].repeat = x.array[1].repeat;
1027
+ x.array[1].repeat = 1;
1028
+ renormalize = true;
877
1029
  }
878
1030
  } while (renormalize);
879
1031
  return this;
@@ -981,7 +1133,7 @@ var PowiainaNum = /*#__PURE__*/function () {
981
1133
  if (this.eq(PowiainaNum.ZERO)) return "0";
982
1134
  return "/".concat(this.rec().toString());
983
1135
  }
984
- if (this.isInfinite()) return "Infinity";
1136
+ if (this.isInfi()) return "Infinity";
985
1137
  // O^a (10{arrow,expans,megota})^repeation base
986
1138
  var res = "";
987
1139
  for (var i = this.array.length - 1; i >= 0; i--) {
@@ -1064,6 +1216,12 @@ var PowiainaNum = /*#__PURE__*/function () {
1064
1216
  value: function cbrt(t) {
1065
1217
  return new PowiainaNum(t).cbrt();
1066
1218
  }
1219
+ }, {
1220
+ key: "tetrate",
1221
+ value: function tetrate(t, other2) {
1222
+ var payload = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
1223
+ return new PowiainaNum(t).tetrate(other2, payload);
1224
+ }
1067
1225
  }, {
1068
1226
  key: "log10",
1069
1227
  value: function log10(t) {
@@ -1100,26 +1258,37 @@ var PowiainaNum = /*#__PURE__*/function () {
1100
1258
  }, {
1101
1259
  key: "tetrate_10",
1102
1260
  value: function tetrate_10(other2) {
1103
- var other = new PowiainaNum(other2);
1104
- var height_int = other.trunc().toNumber();
1105
- var height_frac = other.sub(height_int).toNumber();
1106
- if (other.gt(PowiainaNum.PENTATED_MSI)) return other.clone();else if (other.gt(PowiainaNum.MSI)) {
1107
- other.setOperator(other.getOperator(2) + 1, 2);
1108
- } else if (other.lt(-2)) return PowiainaNum.NaN.clone();else if (other.lt(-1)) {
1261
+ return PowiainaNum.fromNumber(10).tetrate(other2);
1262
+ /*
1263
+ const other = new PowiainaNum(other2);
1264
+ const height_int = other.trunc().toNumber();
1265
+ const height_frac = other.sub(height_int).toNumber();
1266
+ if (other.gt(PowiainaNum.PENTATED_MSI))
1267
+ return other.clone();
1268
+ else if (other.gt(PowiainaNum.MSI)) {
1269
+ other.setOperator(other.getOperator(2)+1, 2);
1270
+ } else if (other.lt(-2))
1271
+ return PowiainaNum.NaN.clone();
1272
+ else if (other.lt(-1)) {
1109
1273
  return other.add(2).log10();
1110
- } else if (other.lt(0)) {
1274
+ }
1275
+ else if (other.lt(0)) {
1111
1276
  return other.add(1);
1112
- } else if (other.lt(1)) {
1113
- return other.pow10(); // 10^x
1114
- } else if (height_int == 1) return PowiainaNum.pow(10, PowiainaNum.pow(10, height_frac));else if (height_int == 2) return PowiainaNum.pow(10, PowiainaNum.pow(10, PowiainaNum.pow(10, height_frac)));else {
1115
- var remain = height_int - 2;
1116
- var a = PowiainaNum.pow(10, PowiainaNum.pow(10, PowiainaNum.pow(10, height_frac)));
1117
- a.setOperator(a.getOperator(1) + remain, 1);
1277
+ }else if (other.lt(1)) {
1278
+ return other.pow10() // 10^x
1279
+ }else if (height_int==1)
1280
+ return PowiainaNum.pow(10,PowiainaNum.pow(10,height_frac));
1281
+ else if (height_int==2)
1282
+ return PowiainaNum.pow(10,PowiainaNum.pow(10,PowiainaNum.pow(10,height_frac)));
1283
+ else {
1284
+ const remain = height_int-2;
1285
+ let a = PowiainaNum.pow(10,PowiainaNum.pow(10,PowiainaNum.pow(10,height_frac)));
1286
+ a.setOperator(a.getOperator(1)+remain, 1);
1118
1287
  return a;
1119
1288
  }
1120
1289
  // 1--2, 10-<1e10, 10^10^0->1
1121
1290
  // 2--3, 1e10-<e1e10, 10^10^10^0->1
1122
- return PowiainaNum.NaN.clone();
1291
+ return PowiainaNum.NaN.clone();*/
1123
1292
  }
1124
1293
  }, {
1125
1294
  key: "isNaN",
@@ -1259,7 +1428,7 @@ var PowiainaNum = /*#__PURE__*/function () {
1259
1428
  a = input.split(/[Ee]/);
1260
1429
  b = [x.array[0].repeat, 0];
1261
1430
  c = 1;
1262
- for (var _i3 = a.length - 1; _i3 >= 0; --_i3) {
1431
+ for (var _i2 = a.length - 1; _i2 >= 0; --_i2) {
1263
1432
  //The things that are already there
1264
1433
  if (b[0] < MSI_LOG10 && b[1] === 0) {
1265
1434
  b[0] = Math.pow(10, c * b[0]);
@@ -1276,12 +1445,12 @@ var PowiainaNum = /*#__PURE__*/function () {
1276
1445
  b[1]++;
1277
1446
  }
1278
1447
  //Multiplying coefficient
1279
- var decimalPointPos = a[_i3].indexOf(".");
1280
- var intPartLen = decimalPointPos == -1 ? a[_i3].length : decimalPointPos;
1448
+ var decimalPointPos = a[_i2].indexOf(".");
1449
+ var intPartLen = decimalPointPos == -1 ? a[_i2].length : decimalPointPos;
1281
1450
  if (b[1] === 0) {
1282
- 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]);
1451
+ if (intPartLen >= LONG_STRING_MIN_LENGTH) b[0] = Math.log10(b[0]) + log10LongString(a[_i2].substring(0, intPartLen)), b[1] = 1;else if (a[_i2]) b[0] *= Number(a[_i2]);
1283
1452
  } else {
1284
- d = intPartLen >= LONG_STRING_MIN_LENGTH ? log10LongString(a[_i3].substring(0, intPartLen)) : a[_i3] ? Math.log10(Number(a[_i3])) : 0;
1453
+ d = intPartLen >= LONG_STRING_MIN_LENGTH ? log10LongString(a[_i2].substring(0, intPartLen)) : a[_i2] ? Math.log10(Number(a[_i2])) : 0;
1285
1454
  if (b[1] == 1) {
1286
1455
  b[0] += d;
1287
1456
  } else if (b[1] == 2 && b[0] < MSI_LOG10 + Math.log10(d)) {