-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.ts
4913 lines (4500 loc) · 181 KB
/
index.ts
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
import { LRUCache } from "./lru-cache";
export type CompareResult = -1 | 0 | 1;
const MAX_SIGNIFICANT_DIGITS = 17; //Maximum number of digits of precision to assume in Number
const 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.)
const LAYER_DOWN: number = Math.log10(9e15);
const 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.
const NUMBER_EXP_MAX = 308; //The largest exponent that can appear in a Number, though not all mantissas are valid here.
const NUMBER_EXP_MIN = -324; //The smallest exponent that can appear in a Number, though not all mantissas are valid here.
const MAX_ES_IN_A_ROW = 5; //For default toString behaviour, when to swap from eee... to (e^n) syntax.
const DEFAULT_FROM_STRING_CACHE_SIZE = (1 << 10) - 1; // The default size of the LRU cache used to cache Decimal.fromString.
const IGNORE_COMMAS = true;
const COMMAS_ARE_DECIMAL_POINTS = false;
const 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
const powersOf10: number[] = [];
for (let i = NUMBER_EXP_MIN + 1; i <= NUMBER_EXP_MAX; i++) {
powersOf10.push(Number("1e" + i));
}
const indexOf0InPowersOf10 = 323;
return function (power: number) {
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
const critical_headers = [2, Math.E, 3, 4, 5, 6, 7, 8, 9, 10];
const 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, //0.0
1.1121114330934078681, //0.1
1.2310389249316089299, //0.2
1.3583836963111376089, //0.3
1.4960519303993531879, //0.4
1.6463542337511945810, //0.5
1.8121385357018724464, //0.6
1.9969713246183068478, //0.7
2.2053895545527544330, //0.8
2.4432574483385252544, //0.9
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,
],
];
const 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.0
-0.90603157029014, //0.1
-0.80786507256596, //0.2
-0.7064666939634, //0.3
-0.60294836853664, //0.4
-0.49849837513117, //0.5
-0.39430303318768, //0.6
-0.29147201034755, //0.7
-0.19097820800866, //0.8
-0.09361896280296, //0.9
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,
],
];
let D = function D(value: DecimalSource): Readonly<Decimal> {
return Decimal.fromValue_noAlloc(value);
};
let FC = function (sign: number, layer: number, mag: number) {
return Decimal.fromComponents(sign, layer, mag);
};
let FC_NN = function FC_NN(sign: number, layer: number, mag: number) {
return Decimal.fromComponents_noNormalize(sign, layer, mag);
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let ME = function ME(mantissa: number, exponent: number) {
return Decimal.fromMantissaExponent(mantissa, exponent);
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let ME_NN = function ME_NN(mantissa: number, exponent: number) {
return Decimal.fromMantissaExponent_noNormalize(mantissa, exponent);
};
const decimalPlaces = function decimalPlaces(value: number, places: number): number {
const len = places + 1;
const numDigits = Math.ceil(Math.log10(Math.abs(value)));
const rounded = Math.round(value * Math.pow(10, len - numDigits)) * Math.pow(10, numDigits - len);
return parseFloat(rounded.toFixed(Math.max(len - numDigits, 0)));
};
const f_maglog10 = function (n: number) {
return Math.sign(n) * Math.log10(Math.abs(n));
};
//from HyperCalc source code
const f_gamma = function (n: number) {
if (!isFinite(n)) {
return n;
}
if (n < -50) {
if (n === Math.trunc(n)) {
return Number.NEGATIVE_INFINITY;
}
return 0;
}
let scal1 = 1;
while (n < 10) {
scal1 = scal1 * n;
++n;
}
n -= 1;
let l = 0.9189385332046727; //0.5*Math.log(2*Math.PI)
l = l + (n + 0.5) * Math.log(n);
l = l - n;
const n2 = n * n;
let 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;
};
const _twopi = 6.2831853071795864769252842; // 2*pi
const _EXPN1 = 0.36787944117144232159553; // exp(-1)
const 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
const f_lambertw = function (z: number, tol = 1e-10, principal = true): number {
let w;
let 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 (let 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: ${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: Decimal, tol = 1e-10, principal = true): Decimal {
let w;
let 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 (let 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: ${z.toString()}`);
//return Decimal.dNaN;
}
export type DecimalSource = Decimal | number | string;
/**
* 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.
*/
export default class Decimal {
/**
* Represents the number 0.
*/
public static readonly dZero = FC_NN(0, 0, 0);
/**
* Represents the number 1.
*/
public static readonly dOne = FC_NN(1, 0, 1);
/**
* Represents the number -1.
*/
public static readonly dNegOne = FC_NN(-1, 0, 1);
/**
* Represents the number 2.
*/
public static readonly dTwo = FC_NN(1, 0, 2);
/**
* Represents the number 10.
*/
public static readonly dTen = FC_NN(1, 0, 10);
/**
* Represents a NaN (Not A Number) value.
*/
public static readonly dNaN = FC_NN(Number.NaN, Number.NaN, Number.NaN);
/**
* Represents positive infinity.
*/
public static readonly dInf = FC_NN(1, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
/**
* Represents negative infinity.
*/
public static readonly dNegInf = FC_NN(-1, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
/**
* Represents the largest value a JavaScript number can have, which is approximately 1.79 * 10^308.
*/
public static readonly dNumberMax = FC(1, 0, Number.MAX_VALUE);
/**
* Represents the smallest value a JavaScript number can have, which is approximately 5 * 10^-324.
*/
public static readonly dNumberMin = FC(1, 0, Number.MIN_VALUE);
/**
* Represents the largest Decimal where adding 1 to the layer is a safe operation
* (Decimals larger than this are too big for pow/exp/log to affect, but tetrate/iteratedlog/slog can still affect them).
* Approximately 10^^(9.007 * 10^15).
*/
public static readonly dLayerSafeMax = FC(1, Number.MAX_SAFE_INTEGER, EXP_LIMIT - 1);
/**
* Represents the smallest Decimal where adding 1 to the layer is a safe operation. Approximately 1 / (10^^(9.007 * 10^15)).
*/
public static readonly dLayerSafeMin = FC(1, Number.MAX_SAFE_INTEGER, -(EXP_LIMIT - 1));
/**
* Represents the largest finite value a Decimal can represent. Approximately 10^^(1.79 * 10^308).
*/
public static readonly dLayerMax = FC(1, Number.MAX_VALUE, EXP_LIMIT - 1);
/**
* Represents the smallest non-zero value a Decimal can represent. Approximately 1 / (10^^(1.79 * 10^308)).
*/
public static readonly dLayerMin = FC(1, Number.MAX_VALUE, -(EXP_LIMIT - 1));
private static fromStringCache = new LRUCache<string, Decimal>(DEFAULT_FROM_STRING_CACHE_SIZE);
public sign = 0;
public mag = 0;
public layer = 0;
constructor(value?: DecimalSource) {
if (value instanceof Decimal) {
this.fromDecimal(value);
} else if (typeof value === "number") {
this.fromNumber(value);
} else if (typeof value === "string") {
this.fromString(value);
}
}
get m(): number {
if (this.sign === 0) {
return 0;
} else if (this.layer === 0) {
const exp = Math.floor(Math.log10(this.mag));
//handle special case 5e-324
let man;
if (this.mag === 5e-324) {
man = 5;
} else {
man = this.mag / powerOf10(exp);
}
return this.sign * man;
} else if (this.layer === 1) {
const 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 m(value: number) {
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;
}
}
}
get e(): number {
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 e(value: number) {
this.fromMantissaExponent(this.m, value);
}
get s(): number {
return this.sign;
}
set s(value: number) {
if (value === 0) {
this.sign = 0;
this.layer = 0;
this.mag = 0;
} else {
this.sign = value;
}
}
// Object.defineProperty(Decimal.prototype, "mantissa", {
get mantissa(): number {
return this.m;
}
set mantissa(value: number) {
this.m = value;
}
get exponent(): number {
return this.e;
}
set exponent(value: number) {
this.e = value;
}
/**
* Turns the given components into a valid Decimal.
*/
public static fromComponents(sign: number, layer: number, mag: number): Decimal {
return new Decimal().fromComponents(sign, layer, mag);
}
/**
* 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.
*/
public static fromComponents_noNormalize(sign: number, layer: number, mag: number): Decimal {
return new Decimal().fromComponents_noNormalize(sign, layer, mag);
}
/**
* Turns the mantissa and exponent into a valid Decimal with value mantissa * 10^exponent.
*/
public static fromMantissaExponent(mantissa: number, exponent: number): Decimal {
return new Decimal().fromMantissaExponent(mantissa, exponent);
}
/**
* Turns the mantissa and exponent into a Decimal, but not necessarily a valid one. Users of this library should not use this function.
*/
public static fromMantissaExponent_noNormalize(mantissa: number, exponent: number): Decimal {
return new Decimal().fromMantissaExponent_noNormalize(mantissa, exponent);
}
/**
* Creates a deep copy of the provided value.
*/
public static fromDecimal(value: Decimal): Decimal {
return new Decimal().fromDecimal(value);
}
/**
* Converts a floating-point number into a Decimal.
*/
public static fromNumber(value: number): Decimal {
return new Decimal().fromNumber(value);
}
/**
* 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.
*/
public static fromString(value: string, linearhyper4: boolean = false): Decimal {
return new Decimal().fromString(value, linearhyper4);
}
/**
* 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.
*/
public static fromValue(value: DecimalSource): Decimal {
return new Decimal().fromValue(value);
}
/**
* Converts a DecimalSource to a Decimal, without constructing a new Decimal
* if the provided value is already a Decimal.
*
* As the return value could be the provided value itself, this function
* returns a read-only Decimal to prevent accidental mutations of the value.
* Use `new Decimal(value)` to explicitly create a writeable copy if mutation
* is required.
*/
public static fromValue_noAlloc(value: DecimalSource): Readonly<Decimal> {
if (value instanceof Decimal) {
return value;
} else if (typeof value === "string") {
const cached = Decimal.fromStringCache.get(value);
if (cached !== undefined) {
return cached;
}
return Decimal.fromString(value);
} else if (typeof value === "number") {
return Decimal.fromNumber(value);
} else {
// This should never happen... but some users like Prestige Tree Rewritten
// pass undefined values in as DecimalSources, so we should handle this
// case to not break them.
return FC_NN(0, 0, 0);
}
}
/**
* Absolute value function: returns 'value' if 'value' >= 0, returns the negative of 'value' if 'value' < 0.
*/
public static abs(value: DecimalSource): Decimal {
return D(value).abs();
}
/**
* Returns the negative of the given value.
*/
public static neg(value: DecimalSource): Decimal {
return D(value).neg();
}
/**
* Returns the negative of the given value.
*/
public static negate(value: DecimalSource): Decimal {
return D(value).neg();
}
/**
* Returns the negative of the given value.
*/
public static negated(value: DecimalSource): Decimal {
return D(value).neg();
}
/**
* Returns the sign of the given value.
*/
public static sign(value: DecimalSource): number {
return D(value).sign;
}
/**
* Returns the sign of the given value.
*/
public static sgn(value: DecimalSource): number {
return D(value).sign;
}
/**
* Rounds the value to the nearest integer.
*/
public static round(value: DecimalSource): Decimal {
return D(value).round();
}
/**
* "Rounds" the value to the nearest integer that's less than or equal to it.
*/
public static floor(value: DecimalSource): Decimal {
return D(value).floor();
}
/**
* "Rounds" the value to the nearest integer that's greater than or equal to it.
*/
public static ceil(value: DecimalSource): Decimal {
return D(value).ceil();
}
/**
* Extracts the integer part of the Decimal and returns it. Behaves like floor on positive numbers, but behaves like ceiling on negative numbers.
*/
public static trunc(value: DecimalSource): Decimal {
return D(value).trunc();
}
/**
* Addition: returns the sum of the two Decimals.
*/
public static add(value: DecimalSource, other: DecimalSource): Decimal {
return D(value).add(other);
}
/**
* Addition: returns the sum of the two Decimals.
*/
public static plus(value: DecimalSource, other: DecimalSource): Decimal {
return D(value).add(other);
}
/**
* Subtraction: returns the difference between 'value' and 'other'.
*/
public static sub(value: DecimalSource, other: DecimalSource): Decimal {
return D(value).sub(other);
}
/**
* Subtraction: returns the difference between 'value' and 'other'.
*/
public static subtract(value: DecimalSource, other: DecimalSource): Decimal {
return D(value).sub(other);
}
/**
* Subtraction: returns the difference between 'value' and 'other'.
*/
public static minus(value: DecimalSource, other: DecimalSource): Decimal {
return D(value).sub(other);
}
/**
* Multiplication: returns the product of the two Decimals.
*/
public static mul(value: DecimalSource, other: DecimalSource): Decimal {
return D(value).mul(other);
}
/**
* Multiplication: returns the product of the two Decimals.
*/
public static multiply(value: DecimalSource, other: DecimalSource): Decimal {
return D(value).mul(other);
}
/**
* Multiplication: returns the product of the two Decimals.
*/
public static times(value: DecimalSource, other: DecimalSource): Decimal {
return D(value).mul(other);
}
/**
* Division: returns the quotient of 'value' and 'other'.
*/
public static div(value: DecimalSource, other: DecimalSource): Decimal {
return D(value).div(other);
}
/**
* Division: returns the quotient of 'value' and 'other'.
*/
public static divide(value: DecimalSource, other: DecimalSource): Decimal {
return D(value).div(other);
}
/**
* Returns the reciprocal (1 / X) of the given value.
*/
public static recip(value: DecimalSource): Decimal {
return D(value).recip();
}
/**
* Returns the reciprocal (1 / X) of the given value.
*/
public static reciprocal(value: DecimalSource): Decimal {
return D(value).recip();
}
/**
* Returns the reciprocal (1 / X) of the given value.
*/
public static reciprocate(value: DecimalSource): Decimal {
return D(value).reciprocate();
}
/**
* Returns the remainder of 'this' divided by 'value': for example, 5 mod 2 = 1, because the remainder of 5 / 2 is 1.
* Uses the "truncated division" modulo, which is the same as JavaScript's native modulo operator (%)...
* unless 'floored' is true, in which case it uses the "floored" modulo, which is closer to how modulo works in number theory.
* These two forms of modulo are the same when only positive numbers are involved, but differ in how they work with negative numbers.
*/
public static mod(value: DecimalSource, other: DecimalSource, floored : boolean = false): Decimal {
return D(value).mod(other, floored);
}
/**
* Returns the remainder of 'this' divided by 'value': for example, 5 mod 2 = 1, because the remainder of 5 / 2 is 1.
* Uses the "truncated division" modulo, which is the same as JavaScript's native modulo operator (%)...
* unless 'floored' is true, in which case it uses the "floored" modulo, which is closer to how modulo works in number theory.
* These two forms of modulo are the same when only positive numbers are involved, but differ in how they work with negative numbers.
*/
public static modulo(value: DecimalSource, other: DecimalSource, floored : boolean = false): Decimal {
return D(value).modulo(other, floored);
}
/**
* Returns the remainder of 'this' divided by 'value': for example, 5 mod 2 = 1, because the remainder of 5 / 2 is 1.
* Uses the "truncated division" modulo, which is the same as JavaScript's native modulo operator (%)...
* unless 'floored' is true, in which case it uses the "floored" modulo, which is closer to how modulo works in number theory.
* These two forms of modulo are the same when only positive numbers are involved, but differ in how they work with negative numbers.
*/
public static modular(value: DecimalSource, other: DecimalSource, floored : boolean = false): Decimal {
return D(value).modular(other, floored);
}
/**
* Returns 1 if 'value' > 'other', returns -1 if 'value' < 'other', returns 0 if 'value' == 'other'.
*/
public static cmp(value: DecimalSource, other: DecimalSource): CompareResult {
return D(value).cmp(other);
}
/**
* Compares the absolute values of this and value.
* Returns 1 if |'value'| > |'other'|, returns -1 if |'value'| < |'other'|, returns 0 if |'value'| == |'other'|.
*/
public static cmpabs(value: DecimalSource, other: DecimalSource): CompareResult {
return D(value).cmpabs(other);
}
/**
* Returns 1 if 'value' > 'other', returns -1 if 'value' < 'other', returns 0 if 'value' == 'other'.
*/
public static compare(value: DecimalSource, other: DecimalSource): CompareResult {
return D(value).cmp(other);
}
/**
* Returns true if the given value is an NaN value.
*/
public static isNaN(value: DecimalSource): boolean {
value = D(value);
return isNaN(value.sign) || isNaN(value.layer) || isNaN(value.mag);
}
/**
* Returns true if the given value is finite (by Decimal standards, not by floating point standards - a humongous Decimal like 10^^10^100 is still finite!)
*/
public static isFinite(value: DecimalSource): boolean {
value = D(value);
return isFinite(value.sign) && isFinite(value.layer) && isFinite(value.mag);
}
/**
* The Decimal equivalent of ==. Returns true if 'value' and 'other' have equal values.
*/
public static eq(value: DecimalSource, other: DecimalSource): boolean {
return D(value).eq(other);
}
/**
* Returns true if 'value' and 'other' have equal values.
*/
public static equals(value: DecimalSource, other: DecimalSource): boolean {
return D(value).eq(other);
}
/**
* The Decimal equivalent of !=. Returns true if 'value' and 'other' do not have equal values.
*/
public static neq(value: DecimalSource, other: DecimalSource): boolean {
return D(value).neq(other);
}
/**
* Returns true if 'value' and 'other' do not have equal values.
*/
public static notEquals(value: DecimalSource, other: DecimalSource): boolean {
return D(value).notEquals(other);
}
/**
* The Decimal equivalent of <. Returns true if 'value' is less than 'other'.
*/
public static lt(value: DecimalSource, other: DecimalSource): boolean {
return D(value).lt(other);
}
/**
* The Decimal equivalent of <=. Returns true if 'value' is less than or equal to 'other'.
*/
public static lte(value: DecimalSource, other: DecimalSource): boolean {
return D(value).lte(other);
}
/**
* The Decimal equivalent of >. Returns true if 'value' is greater than 'other'.
*/
public static gt(value: DecimalSource, other: DecimalSource): boolean {
return D(value).gt(other);
}
/**
* The Decimal equivalent of >=. Returns true if 'value' is greater than or equal to 'other'.
*/
public static gte(value: DecimalSource, other: DecimalSource): boolean {
return D(value).gte(other);
}
/**
* Returns whichever of 'value' and 'other' is higher.
*/
public static max(value: DecimalSource, other: DecimalSource): Decimal {
return D(value).max(other);
}
/**
* Returns whichever of 'value' and 'other' is lower.
*/
public static min(value: DecimalSource, other: DecimalSource): Decimal {
return D(value).min(other);
}
/**
* Returns whichever of 'value' and 'other' has a larger absolute value.
*/
public static minabs(value: DecimalSource, other: DecimalSource): Decimal {
return D(value).minabs(other);
}
/**
* Returns whichever of 'value' and 'other' has a smaller absolute value.
*/
public static maxabs(value: DecimalSource, other: DecimalSource): Decimal {
return D(value).maxabs(other);
}
/**
* A combination of minimum and maximum: the value returned by clamp is normally 'value', but it won't go below 'min' and it won't go above 'max'.
* Therefore, if 'value' < 'min', then 'min' is returned, and if 'value' > 'max', then 'max' is returned.
*/
public static clamp(value: DecimalSource, min: DecimalSource, max: DecimalSource): Decimal {
return D(value).clamp(min, max);
}
/**
* Returns 'value', unless 'value' is less than 'min', in which case 'min' is returned.
*/
public static clampMin(value: DecimalSource, min: DecimalSource): Decimal {
return D(value).clampMin(min);
}
/**
* Returns 'value', unless 'value' is greater than 'max', in which case 'max' is returned.
*/
public static clampMax(value: DecimalSource, max: DecimalSource): Decimal {
return D(value).clampMax(max);
}
/**
* Returns 1 if 'value' is greater than 'other', returns -1 if 'value' is less than 'other', returns 0 if 'value' is equal to 'other'.
* However, the two Decimals are considered equal if they're approximately equal up to a certain tolerance.
* Tolerance is a relative tolerance, multiplied by the greater of the magnitudes of the two arguments.
* For example, if you put in 1e-9, then any number closer to the
* larger number than (larger number)*1e-9 will be considered equal.
*/
public static cmp_tolerance(
value: DecimalSource,
other: DecimalSource,
tolerance: number
): CompareResult {
return D(value).cmp_tolerance(other, tolerance);
}
/**
* Returns 1 if 'value' is greater than 'other', returns -1 if 'value' is less than 'other', returns 0 if 'value' is equal to 'other'.
* However, the two Decimals are considered equal if they're approximately equal up to a certain tolerance.
* Tolerance is a relative tolerance, multiplied by the greater of the magnitudes of the two arguments.
* For example, if you put in 1e-9, then any number closer to the
* larger number than (larger number)*1e-9 will be considered equal.
*/
public static compare_tolerance(
value: DecimalSource,
other: DecimalSource,
tolerance: number
): CompareResult {
return D(value).cmp_tolerance(other, tolerance);
}
/**
* Tests whether two Decimals are approximately equal, up to a certain tolerance.
* Tolerance is a relative tolerance, multiplied by the greater of the magnitudes of the two arguments.
* For example, if you put in 1e-9, then any number closer to the
* larger number than (larger number)*1e-9 will be considered equal.
*/
public static eq_tolerance(
value: DecimalSource,
other: DecimalSource,
tolerance: number
): boolean {
return D(value).eq_tolerance(other, tolerance);
}
/**
* Tests whether two Decimals are approximately equal, up to a certain tolerance.
* Tolerance is a relative tolerance, multiplied by the greater of the magnitudes of the two arguments.
* For example, if you put in 1e-9, then any number closer to the
* larger number than (larger number)*1e-9 will be considered equal.
*/
public static equals_tolerance(
value: DecimalSource,
other: DecimalSource,
tolerance: number
): boolean {
return D(value).eq_tolerance(other, tolerance);
}
/**