-
Notifications
You must be signed in to change notification settings - Fork 46
/
break_eternity.esm.js
4957 lines (4950 loc) · 199 KB
/
break_eternity.esm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
Object.defineProperty(Constructor, "prototype", {
writable: false
});
return Constructor;
}
function _toPrimitive(input, hint) {
if (typeof input !== "object" || input === null) return input;
var prim = input[Symbol.toPrimitive];
if (prim !== undefined) {
var res = prim.call(input, hint || "default");
if (typeof res !== "object") return res;
throw new TypeError("@@toPrimitive must return a primitive value.");
}
return (hint === "string" ? String : Number)(input);
}
function _toPropertyKey(arg) {
var key = _toPrimitive(arg, "string");
return typeof key === "symbol" ? key : String(key);
}
/**
* A LRU cache intended for caching pure functions.
*/
var LRUCache = /*#__PURE__*/function () {
/**
* @param maxSize The maximum size for this cache. We recommend setting this
* to be one less than a power of 2, as most hashtables - including V8's
* Object hashtable (https://crsrc.org/c/v8/src/objects/ordered-hash-table.cc)
* - uses powers of two for hashtable sizes. It can't exactly be a power of
* two, as a .set() call could temporarily set the size of the map to be
* maxSize + 1.
*/
function LRUCache(maxSize) {
_classCallCheck(this, LRUCache);
this.map = new Map();
// Invariant: Exactly one of the below is true before and after calling a
// LRUCache method:
// - first and last are both undefined, and map.size() is 0.
// - first and last are the same object, and map.size() is 1.
// - first and last are different objects, and map.size() is greater than 1.
this.first = undefined;
this.last = undefined;
this.maxSize = maxSize;
}
_createClass(LRUCache, [{
key: "size",
get: function get() {
return this.map.size;
}
/**
* Gets the specified key from the cache, or undefined if it is not in the
* cache.
* @param key The key to get.
* @returns The cached value, or undefined if key is not in the cache.
*/
}, {
key: "get",
value: function get(key) {
var node = this.map.get(key);
if (node === undefined) {
return undefined;
}
// It is guaranteed that there is at least one item in the cache.
// Therefore, first and last are guaranteed to be a ListNode...
// but if there is only one item, they might be the same.
// Update the order of the list to make this node the first node in the
// list.
// This isn't needed if this node is already the first node in the list.
if (node !== this.first) {
// As this node is DIFFERENT from the first node, it is guaranteed that
// there are at least two items in the cache.
// However, this node could possibly be the last item.
if (node === this.last) {
// This node IS the last node.
this.last = node.prev;
// From the invariants, there must be at least two items in the cache,
// so node - which is the original "last node" - must have a defined
// previous node. Therefore, this.last - set above - must be defined
// here.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.last.next = undefined;
} else {
// This node is somewhere in the middle of the list, so there must be at
// least THREE items in the list, and this node's prev and next must be
// defined here.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
node.prev.next = node.next;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
node.next.prev = node.prev;
}
node.next = this.first;
// From the invariants, there must be at least two items in the cache, so
// this.first must be a valid ListNode.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.first.prev = node;
this.first = node;
}
return node.value;
}
/**
* Sets an entry in the cache.
*
* @param key The key of the entry.
* @param value The value of the entry.
* @throws Error, if the map already contains the key.
*/
}, {
key: "set",
value: function set(key, value) {
// Ensure that this.maxSize >= 1.
if (this.maxSize < 1) {
return;
}
if (this.map.has(key)) {
throw new Error("Cannot update existing keys in the cache");
}
var node = new ListNode(key, value);
// Move node to the front of the list.
if (this.first === undefined) {
// If the first is undefined, the last is undefined too.
// Therefore, this cache has no items in it.
this.first = node;
this.last = node;
} else {
// This cache has at least one item in it.
node.next = this.first;
this.first.prev = node;
this.first = node;
}
this.map.set(key, node);
while (this.map.size > this.maxSize) {
// We are guaranteed that this.maxSize >= 1,
// so this.map.size is guaranteed to be >= 2,
// so this.first and this.last must be different valid ListNodes,
// and this.last.prev must also be a valid ListNode (possibly this.first).
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
var last = this.last;
this.map["delete"](last.key);
this.last = last.prev;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.last.next = undefined;
}
}
}]);
return LRUCache;
}();
/**
* A node in a doubly linked list.
*/
var ListNode = /*#__PURE__*/_createClass(function ListNode(key, value) {
_classCallCheck(this, ListNode);
this.next = undefined;
this.prev = undefined;
this.key = key;
this.value = value;
});
var MAX_SIGNIFICANT_DIGITS = 17; //Maximum number of digits of precision to assume in Number
var EXP_LIMIT = 9e15; //If we're ABOVE this value, increase a layer. (9e15 is close to the largest integer that can fit in a Number.)
var LAYER_DOWN = Math.log10(9e15);
var FIRST_NEG_LAYER = 1 / 9e15; //At layer 0, smaller non-zero numbers than this become layer 1 numbers with negative mag. After that the pattern continues as normal.
var NUMBER_EXP_MAX = 308; //The largest exponent that can appear in a Number, though not all mantissas are valid here.
var NUMBER_EXP_MIN = -324; //The smallest exponent that can appear in a Number, though not all mantissas are valid here.
var MAX_ES_IN_A_ROW = 5; //For default toString behaviour, when to swap from eee... to (e^n) syntax.
var DEFAULT_FROM_STRING_CACHE_SIZE = (1 << 10) - 1; // The default size of the LRU cache used to cache Decimal.fromString.
var powerOf10 = function () {
// We need this lookup table because Math.pow(10, exponent)
// when exponent's absolute value is large is slightly inaccurate.
// You can fix it with the power of math... or just make a lookup table.
// Faster AND simpler
var powersOf10 = [];
for (var i = NUMBER_EXP_MIN + 1; i <= NUMBER_EXP_MAX; i++) {
powersOf10.push(Number("1e" + i));
}
var indexOf0InPowersOf10 = 323;
return function (power) {
return powersOf10[power + indexOf0InPowersOf10];
};
}();
//tetration/slog to real height stuff
//background info and tables of values for critical functions taken here: https://github.com/Patashu/break_eternity.js/issues/22
var critical_headers = [2, Math.E, 3, 4, 5, 6, 7, 8, 9, 10];
var critical_tetr_values = [[
// Base 2 (using http://myweb.astate.edu/wpaulsen/tetcalc/tetcalc.html )
1, 1.0891180521811202527, 1.1789767925673958433, 1.2701455431742086633, 1.3632090180450091941, 1.4587818160364217007, 1.5575237916251418333, 1.6601571006859253673, 1.7674858188369780435, 1.8804192098842727359, 2], [
// Base E (using http://myweb.astate.edu/wpaulsen/tetcalc/tetcalc.html )
1, 1.1121114330934078681, 1.2310389249316089299, 1.3583836963111376089, 1.4960519303993531879, 1.6463542337511945810, 1.8121385357018724464, 1.9969713246183068478, 2.2053895545527544330, 2.4432574483385252544, Math.E //1.0
], [
// Base 3
1, 1.1187738849693603, 1.2464963939368214, 1.38527004705667, 1.5376664685821402, 1.7068895236551784, 1.897001227148399, 2.1132403089001035, 2.362480153784171, 2.6539010333870774, 3], [
// Base 4
1, 1.1367350847096405, 1.2889510672956703, 1.4606478703324786, 1.6570295196661111, 1.8850062585672889, 2.1539465047453485, 2.476829779693097, 2.872061932789197, 3.3664204535587183, 4], [
// Base 5
1, 1.1494592900767588, 1.319708228183931, 1.5166291280087583, 1.748171114438024, 2.0253263297298045, 2.3636668498288547, 2.7858359149579424, 3.3257226212448145, 4.035730287722532, 5], [
// Base 6
1, 1.159225940787673, 1.343712473580932, 1.5611293155111927, 1.8221199554561318, 2.14183924486326, 2.542468319282638, 3.0574682501653316, 3.7390572020926873, 4.6719550537360774, 6], [
// Base 7
1, 1.1670905356972596, 1.3632807444991446, 1.5979222279405536, 1.8842640123816674, 2.2416069644878687, 2.69893426559423, 3.3012632110403577, 4.121250340630164, 5.281493033448316, 7], [
// Base 8
1, 1.1736630594087796, 1.379783782386201, 1.6292821855668218, 1.9378971836180754, 2.3289975651071977, 2.8384347394720835, 3.5232708454565906, 4.478242031114584, 5.868592169644505, 8], [
// Base 9
1, 1.1793017514670474, 1.394054150657457, 1.65664127441059, 1.985170999970283, 2.4069682290577457, 2.9647310119960752, 3.7278665320924946, 4.814462547283592, 6.436522247411611, 9], [
// Base 10 (using http://myweb.astate.edu/wpaulsen/tetcalc/tetcalc.html )
1, 1.1840100246247336579, 1.4061375836156954169, 1.6802272208863963918, 2.026757028388618927, 2.4770056063449647580, 3.0805252717554819987, 3.9191964192627283911, 5.1351528408331864230, 6.9899611795347148455, 10]];
var critical_slog_values = [[
// Base 2
-1, -0.9194161097107025, -0.8335625019330468, -0.7425599821143978, -0.6466611521029437, -0.5462617907227869, -0.4419033816638769, -0.3342645487554494, -0.224140440909962, -0.11241087890006762, 0], [
// Base E
-1, -0.90603157029014, -0.80786507256596, -0.7064666939634, -0.60294836853664, -0.49849837513117, -0.39430303318768, -0.29147201034755, -0.19097820800866, -0.09361896280296, 0 //1.0
], [
// Base 3
-1, -0.9021579584316141, -0.8005762598234203, -0.6964780623319391, -0.5911906810998454, -0.486050182576545, -0.3823089430815083, -0.28106046722897615, -0.1831906535795894, -0.08935809204418144, 0], [
// Base 4
-1, -0.8917227442365535, -0.781258746326964, -0.6705130326902455, -0.5612813129406509, -0.4551067709033134, -0.35319256652135966, -0.2563741554088552, -0.1651412821106526, -0.0796919581982668, 0], [
// Base 5
-1, -0.8843387974366064, -0.7678744063886243, -0.6529563724510552, -0.5415870994657841, -0.4352842206588936, -0.33504449124791424, -0.24138853420685147, -0.15445285440944467, -0.07409659641336663, 0], [
// Base 6
-1, -0.8786709358426346, -0.7577735191184886, -0.6399546189952064, -0.527284921869926, -0.4211627631006314, -0.3223479611761232, -0.23107655627789858, -0.1472057700818259, -0.07035171210706326, 0], [
// Base 7
-1, -0.8740862815291583, -0.7497032990976209, -0.6297119746181752, -0.5161838335958787, -0.41036238255751956, -0.31277212146489963, -0.2233976621705518, -0.1418697367979619, -0.06762117662323441, 0], [
// Base 8
-1, -0.8702632331800649, -0.7430366914122081, -0.6213373075161548, -0.5072025698095242, -0.40171437727184167, -0.30517930701410456, -0.21736343968190863, -0.137710238299109, -0.06550774483471955, 0], [
// Base 9
-1, -0.8670016295947213, -0.7373984232432306, -0.6143173985094293, -0.49973884395492807, -0.394584953527678, -0.2989649949848695, -0.21245647317021688, -0.13434688362382652, -0.0638072667348083, 0], [
// Base 10
-1, -0.8641642839543857, -0.732534623168535, -0.6083127477059322, -0.4934049257184696, -0.3885773075899922, -0.29376029055315767, -0.2083678561173622, -0.13155653399373268, -0.062401588652553186, 0]];
var D = function D(value) {
return Decimal.fromValue_noAlloc(value);
};
var FC = function FC(sign, layer, mag) {
return Decimal.fromComponents(sign, layer, mag);
};
var FC_NN = function FC_NN(sign, layer, mag) {
return Decimal.fromComponents_noNormalize(sign, layer, mag);
};
var decimalPlaces = function decimalPlaces(value, places) {
var len = places + 1;
var numDigits = Math.ceil(Math.log10(Math.abs(value)));
var rounded = Math.round(value * Math.pow(10, len - numDigits)) * Math.pow(10, numDigits - len);
return parseFloat(rounded.toFixed(Math.max(len - numDigits, 0)));
};
var f_maglog10 = function f_maglog10(n) {
return Math.sign(n) * Math.log10(Math.abs(n));
};
//from HyperCalc source code
var f_gamma = function f_gamma(n) {
if (!isFinite(n)) {
return n;
}
if (n < -50) {
if (n === Math.trunc(n)) {
return Number.NEGATIVE_INFINITY;
}
return 0;
}
var scal1 = 1;
while (n < 10) {
scal1 = scal1 * n;
++n;
}
n -= 1;
var l = 0.9189385332046727; //0.5*Math.log(2*Math.PI)
l = l + (n + 0.5) * Math.log(n);
l = l - n;
var n2 = n * n;
var np = n;
l = l + 1 / (12 * np);
np = np * n2;
l = l - 1 / (360 * np);
np = np * n2;
l = l + 1 / (1260 * np);
np = np * n2;
l = l - 1 / (1680 * np);
np = np * n2;
l = l + 1 / (1188 * np);
np = np * n2;
l = l - 691 / (360360 * np);
np = np * n2;
l = l + 7 / (1092 * np);
np = np * n2;
l = l - 3617 / (122400 * np);
return Math.exp(l) / scal1;
};
var _EXPN1 = 0.36787944117144232159553; // exp(-1)
var OMEGA = 0.56714329040978387299997; // W(1, 0)
//from https://math.stackexchange.com/a/465183
// The evaluation can become inaccurate very close to the branch point
// Evaluates W(x, 0) if principal is true, W(x, -1) if principal is false
var f_lambertw = function f_lambertw(z) {
var tol = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1e-10;
var principal = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
var w;
var wn;
if (!Number.isFinite(z)) {
return z;
}
if (principal) {
if (z === 0) {
return z;
}
if (z === 1) {
return OMEGA;
}
if (z < 10) {
w = 0;
} else {
w = Math.log(z) - Math.log(Math.log(z));
}
} else {
if (z === 0) return -Infinity;
if (z <= -0.1) {
w = -2;
} else {
w = Math.log(-z) - Math.log(-Math.log(-z));
}
}
for (var i = 0; i < 100; ++i) {
wn = (z * Math.exp(-w) + w * w) / (w + 1);
if (Math.abs(wn - w) < tol * Math.abs(wn)) {
return wn;
} else {
w = wn;
}
}
throw Error("Iteration failed to converge: ".concat(z.toString()));
//return Number.NaN;
};
//from https://github.com/scipy/scipy/blob/8dba340293fe20e62e173bdf2c10ae208286692f/scipy/special/lambertw.pxd
// The evaluation can become inaccurate very close to the branch point
// at ``-1/e``. In some corner cases, `lambertw` might currently
// fail to converge, or can end up on the wrong branch.
// Evaluates W(x, 0) if principal is true, W(x, -1) if principal is false
function d_lambertw(z) {
var tol = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1e-10;
var principal = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
var w;
var ew, wewz, wn;
if (!Number.isFinite(z.mag)) {
return new Decimal(z);
}
if (principal) {
if (z.eq(Decimal.dZero)) {
return FC_NN(0, 0, 0);
}
if (z.eq(Decimal.dOne)) {
//Split out this case because the asymptotic series blows up
return Decimal.fromNumber(OMEGA);
}
//Get an initial guess for Halley's method
w = Decimal.ln(z);
} else {
if (z.eq(Decimal.dZero)) {
return new Decimal(Decimal.dNegInf);
}
//Get an initial guess for Halley's method
w = Decimal.ln(z.neg());
}
//Halley's method; see 5.9 in [1]
for (var i = 0; i < 100; ++i) {
ew = w.neg().exp();
wewz = w.sub(z.mul(ew));
wn = w.sub(wewz.div(w.add(1).sub(w.add(2).mul(wewz).div(Decimal.mul(2, w).add(2)))));
if (Decimal.abs(wn.sub(w)).lt(Decimal.abs(wn).mul(tol))) {
return wn;
} else {
w = wn;
}
}
throw Error("Iteration failed to converge: ".concat(z.toString()));
//return Decimal.dNaN;
}
/**
* The value of the Decimal is sign * 10^10^10...^mag, with (layer) 10s. If the layer is not 0, then negative mag means it's the reciprocal of the corresponding number with positive mag.
*/
var Decimal = /*#__PURE__*/function () {
function Decimal(value) {
_classCallCheck(this, Decimal);
this.sign = 0;
this.mag = 0;
this.layer = 0;
if (value instanceof Decimal) {
this.fromDecimal(value);
} else if (typeof value === "number") {
this.fromNumber(value);
} else if (typeof value === "string") {
this.fromString(value);
}
}
_createClass(Decimal, [{
key: "m",
get: function get() {
if (this.sign === 0) {
return 0;
} else if (this.layer === 0) {
var exp = Math.floor(Math.log10(this.mag));
//handle special case 5e-324
var man;
if (this.mag === 5e-324) {
man = 5;
} else {
man = this.mag / powerOf10(exp);
}
return this.sign * man;
} else if (this.layer === 1) {
var residue = this.mag - Math.floor(this.mag);
return this.sign * Math.pow(10, residue);
} else {
//mantissa stops being relevant past 1e9e15 / ee15.954
return this.sign;
}
},
set: function set(value) {
if (this.layer <= 2) {
this.fromMantissaExponent(value, this.e);
} else {
//don't even pretend mantissa is meaningful
this.sign = Math.sign(value);
if (this.sign === 0) {
this.layer = 0;
this.exponent = 0;
}
}
}
}, {
key: "e",
get: function get() {
if (this.sign === 0) {
return 0;
} else if (this.layer === 0) {
return Math.floor(Math.log10(this.mag));
} else if (this.layer === 1) {
return Math.floor(this.mag);
} else if (this.layer === 2) {
return Math.floor(Math.sign(this.mag) * Math.pow(10, Math.abs(this.mag)));
} else {
return this.mag * Number.POSITIVE_INFINITY;
}
},
set: function set(value) {
this.fromMantissaExponent(this.m, value);
}
}, {
key: "s",
get: function get() {
return this.sign;
},
set: function set(value) {
if (value === 0) {
this.sign = 0;
this.layer = 0;
this.mag = 0;
} else {
this.sign = value;
}
}
// Object.defineProperty(Decimal.prototype, "mantissa", {
}, {
key: "mantissa",
get: function get() {
return this.m;
},
set: function set(value) {
this.m = value;
}
}, {
key: "exponent",
get: function get() {
return this.e;
},
set: function set(value) {
this.e = value;
}
/**
* Turns the given components into a valid Decimal.
*/
}, {
key: "normalize",
value:
/**
* Turns the Decimal into a valid Decimal. This function is meant for internal purposes - users of this library should not need to use normalize.
*
* Note: this function mutates the Decimal it is called on.
*/
function normalize() {
/*
PSEUDOCODE:
Whenever we are partially 0 (sign is 0 or mag and layer is 0), make it fully 0.
Whenever we are at or hit layer 0, extract sign from negative mag.
If layer === 0 and mag < FIRST_NEG_LAYER (1/9e15), shift to 'first negative layer' (add layer, log10 mag).
While abs(mag) > EXP_LIMIT (9e15), layer += 1, mag = maglog10(mag).
While abs(mag) < LAYER_DOWN (15.954) and layer > 0, layer -= 1, mag = pow(10, mag).
When we're done, all of the following should be true OR one of the numbers is not IsFinite OR layer is not IsInteger (error state):
Any 0 is totally zero (0, 0, 0) and any NaN is totally NaN (NaN, NaN, NaN).
Anything layer 0 has mag 0 OR mag > 1/9e15 and < 9e15.
Anything layer 1 or higher has abs(mag) >= 15.954 and < 9e15.
Any positive infinity is (1, Infinity, Infinity) and any negative infinity is (-1, Infinity, Infinity).
We will assume in calculations that all Decimals are either erroneous or satisfy these criteria. (Otherwise: Garbage in, garbage out.)
*/
//Any 0 is totally 0
if (this.sign === 0 || this.mag === 0 && this.layer === 0 || this.mag === Number.NEGATIVE_INFINITY && this.layer > 0 && Number.isFinite(this.layer)) {
this.sign = 0;
this.mag = 0;
this.layer = 0;
return this;
}
//extract sign from negative mag at layer 0
if (this.layer === 0 && this.mag < 0) {
this.mag = -this.mag;
this.sign = -this.sign;
}
//Handle infinities
if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY || this.mag === Number.NEGATIVE_INFINITY || this.layer === Number.NEGATIVE_INFINITY) {
this.mag = Number.POSITIVE_INFINITY;
this.layer = Number.POSITIVE_INFINITY;
return this;
}
//Handle shifting from layer 0 to negative layers.
if (this.layer === 0 && this.mag < FIRST_NEG_LAYER) {
this.layer += 1;
this.mag = Math.log10(this.mag);
return this;
}
var absmag = Math.abs(this.mag);
var signmag = Math.sign(this.mag);
if (absmag >= EXP_LIMIT) {
this.layer += 1;
this.mag = signmag * Math.log10(absmag);
return this;
} else {
while (absmag < LAYER_DOWN && this.layer > 0) {
this.layer -= 1;
if (this.layer === 0) {
this.mag = Math.pow(10, this.mag);
} else {
this.mag = signmag * Math.pow(10, absmag);
absmag = Math.abs(this.mag);
signmag = Math.sign(this.mag);
}
}
if (this.layer === 0) {
if (this.mag < 0) {
//extract sign from negative mag at layer 0
this.mag = -this.mag;
this.sign = -this.sign;
} else if (this.mag === 0) {
//excessive rounding can give us all zeroes
this.sign = 0;
}
}
}
if (Number.isNaN(this.sign) || Number.isNaN(this.layer) || Number.isNaN(this.mag)) {
this.sign = Number.NaN;
this.layer = Number.NaN;
this.mag = Number.NaN;
}
return this;
}
/**
* Turns the given components into a valid Decimal.
*
* Note: this function mutates the Decimal it is called on.
*/
}, {
key: "fromComponents",
value: function fromComponents(sign, layer, mag) {
this.sign = sign;
this.layer = layer;
this.mag = mag;
this.normalize();
return this;
}
/**
* Turns the given components into a Decimal, but not necessarily a valid one (it's only valid if the components would already create a valid Decimal without normalization). Users of this library should not use this function.
*
* Note: this function mutates the Decimal it is called on.
*/
}, {
key: "fromComponents_noNormalize",
value: function fromComponents_noNormalize(sign, layer, mag) {
this.sign = sign;
this.layer = layer;
this.mag = mag;
return this;
}
/**
* Turns the mantissa and exponent into a valid Decimal with value mantissa * 10^exponent.
*
* Note: this function mutates the Decimal it is called on.
*/
}, {
key: "fromMantissaExponent",
value: function fromMantissaExponent(mantissa, exponent) {
this.layer = 1;
this.sign = Math.sign(mantissa);
mantissa = Math.abs(mantissa);
this.mag = exponent + Math.log10(mantissa);
this.normalize();
return this;
}
/**
* Turns the mantissa and exponent into a Decimal, but not necessarily a valid one. Users of this library should not use this function.
*
* Note: this function mutates the Decimal it is called on.
*/
}, {
key: "fromMantissaExponent_noNormalize",
value: function fromMantissaExponent_noNormalize(mantissa, exponent) {
//The idea of 'normalizing' a break_infinity.js style Decimal doesn't really apply. So just do the same thing.
this.fromMantissaExponent(mantissa, exponent);
return this;
}
/**
* Turns the Decimal that this function is called on into a deep copy of the provided value.
*
* Note: this function mutates the Decimal it is called on.
*/
}, {
key: "fromDecimal",
value: function fromDecimal(value) {
this.sign = value.sign;
this.layer = value.layer;
this.mag = value.mag;
return this;
}
/**
* Converts a floating-point number into a Decimal.
*
* Note: this function mutates the Decimal it is called on.
*/
}, {
key: "fromNumber",
value: function fromNumber(value) {
this.mag = Math.abs(value);
this.sign = Math.sign(value);
this.layer = 0;
this.normalize();
return this;
}
/**
* Converts a string into a Decimal.
*
* If linearhyper4 is true, then strings like "10^^8.5" will use the linear approximation of tetration even for bases <= 10.
*
* Note: this function mutates the Decimal it is called on.
*/
}, {
key: "fromString",
value: function fromString(value) {
var linearhyper4 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var originalValue = value;
var cached = Decimal.fromStringCache.get(originalValue);
if (cached !== undefined) {
return this.fromDecimal(cached);
}
{
value = value.replace(",", "");
}
//Handle x^^^y format. Note that no linearhyper5 parameter is needed, as pentation has no analytic approximation.
var pentationparts = value.split("^^^");
if (pentationparts.length === 2) {
var _base = parseFloat(pentationparts[0]);
var _height = parseFloat(pentationparts[1]);
var heightparts = pentationparts[1].split(";");
var payload = 1;
if (heightparts.length === 2) {
payload = parseFloat(heightparts[1]);
if (!isFinite(payload)) {
payload = 1;
}
}
if (isFinite(_base) && isFinite(_height)) {
var result = Decimal.pentate(_base, _height, payload, linearhyper4);
this.sign = result.sign;
this.layer = result.layer;
this.mag = result.mag;
if (Decimal.fromStringCache.maxSize >= 1) {
Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this));
}
return this;
}
}
//Handle x^^y format.
var tetrationparts = value.split("^^");
if (tetrationparts.length === 2) {
var _base2 = parseFloat(tetrationparts[0]);
var _height2 = parseFloat(tetrationparts[1]);
var _heightparts = tetrationparts[1].split(";");
var _payload = 1;
if (_heightparts.length === 2) {
_payload = parseFloat(_heightparts[1]);
if (!isFinite(_payload)) {
_payload = 1;
}
}
if (isFinite(_base2) && isFinite(_height2)) {
var _result = Decimal.tetrate(_base2, _height2, _payload, linearhyper4);
this.sign = _result.sign;
this.layer = _result.layer;
this.mag = _result.mag;
if (Decimal.fromStringCache.maxSize >= 1) {
Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this));
}
return this;
}
}
//Handle x^y format.
var powparts = value.split("^");
if (powparts.length === 2) {
var _base3 = parseFloat(powparts[0]);
var _exponent = parseFloat(powparts[1]);
if (isFinite(_base3) && isFinite(_exponent)) {
var _result2 = Decimal.pow(_base3, _exponent);
this.sign = _result2.sign;
this.layer = _result2.layer;
this.mag = _result2.mag;
if (Decimal.fromStringCache.maxSize >= 1) {
Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this));
}
return this;
}
}
//Handle various cases involving it being a Big Number.
value = value.trim().toLowerCase();
//handle X PT Y format.
var base;
var height;
var ptparts = value.split("pt");
if (ptparts.length === 2) {
base = 10;
var negative = false;
if (ptparts[0][0] == "-") {
negative = true;
ptparts[0] = ptparts[0].slice(1);
}
height = parseFloat(ptparts[0]);
ptparts[1] = ptparts[1].replace("(", "");
ptparts[1] = ptparts[1].replace(")", "");
var _payload2 = parseFloat(ptparts[1]);
if (!isFinite(_payload2)) {
_payload2 = 1;
}
if (isFinite(base) && isFinite(height)) {
var _result3 = Decimal.tetrate(base, height, _payload2, linearhyper4);
this.sign = _result3.sign;
this.layer = _result3.layer;
this.mag = _result3.mag;
if (Decimal.fromStringCache.maxSize >= 1) {
Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this));
}
if (negative) this.sign *= -1;
return this;
}
}
//handle XpY format (it's the same thing just with p).
ptparts = value.split("p");
if (ptparts.length === 2) {
base = 10;
var _negative = false;
if (ptparts[0][0] == "-") {
_negative = true;
ptparts[0] = ptparts[0].slice(1);
}
height = parseFloat(ptparts[0]);
ptparts[1] = ptparts[1].replace("(", "");
ptparts[1] = ptparts[1].replace(")", "");
var _payload3 = parseFloat(ptparts[1]);
if (!isFinite(_payload3)) {
_payload3 = 1;
}
if (isFinite(base) && isFinite(height)) {
var _result4 = Decimal.tetrate(base, height, _payload3, linearhyper4);
this.sign = _result4.sign;
this.layer = _result4.layer;
this.mag = _result4.mag;
if (Decimal.fromStringCache.maxSize >= 1) {
Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this));
}
if (_negative) this.sign *= -1;
return this;
}
}
//handle XFY format
ptparts = value.split("f");
if (ptparts.length === 2) {
base = 10;
var _negative2 = false;
if (ptparts[0][0] == "-") {
_negative2 = true;
ptparts[0] = ptparts[0].slice(1);
}
ptparts[0] = ptparts[0].replace("(", "");
ptparts[0] = ptparts[0].replace(")", "");
var _payload4 = parseFloat(ptparts[0]);
ptparts[1] = ptparts[1].replace("(", "");
ptparts[1] = ptparts[1].replace(")", "");
height = parseFloat(ptparts[1]);
if (!isFinite(_payload4)) {
_payload4 = 1;
}
if (isFinite(base) && isFinite(height)) {
var _result5 = Decimal.tetrate(base, height, _payload4, linearhyper4);
this.sign = _result5.sign;
this.layer = _result5.layer;
this.mag = _result5.mag;
if (Decimal.fromStringCache.maxSize >= 1) {
Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this));
}
if (_negative2) this.sign *= -1;
return this;
}
}
var parts = value.split("e");
var ecount = parts.length - 1;
//Handle numbers that are exactly floats (0 or 1 es).
if (ecount === 0) {
var numberAttempt = parseFloat(value);
if (isFinite(numberAttempt)) {
this.fromNumber(numberAttempt);
if (Decimal.fromStringCache.size >= 1) {
Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this));
}
return this;
}
} else if (ecount === 1) {
//Very small numbers ("2e-3000" and so on) may look like valid floats but round to 0.
var _numberAttempt = parseFloat(value);
if (isFinite(_numberAttempt) && _numberAttempt !== 0) {
this.fromNumber(_numberAttempt);
if (Decimal.fromStringCache.maxSize >= 1) {
Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this));
}
return this;
}
}
//Handle new (e^N)X format.
var newparts = value.split("e^");
if (newparts.length === 2) {
this.sign = 1;
if (newparts[0].charAt(0) == "-") {
this.sign = -1;
}
var layerstring = "";
for (var i = 0; i < newparts[1].length; ++i) {
var chrcode = newparts[1].charCodeAt(i);
if (chrcode >= 43 && chrcode <= 57 || chrcode === 101) {
//is "0" to "9" or "+" or "-" or "." or "e" (or "," or "/")
layerstring += newparts[1].charAt(i);
} //we found the end of the layer count
else {
this.layer = parseFloat(layerstring);
this.mag = parseFloat(newparts[1].substr(i + 1));
// Handle invalid cases like (e^-8)1 and (e^10.5)1 by just calling tetrate
if (this.layer < 0 || this.layer % 1 != 0) {
var _result6 = Decimal.tetrate(10, this.layer, this.mag, linearhyper4);
this.sign = _result6.sign;
this.layer = _result6.layer;
this.mag = _result6.mag;
}
this.normalize();
if (Decimal.fromStringCache.maxSize >= 1) {
Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this));
}
return this;
}
}
}
if (ecount < 1) {
this.sign = 0;
this.layer = 0;
this.mag = 0;
if (Decimal.fromStringCache.maxSize >= 1) {
Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this));
}
return this;
}
var mantissa = parseFloat(parts[0]);
if (mantissa === 0) {
this.sign = 0;
this.layer = 0;
this.mag = 0;
if (Decimal.fromStringCache.maxSize >= 1) {
Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this));
}
return this;
}
var exponent = parseFloat(parts[parts.length - 1]);
//handle numbers like AeBeC and AeeeeBeC
if (ecount >= 2) {
var me = parseFloat(parts[parts.length - 2]);
if (isFinite(me)) {
exponent *= Math.sign(me);
exponent += f_maglog10(me);
}
}
//Handle numbers written like eee... (N es) X
if (!isFinite(mantissa)) {
this.sign = parts[0] === "-" ? -1 : 1;
this.layer = ecount;
this.mag = exponent;
}
//Handle numbers written like XeY
else if (ecount === 1) {
this.sign = Math.sign(mantissa);
this.layer = 1;
//Example: 2e10 is equal to 10^log10(2e10) which is equal to 10^(10+log10(2))
this.mag = exponent + Math.log10(Math.abs(mantissa));
}
//Handle numbers written like Xeee... (N es) Y
else {
this.sign = Math.sign(mantissa);
this.layer = ecount;
if (ecount === 2) {
var _result7 = Decimal.mul(FC(1, 2, exponent), D(mantissa));
this.sign = _result7.sign;
this.layer = _result7.layer;
this.mag = _result7.mag;
if (Decimal.fromStringCache.maxSize >= 1) {
Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this));
}
return this;
} else {
//at eee and above, mantissa is too small to be recognizable!
this.mag = exponent;
}
}
this.normalize();
if (Decimal.fromStringCache.maxSize >= 1) {
Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this));
}
return this;
}
/**
* The function used by new Decimal() to create a new Decimal. Accepts a DecimalSource: uses fromNumber if given a number, uses fromString if given a string, and uses fromDecimal if given a Decimal.
*
* Note: this function mutates the Decimal it is called on.
*/
}, {
key: "fromValue",
value: function fromValue(value) {
if (value instanceof Decimal) {
return this.fromDecimal(value);
}
if (typeof value === "number") {
return this.fromNumber(value);
}
if (typeof value === "string") {
return this.fromString(value);
}
this.sign = 0;
this.layer = 0;
this.mag = 0;
return this;
}
/**
* Returns the numeric value of the Decimal it's called on. Will return Infinity (or -Infinity for negatives) for Decimals that are larger than Number.MAX_VALUE.
*/
}, {
key: "toNumber",
value: function toNumber() {
if (this.mag === Number.POSITIVE_INFINITY && this.layer === Number.POSITIVE_INFINITY && this.sign === 1) {
return Number.POSITIVE_INFINITY;
}
if (this.mag === Number.POSITIVE_INFINITY && this.layer === Number.POSITIVE_INFINITY && this.sign === -1) {
return Number.NEGATIVE_INFINITY;
}
if (!Number.isFinite(this.layer)) {
return Number.NaN;
}
if (this.layer === 0) {
return this.sign * this.mag;
} else if (this.layer === 1) {
return this.sign * Math.pow(10, this.mag);
} //overflow for any normalized Decimal
else {
return this.mag > 0 ? this.sign > 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY : 0;
}
}
}, {
key: "mantissaWithDecimalPlaces",
value: function mantissaWithDecimalPlaces(places) {
// https://stackoverflow.com/a/37425022
if (isNaN(this.m)) {
return Number.NaN;