-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigIntType.js
2428 lines (2426 loc) · 125 KB
/
BigIntType.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
//@ts-check
"use strict";
// TODO add "use strict"; to every function and change jsdocs to {@linkcode [optional_param]} == null ? set default val : (typecheck ? throw : 0) and copy to HTML page
/**
* __Class for arbitrarily large integers with typed arrays__ \
* can natively convert `BigIntType` numbers to `BigInt` or `String` when needed
* @class
* @author MAZ <https://MAZ01001.GitHub.io/>
*/
class BigIntType{
//~ property/method names starting with '#' are private - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields for details
/*
max number with 500 base 256 digits is
256**500-1 = 13 182 040 934 309 431 001 038 897 942 365 913 631 840 191 610 932 727
690 928 034 502 417 569 281 128 344 551 079 752 123 172 122 033 140 940 756 480
716 823 038 446 817 694 240 581 281 731 062 452 512 184 038 544 674 444 386 888
956 328 970 642 771 993 930 036 586 552 924 249 514 488 832 183 389 415 832 375
620 009 284 922 608 946 111 038 578 754 077 913 265 440 918 583 125 586 050 431
647 284 603 636 490 823 850 007 826 811 672 468 900 210 689 104 488 089 485 347
192 152 708 820 119 765 006 125 944 858 397 761 874 669 301 278 745 233 504 796
586 994 514 054 435 217 053 803 732 703 240 283 400 815 926 169 348 364 799 472
716 094 576 894 007 243 168 662 568 886 603 065 832 486 830 606 125 017 643 356
469 732 407 252 874 567 217 733 694 824 236 675 323 341 755 681 839 221 954 693
820 456 072 020 253 884 371 226 826 844 858 636 194 212 875 139 566 587 445 390
068 014 747 975 813 971 748 114 770 439 248 826 688 667 129 237 954 128 555 841
874 460 665 729 630 492 658 600 179 338 272 579 110 020 881 228 767 361 200 603
478 973 120 168 893 997 574 353 727 653 998 969 223 092 798 255 701 666 067 972
698 906 236 921 628 764 772 837 915 526 086 464 389 161 570 534 616 956 703 744
840 502 975 279 094 087 587 298 968 423 516 531 626 090 898 389 351 449 020 056
851 221 079 048 966 718 878 943 309 232 071 978 575 639 877 208 621 237 040 940
126 912 767 610 658 141 079 378 758 043 403 611 425 454 744 180 577 150 855 204
937 163 460 902 512 732 551 260 539 639 221 457 005 977 247 266 676 344 018 155
647 509 515 396 711 351 487 546 062 479 444 592 779 055 555 421 362 722 504 575
706 910 949 375 in base 10 which is 1205 digits (2.41 times longer)
*/
//~ for potentially larger numbers Uint16Array could be used ('cause 2**16*2**16 is still a save integer is js so save to compute)
//~ but it is the same size in memory just potentially a longer number 'cause the index can go higher (a digit-array in base 65536 is 2x shorter than in base 256)
//~ but with (2**53-1) digits (MAX_SAFE_INTEGER) and 1Byte (base 256) per digit - it is large enough (usually not more than 2GB can be allocated anyways...)
/**
* @type {number} - maximum possible length of a number _(excluding sign)_ - originally `500` = 0.5KB in RAM
*/
static #MAX_SIZE=0x1F4;
/**@returns {number} _current_ maximum possible length of a number _(excluding sign)_*/
static get MAX_SIZE(){return BigIntType.#MAX_SIZE;}
/**@throws {RangeError} - if setting this to a number that is not an integer in range `[1-67108864]` - _( `67108864` = 64MiB in RAM )_*/
static set MAX_SIZE(n){
//~ technically, max is 9007199254740991 (Number.MAX_SAFE_INTEGER) but with 1 Byte each entry that's almost 8PiB ! for ONE number
//~ and chrome browser will only create typed arrays up to 2GiB ~
if(!Number.isInteger(n)||n<1||n>0x4000000)throw new RangeError("[MAX_SIZE] must be an integer in range [1-67108864]");
BigIntType.#MAX_SIZE=n;
}
/**
* __converts base names to the corresponding number__ \
* _( supports numbers from `0` to `4294967296` (incl.) )_
* @param {string|number} str
* * base of `num` as a number or string ( case insensitive )
* - base braille ← `0` or `"braille"` ( must be a string with `'⠀'`-`'⣿'` (Unicode Braille Pattern `0x2800`-`0x28FF`) )
* - base 2 ← `'b'`, `"bin"`, `"bits"`, `"binary"`, or `"1bit"`
* - base 3 ← `"ternary"` or `"trinary"`
* - base 4 ← `'q'`, `"quaternary"`, or `"2bit"`
* - base 5 ← `"quinary"` or `"pental"`
* - base 6 ← `"senary"`, `"heximal"`, or `"seximal"`
* - base 7 ← `"septenary"`
* - base 8 ← `'o'`, `"oct"`, `"octal"`, or `"3bit"`
* - base 9 ← `"nonary"`
* - base 10 ← `'d'`, `"dec"`, `"decimal"`, or `"denary"`
* - base 11 ← `"undecimal"`
* - base 12 ← `"duodecimal"`, `"dozenal"`, or `"uncial"`
* - base 13 ← `"tridecimal"`
* - base 14 ← `"tetradecimal"`
* - base 15 ← `"pentadecimal"`
* - base 16 ← `'h'`, `"hex"`, `"hexadecimal"`, `"sexadecimal"`, or `"4bit"`
* - base 17 ← `"heptadecimal"`
* - base 18 ← `"octodecimal"`
* - base 19 ← `"enneadecimal"`
* - base 20 ← `"vigesimal"`
* - base 21 ← `"unvigesimal"`
* - base 22 ← `"duovigesimal"`
* - base 23 ← `"trivigesimal"`
* - base 24 ← `"tetravigesimal"`
* - base 25 ← `"pentavigesimal"`
* - base 26 ← `"hexavigesimal"`
* - base 27 ← `"heptavigesimal septemvigesimal"`
* - base 28 ← `"octovigesimal"`
* - base 29 ← `"enneavigesimal"`
* - base 30 ← `"trigesimal"`
* - base 31 ← `"untrigesimal"`
* - base 32 ← `"duotrigesimal"` or `"5bit"`
* - base 33 ← `"tritrigesimal"`
* - base 34 ← `"tetratrigesimal"`
* - base 35 ← `"pentatrigesimal"`
* - base 36 ← `'t'`, `"txt"`, `"text"`, or `"hexatrigesimal"`
* - base 37 ← `"heptatrigesimal"`
* - base 38 ← `"octotrigesimal"`
* - base 39 ← `"enneatrigesimal"`
* - base 40 ← `"quadragesimal"`
* - base 42 ← `"duoquadragesimal"`
* - base 45 ← `"pentaquadragesimal"`
* - base 47 ← `"septaquadragesimal"`
* - base 48 ← `"octoquadragesimal"`
* - base 49 ← `"enneaquadragesimal"`
* - base 50 ← `"quinquagesimal"`
* - base 52 ← `"duoquinquagesimal"`
* - base 54 ← `"tetraquinquagesimal"`
* - base 56 ← `"hexaquinquagesimal"`
* - base 57 ← `"heptaquinquagesimal"`
* - base 58 ← `"octoquinquagesimal"`
* - base 60 ← `"sexagesimal"` or `"sexagenary"`
* - base 62 ← `"duosexagesimal"`
* - base 64 ← `"tetrasexagesimal"` or `"6bit"`
* - base 72 ← `"duoseptuagesimal"`
* - base 80 ← `"octogesimal"`
* - base 81 ← `"unoctogesimal"`
* - base 85 ← `"pentoctogesimal"`
* - base 89 ← `"enneaoctogesimal"`
* - base 90 ← `"nonagesimal"`
* - base 91 ← `"unnonagesimal"`
* - base 92 ← `"duononagesimal"`
* - base 93 ← `"trinonagesimal"`
* - base 94 ← `"tetranonagesimal"`
* - base 95 ← `"pentanonagesimal"`
* - base 96 ← `"hexanonagesimal"`
* - base 97 ← `"septanonagesimal"`
* - base 100 ← `"centesimal"`
* - base 120 ← `"centevigesimal"`
* - base 121 ← `"centeunvigesimal"`
* - base 125 ← `"centepentavigesimal"`
* - base 128 ← `"centeoctovigesimal"` or `"7bit"`
* - base 144 ← `"centetetraquadragesimal"`
* - base 169 ← `"centenovemsexagesimal"`
* - base 185 ← `"centepentoctogesimal"`
* - base 196 ← `"centehexanonagesimal"`
* - base 200 ← `"duocentesimal"`
* - base 210 ← `"duocentedecimal"`
* - base 216 ← `"duocentehexidecimal"`
* - base 225 ← `"duocentepentavigesimal"`
* - base 256 ← `"duocentehexaquinquagesimal"`, `"byte"`, or `"8bit"`
* - base 300 ← `"trecentesimal"`
* - base 360 ← `"trecentosexagesimal"`
* - base 512 ← `"9bit"`
* - base 1024 ← `"10bit"`
* - base 2048 ← `"11bit"`
* - base 4096 ← `"12bit"`
* - base 8192 ← `"13bit"`
* - base 16384 ← `"14bit"`
* - base 32768 ← `"15bit"`
* - base 65536 ← `"16bit"`
* - base 131072 ← `"17bit"`
* - base 262144 ← `"18bit"`
* - base 524288 ← `"19bit"`
* - base 1048576 ← `"20bit"`
* - base 2097152 ← `"21bit"`
* - base 4194304 ← `"22bit"`
* - base 8388608 ← `"23bit"`
* - base 16777216 ← `"24bit"`
* - base 33554432 ← `"25bit"`
* - base 67108864 ← `"26bit"`
* - base 134217728 ← `"27bit"`
* - base 268435456 ← `"28bit"`
* - base 536870912 ← `"29bit"`
* - base 1073741824 ← `"30bit"`
* - base 2147483648 ← `"31bit"`
* - base 4294967296 ← `"32bit"`
* - any base within 1 to 4294967296 (incl.) can also be a number or a string representing that number
* @returns {number} the base according to the list above (`"braille"` will be `0`), or `NaN` if not supported
*/
static #strBase(str){
if(!Number.isNaN(Number(str))){
str=Number(str);
return str>=0&&str<=0x100000000?str:NaN;
}
switch(String(str).toLowerCase()){//~ ( https://en.wikipedia.org/wiki/list_of_numeral_systems#standard_positional_numeral_systems )
//~ > "ALL YOUR BASE ARE BELONG TO US" --CATS
case'b':case"bin":case"bits":case"binary":case"1bit":return 2;
case"ternary":case"trinary":return 3;
case'q':case"quaternary":case"2bit":return 4;
case"quinary":case"pental":return 5;
case"senary":case"heximal":case"seximal":return 6;
case"septenary":return 7;
case'o':case"oct":case"octal":case"3bit":return 8;
case"nonary":return 9;
case'd':case"dec":case"decimal":case"denary":return 0xA;
case"undecimal":return 0xB;
case"duodecimal":case"dozenal":case"uncial":return 0xC;
case"tridecimal":return 0xD;
case"tetradecimal":return 0xE;
case"pentadecimal":return 0xF;
case'h':case"hex":case"hexadecimal":case"sexadecimal":case"4bit":return 0x10;
case"heptadecimal":return 0x11;
case"octodecimal":return 0x12;
case"enneadecimal":return 0x13;
case"vigesimal":return 0x14;
case"unvigesimal":return 0x15;
case"duovigesimal":return 0x16;
case"trivigesimal":return 0x17;
case"tetravigesimal":return 0x18;
case"pentavigesimal":return 0x19;
case"hexavigesimal":return 0x1A;
case"heptavigesimal":case"septemvigesimal":return 0x1B;
case"octovigesimal":return 0x1C;
case"enneavigesimal":return 0x1D;
case"trigesimal":return 0x1E;
case"untrigesimal":return 0x1F;
case"duotrigesimal":case"5bit":return 0x20;
case"tritrigesimal":return 0x21;
case"tetratrigesimal":return 0x22;
case"pentatrigesimal":return 0x23;
case't':case"txt":case"text":case"hexatrigesimal":return 0x24;
case"heptatrigesimal":return 0x25;
case"octotrigesimal":return 0x26;
case"enneatrigesimal":return 0x27;
case"quadragesimal":return 0x28;
case"duoquadragesimal":return 0x2A;
case"pentaquadragesimal":return 0x2D;
case"septaquadragesimal":return 0x2F;
case"octoquadragesimal":return 0x30;
case"enneaquadragesimal":return 0x31;
case"quinquagesimal":return 0x32;
case"duoquinquagesimal":return 0x34;
case"tetraquinquagesimal":return 0x36;
case"hexaquinquagesimal":return 0x38;
case"heptaquinquagesimal":return 0x39;
case"octoquinquagesimal":return 0x3A;
case"sexagesimal":case"sexagenary":return 0x3C;
case"duosexagesimal":return 0x3E;
case"tetrasexagesimal":case"6bit":return 0x40;
case"duoseptuagesimal":return 0x48;
case"octogesimal":return 0x50;
case"unoctogesimal":return 0x51;
case"pentoctogesimal":return 0x55;
case"enneaoctogesimal":return 0x59;
case"nonagesimal":return 0x5A;
case"unnonagesimal":return 0x5B;
case"duononagesimal":return 0x5C;
case"trinonagesimal":return 0x5D;
case"tetranonagesimal":return 0x5E;
case"pentanonagesimal":return 0x5F;
case"hexanonagesimal":return 0x60;
case"septanonagesimal":return 0x61;
case"centesimal":return 0x64;
case"centevigesimal":return 0x78;
case"centeunvigesimal":return 0x79;
case"centepentavigesimal":return 0x7D;
case"centeoctovigesimal":case"7bit":return 0x80;
case"centetetraquadragesimal":return 0x90;
case"centenovemsexagesimal":return 0xA9;
case"centepentoctogesimal":return 0xB9;
case"centehexanonagesimal":return 0xC4;
case"duocentesimal":return 0xC8;
case"duocentedecimal":return 0xD2;
case"duocentehexidecimal":return 0xD8;
case"duocentepentavigesimal":return 0xE1;
case"duocentehexaquinquagesimal":case"byte":case"8bit":return 0x100;
case"braille":return 0;//~ braille-pattern (unicode)
case"trecentesimal":return 0x12C;
case"trecentosexagesimal":return 0x168;
case"9bit":return 0x200;
case"10bit":return 0x400;
case"11bit":return 0x800;
case"12bit":return 0x1000;
case"13bit":return 0x2000;
case"14bit":return 0x4000;
case"15bit":return 0x8000;
case"16bit":return 0x10000;
case"17bit":return 0x20000;
case"18bit":return 0x40000;
case"19bit":return 0x80000;
case"20bit":return 0x100000;
case"21bit":return 0x200000;
case"22bit":return 0x400000;
case"23bit":return 0x800000;
case"24bit":return 0x1000000;
case"25bit":return 0x2000000;
case"26bit":return 0x4000000;
case"27bit":return 0x8000000;
case"28bit":return 0x10000000;
case"29bit":return 0x20000000;
case"30bit":return 0x40000000;
case"31bit":return 0x80000000;
case"32bit":return 0x100000000;
}
return NaN;
}
/**
* __constructs a RegExp to match a number string in base `base`__ \
* _min 1 digit_ \
* allows prefixes for bases 2, 8, and 16 \
* allows '_' between digits \
* __supports bases 1 to 36 (incl.)__ \
* __base 0 is braille pattern__ \
* match-groups:
* 1. sign / null
* 2. number
* @param {number} base - the base for the RegExp wich checks against a number-string - save integer
* + base "braille" (0) [digits `⠀`-`⣿`]
* + base 1-10 [digits `0`-`9`]
* + base 11-36 [digits `0`-`9` and `A`-`F`]
* @returns {RegExp} the regexp for number-strings with base `base`
* @throws {RangeError} if `base` is not a save integer or bigger than `36`
*/
static #REGEXP_STRING(base){
base=Math.abs(Number(base));if(!Number.isSafeInteger(base)||base>0x10)throw RangeError("[REGEXP_STRING] base is out of range");
switch(Number(base)){//~ special cases
case 0:return/^([+-]?)((?:\u2800|[\u2801-\u28FF][\u2800-\u28FF]*)+(?:_(?:\u2800|[\u2801-\u28FF][\u2800-\u28FF]*)+)*)$/;//~ base 256 in braille-patterns
case 1:return/^([+-]?)(0+(?:_0+)*)$/;//~ length is the number value -1, so "0" is 0, "00" is 1, "000" is 2, etc.
case 2:return/^([+-]?)(?:0b)?((?:0|1[01]*)+(?:_(?:0|1[01]*)+)*)$/i;
case 8:return/^([+-]?)(?:0o)?((?:0|[1-7][0-7]*)+(?:_(?:0|[1-7][0-7]*)+)*)$/i;
case 0x10:return/^([+-]?)(?:0x)?((?:0|[1-9A-F][0-9A-F]*)+(?:_(?:0|[1-9A-F][0-9A-F]*)+)*)$/i;
}
const add_char_seperator=characters=>`(?:${characters})+(?:_(?:${characters})+)*`,
construct_regexp=(number_regexp,flag='')=>new RegExp(`^([+-]?)(${number_regexp})$`,flag);
if(base<=0xA){//~ base 2-10
const characters=`0|[1-${base-1}][0-${base-1}]*`;
return construct_regexp(add_char_seperator(characters));
}else if(base<=0x10){//~ base 11-36
const characters=`0|[1-9A-${String.fromCharCode(0x36+base)}][0-9A-${String.fromCharCode(0x36+base)}]*`;
return construct_regexp(add_char_seperator(characters),'i');
}
return /^([+-]?)(0?)$/i;
}
/**
* @readonly
* @returns {RegExp} regexp for comma separated number (list) - match groups: [sign, number]
*/
static get #REGEXP_CSNUM(){return/^([+-]?)((?:0|[1-9][0-9]*(?:_[0-9]+)*)(?:\,(?:0|[1-9][0-9]*(?:_[0-9]+)*))*)$/;}
/**
* @type {boolean} - sign of the number - `true` = positive
*/
#sign=true;
/**
* @readonly
* @returns {boolean} sign of the number - `true` = positive
*/
get sign(){return this.#sign;}
/**
* @type {Uint8Array} - the number as unsigned 8bit integer array (base 256) - index 0 is the 0st-digit of the number
*/
#digits=new Uint8Array(1);
/**
* @readonly
* @returns {Uint8Array} a copy of the digits as an unsigned 8bit integer array (base 256) - index 0 is the 0st-digit of the number
* @throws {RangeError} - _if some Array could not be allocated (system-specific & memory size)_
*/
get digits(){return this.#digits.slice();}
/**
* @readonly
* @returns {number} number of digits (base 256)
*/
get length(){return this.#digits.length;}
/**
* @readonly
* @returns {BigIntType} biggest possible number according to `MAX_SIZE`
* @throws {RangeError} - _if some Array could not be allocated (system-specific & memory size)_
*/
static get MAX_VALUE(){return new BigIntType(new Uint8Array(BigIntType.MAX_SIZE).fill(0xFF),0x100);}
/**
* @readonly
* @returns {BigIntType} "Hello There" in Braille - see `toString("braille")`
* @throws {RangeError} - if current `MAX_SIZE` is to small - requires 22B
* @throws {RangeError} - _if some Array could not be allocated (system-specific & memory size)_
*/
static get HelloThere(){
if(BigIntType.MAX_SIZE<0x16)throw new RangeError("[HelloThere] MAX_SIZE is to small");
return new BigIntType(Uint8Array.of(0x41,0xEF,0x85,0x5F,0x41,0xEF,0x47,0x67,1,0xB9,0,0,0x47,0xCF,0x40,0xC7,0x40,0xC7,0x41,0xEF,0x47,0x67),0x100);
}
/**
* @readonly
* @returns {BigIntType} `Infinity` (`Number`) - `2**1024` (use `isFinite` for checking a number below or equal to `Number.MAX_VALUE`)
* @throws {RangeError} - if current `MAX_SIZE` is to small - requires 129B
* @throws {RangeError} - _if some Array could not be allocated (system-specific & memory size)_
*/
static get Infinity(){
if(BigIntType.MAX_SIZE<0x81)throw new RangeError("[Infinity] MAX_SIZE is to small");
return new BigIntType(Uint8Array.of(...new Uint8Array(0x80),1),0x100);
}
/**
* @readonly
* @returns {BigIntType} `0`
* @throws {RangeError} - _if some Array could not be allocated (system-specific & memory size)_
*/
static get Zero(){return new BigIntType(Uint8Array.of(0),0x100);}
/**
* @readonly
* @returns {BigIntType} `1`
* @throws {RangeError} - _if some Array could not be allocated (system-specific & memory size)_
*/
static get One(){return new BigIntType(Uint8Array.of(1),0x100);}
/**
* @readonly
* @returns {BigIntType} `2`
* @throws {RangeError} - _if some Array could not be allocated (system-specific & memory size)_
*/
static get Two(){return new BigIntType(Uint8Array.of(2),0x100);}
/**
* @readonly
* @returns {BigIntType} `-1`
* @throws {RangeError} - _if some Array could not be allocated (system-specific & memory size)_
*/
static get NOne(){return BigIntType.One.neg();}
/**
* @readonly
* @returns {BigIntType} `-2`
* @throws {RangeError} - _if some Array could not be allocated (system-specific & memory size)_
*/
static get NTwo(){return BigIntType.Two.neg();}
/**
* __constructs a BigIntType number__
* @param {string|bigint|number|number[]|Uint8Array} num
* * an integer - _default `'1'`_
* * note:
* * ( `base` is ignored if `num` is of type `bigint` or `number` )
* * ( in arrays the number is unsigned/positive and index 0 = 0th-place-digit for example: `"1230"` → `[0,3,2,1]` )
* * ( if `num` is an Uint8Array and `base` 256 then the original Uint8Array will be used )
* - type `number` and `number[]` must be integers
* - `base` `1` → as a string or an array of just 0s ( length of the number, minus one, equals the numerical value of it )
* - `base` `2` to `36` → as a string with alphanumeric symbols (0 to 9 and A to Z) or an array
* - `base` `37` to `4294967296` → as a string as comma-separated list of numbers ( can use sign ) or an array
* - `base` `"braille"` (256) → as a string with characters "⠀" to "⣿" ( braille-patterns U+2800 to U+28FF ) (can use sign)
* @param {string|number} base
* * base of `num` as a number or string ( case insensitive ) - _default `10`_
* - base braille ← `0` or `"braille"` ( must be a string with `'⠀'`-`'⣿'` (Unicode Braille Pattern `0x2800`-`0x28FF`) )
* - base 2 ← `'b'`, `"bin"`, `"bits"`, `"binary"`, or `"1bit"`
* - base 3 ← `"ternary"` or `"trinary"`
* - base 4 ← `'q'`, `"quaternary"`, or `"2bit"`
* - base 5 ← `"quinary"` or `"pental"`
* - base 6 ← `"senary"`, `"heximal"`, or `"seximal"`
* - base 7 ← `"septenary"`
* - base 8 ← `'o'`, `"oct"`, `"octal"`, or `"3bit"`
* - base 9 ← `"nonary"`
* - base 10 ← `'d'`, `"dec"`, `"decimal"`, or `"denary"`
* - base 11 ← `"undecimal"`
* - base 12 ← `"duodecimal"`, `"dozenal"`, or `"uncial"`
* - base 13 ← `"tridecimal"`
* - base 14 ← `"tetradecimal"`
* - base 15 ← `"pentadecimal"`
* - base 16 ← `'h'`, `"hex"`, `"hexadecimal"`, `"sexadecimal"`, or `"4bit"`
* - base 17 ← `"heptadecimal"`
* - base 18 ← `"octodecimal"`
* - base 19 ← `"enneadecimal"`
* - base 20 ← `"vigesimal"`
* - base 21 ← `"unvigesimal"`
* - base 22 ← `"duovigesimal"`
* - base 23 ← `"trivigesimal"`
* - base 24 ← `"tetravigesimal"`
* - base 25 ← `"pentavigesimal"`
* - base 26 ← `"hexavigesimal"`
* - base 27 ← `"heptavigesimal septemvigesimal"`
* - base 28 ← `"octovigesimal"`
* - base 29 ← `"enneavigesimal"`
* - base 30 ← `"trigesimal"`
* - base 31 ← `"untrigesimal"`
* - base 32 ← `"duotrigesimal"` or `"5bit"`
* - base 33 ← `"tritrigesimal"`
* - base 34 ← `"tetratrigesimal"`
* - base 35 ← `"pentatrigesimal"`
* - base 36 ← `'t'`, `"txt"`, `"text"`, or `"hexatrigesimal"`
* - base 37 ← `"heptatrigesimal"`
* - base 38 ← `"octotrigesimal"`
* - base 39 ← `"enneatrigesimal"`
* - base 40 ← `"quadragesimal"`
* - base 42 ← `"duoquadragesimal"`
* - base 45 ← `"pentaquadragesimal"`
* - base 47 ← `"septaquadragesimal"`
* - base 48 ← `"octoquadragesimal"`
* - base 49 ← `"enneaquadragesimal"`
* - base 50 ← `"quinquagesimal"`
* - base 52 ← `"duoquinquagesimal"`
* - base 54 ← `"tetraquinquagesimal"`
* - base 56 ← `"hexaquinquagesimal"`
* - base 57 ← `"heptaquinquagesimal"`
* - base 58 ← `"octoquinquagesimal"`
* - base 60 ← `"sexagesimal"` or `"sexagenary"`
* - base 62 ← `"duosexagesimal"`
* - base 64 ← `"tetrasexagesimal"` or `"6bit"`
* - base 72 ← `"duoseptuagesimal"`
* - base 80 ← `"octogesimal"`
* - base 81 ← `"unoctogesimal"`
* - base 85 ← `"pentoctogesimal"`
* - base 89 ← `"enneaoctogesimal"`
* - base 90 ← `"nonagesimal"`
* - base 91 ← `"unnonagesimal"`
* - base 92 ← `"duononagesimal"`
* - base 93 ← `"trinonagesimal"`
* - base 94 ← `"tetranonagesimal"`
* - base 95 ← `"pentanonagesimal"`
* - base 96 ← `"hexanonagesimal"`
* - base 97 ← `"septanonagesimal"`
* - base 100 ← `"centesimal"`
* - base 120 ← `"centevigesimal"`
* - base 121 ← `"centeunvigesimal"`
* - base 125 ← `"centepentavigesimal"`
* - base 128 ← `"centeoctovigesimal"` or `"7bit"`
* - base 144 ← `"centetetraquadragesimal"`
* - base 169 ← `"centenovemsexagesimal"`
* - base 185 ← `"centepentoctogesimal"`
* - base 196 ← `"centehexanonagesimal"`
* - base 200 ← `"duocentesimal"`
* - base 210 ← `"duocentedecimal"`
* - base 216 ← `"duocentehexidecimal"`
* - base 225 ← `"duocentepentavigesimal"`
* - base 256 ← `"duocentehexaquinquagesimal"`, `"byte"`, or `"8bit"`
* - base 300 ← `"trecentesimal"`
* - base 360 ← `"trecentosexagesimal"`
* - base 512 ← `"9bit"`
* - base 1024 ← `"10bit"`
* - base 2048 ← `"11bit"`
* - base 4096 ← `"12bit"`
* - base 8192 ← `"13bit"`
* - base 16384 ← `"14bit"`
* - base 32768 ← `"15bit"`
* - base 65536 ← `"16bit"`
* - base 131072 ← `"17bit"`
* - base 262144 ← `"18bit"`
* - base 524288 ← `"19bit"`
* - base 1048576 ← `"20bit"`
* - base 2097152 ← `"21bit"`
* - base 4194304 ← `"22bit"`
* - base 8388608 ← `"23bit"`
* - base 16777216 ← `"24bit"`
* - base 33554432 ← `"25bit"`
* - base 67108864 ← `"26bit"`
* - base 134217728 ← `"27bit"`
* - base 268435456 ← `"28bit"`
* - base 536870912 ← `"29bit"`
* - base 1073741824 ← `"30bit"`
* - base 2147483648 ← `"31bit"`
* - base 4294967296 ← `"32bit"`
* - any base within 1 to 4294967296 (incl.) can also be a number or a string representing that number
* @throws {SyntaxError} if `base` is not an available option _( outside the range of [0-4294967296] (incl.) )_
* @throws {RangeError} if `num` is not an integer when type `number` or an integer array when type `number[]`
* @throws {RangeError} if `num` as array has incorrect values according to `base`
* @throws {SyntaxError} if `base` is `"braille"` and `num` is not a string
* @throws {SyntaxError} if `num` does not have the correct format for this `base`
* @throws {RangeError} if `num` exceedes `MAX_SIZE` ( after conversion in base 256 )
* @throws {RangeError} _if some Array could not be allocated ( system-specific & memory size )_
*/
constructor(num='1',base=0xA){
/**@type {string|bigint|number|number[]|Uint8Array} current digits array - content and type might change*/
let currentDigits=num,
/**@type {number} current base of `currentDigits` - value might change*/
currentBase=BigIntType.#strBase(base);
if(Number.isNaN(currentBase))throw new SyntaxError("[new BigIntType] base is not an available option");
this.#sign=true;
if(currentDigits instanceof Uint8Array){
if(currentDigits.some(v=>v>=currentBase))throw new RangeError(`[new BigIntType] num (Uint8Array) has incorrect values for base ${currentBase}`);
}else if(Array.isArray(currentDigits)){
let count=0;
currentDigits.forEach(v=>{
if(typeof v!=="number")throw new SyntaxError("[new BigIntType] num is not an integer array");
if(v<0||v>=currentBase)throw new RangeError(`[new BigIntType] num (integer array) has incorrect values for base ${currentBase}`);
count++;
});
if(currentDigits.length!==count)throw new SyntaxError("[new BigIntType] num (array) has empty entries / holes");
if(currentBase<=0x100)currentDigits=Uint8Array.from(currentDigits);
}else if(typeof currentDigits==="number"){
if(!Number.isInteger(currentDigits))throw new RangeError("[new BigIntType] num is not an integer");
currentDigits=currentDigits.toString(0x10);
currentBase=0x10;
}else if(typeof currentDigits==="bigint"){
currentDigits=currentDigits.toString(0x10);
currentBase=0x10;
}else currentDigits=String(currentDigits).toUpperCase();
if(currentBase===0&&typeof currentDigits!=="string")throw new SyntaxError("[new BigIntType] base \"braille\" requires num to be a string");
if(typeof currentDigits==="string"){
if(currentBase<=0x24){
/**@type {RegExpMatchArray|null} - sign and number from string or `null` if no match*/
let _match=currentDigits.match(BigIntType.#REGEXP_STRING(currentBase));
if(!_match)throw new SyntaxError(`[new BigIntType] num (string) does not have the correct format for base ${currentBase===0?'256 (braille)':currentBase}`);
this.#sign=_match[1]!=='-';
_match[2]=_match[2].replace('_','');
if(currentBase===0){//~ braille-pattern
currentBase=0x100;
currentDigits=Uint8Array.from(_match[2].split('').reverse(),v=>v.charCodeAt(0)-0x2800);
}else if(currentBase===1){//~ length-1 = numerical value
currentBase=0x10;
currentDigits=Uint8Array.from((_match[2].length-1).toString(currentBase).split('').reverse(),v=>Number.parseInt(v,currentBase));
}else currentDigits=Uint8Array.from(_match[2].split('').reverse(),v=>Number.parseInt(v,currentBase));
}else{
const CSNumError=new SyntaxError(`[new BigIntType] num (string / comma separated list) does not have the correct format for base ${currentBase}`);
let _match=currentDigits.match(BigIntType.#REGEXP_CSNUM);
if(!_match)throw CSNumError;
const _digits=_match[2].replace('_','').split(',').reverse(),
baseStr=String(currentBase);
for(let i=0;i<_digits.length;i++){
if(_digits[i].length>baseStr.length)throw CSNumError;
if(_digits[i].length===baseStr.length)
for(let j=0;j<baseStr.length;j++){
if(_digits[i].charCodeAt(j)>baseStr.charCodeAt(j))throw CSNumError;
if(_digits[i].charCodeAt(j)<baseStr.charCodeAt(j))break;
if(j===baseStr.length-1)throw CSNumError;
}
}
this.#sign=_match[1]!=='-';
if(currentBase<=0x100)currentDigits=Uint8Array.from(_digits,Number);
else currentDigits=Array.from(_digits,Number);
}
}else if(currentBase===1){//~ length-1 = numerical value
currentBase=0x10;
currentDigits=Uint8Array.from((currentDigits.length-1).toString(currentBase).split('').reverse(),v=>Number.parseInt(v,currentBase));
}
//! here type of currentDigits is Uint8Array or number[] when base > 256
switch(currentBase){
case 2://~ 8* digits are 1 8bit digit
this.#digits=new Uint8Array(Math.ceil(currentDigits.length/8));
for(let i=0;i<this.length;i++)
this.#digits[i]=currentDigits[i*8]
+((currentDigits[i*8+1]??0)<<1)
+((currentDigits[i*8+2]??0)<<2)
+((currentDigits[i*8+3]??0)<<3)
+((currentDigits[i*8+4]??0)<<4)
+((currentDigits[i*8+5]??0)<<5)
+((currentDigits[i*8+6]??0)<<6)
+((currentDigits[i*8+7]??0)<<7);
break;
case 4://~ 4* digits are 1 8bit digit
this.#digits=new Uint8Array(Math.ceil(currentDigits.length/4));
for(let i=0;i<this.length;i++)
this.#digits[i]=currentDigits[i*4]
+((currentDigits[i*4+1]??0)<<2)
+((currentDigits[i*4+2]??0)<<4)
+((currentDigits[i*4+3]??0)<<6);
break;
case 0x10://~ 2* digits are 1 8bit digit
this.#digits=new Uint8Array(Math.ceil(currentDigits.length/2));
for(let i=0;i<this.length;i++)
this.#digits[i]=currentDigits[i*2]
+((currentDigits[i*2+1]??0)<<4);
break;
case 0x100://~ copy memory adress of original
//@ts-ignore currentDigits type here is Uint8Array only
this.#digits=currentDigits;
break;
case 0x10000://~ each digit is two 8bit digits (2**(2*8) = 65536)
//~ (check the size before creating because it could be bigger than MAX_SAVE_INTEGER and is impossible for Uint8Array to create)
if(currentDigits.length*2>BigIntType.MAX_SIZE)throw new RangeError(`[new BigIntType] new number is longer than [MAX_SIZE]`);
this.#digits=new Uint8Array(currentDigits.length*2);
for(let i=0;i<currentDigits.length;i++){
this.#digits[2*i]=currentDigits[i]&0xFF;//~ (2**8-1) = 255
this.#digits[2*i+1]=currentDigits[i]&0xFF00;//~ (2**16-1)-(2**8-1) = 65280
}
break;
case 0x1000000://~ each digit is three 8bit digits (2**(3*8) = 16777216)
//~ (check the size before creating because it could be bigger than MAX_SAVE_INTEGER and is impossible for Uint8Array to create)
if(currentDigits.length*3>BigIntType.MAX_SIZE)throw new RangeError(`[new BigIntType] new number is longer than [MAX_SIZE]`);
this.#digits=new Uint8Array(currentDigits.length*3);
for(let i=0;i<currentDigits.length;i++){
this.#digits[3*i]=currentDigits[i]&0xFF;//~ (2**8-1) = 255
this.#digits[3*i+1]=currentDigits[i]&0xFF00;//~ (2**16-1)-(2**8-1) = 65280
this.#digits[3*i+2]=currentDigits[i]&0xFF0000;//~ (2**24-1)-(2**16-1) = 16711680
}
break;
case 0x100000000://~ each digit is four 8bit digits (2**(4*8) = 4294967296)
//~ (check the size before creating because it could be bigger than MAX_SAVE_INTEGER and is impossible for Uint8Array to create)
if(currentDigits.length*4>BigIntType.MAX_SIZE)throw new RangeError(`[new BigIntType] new number is longer than [MAX_SIZE]`);
this.#digits=new Uint8Array(currentDigits.length*4);
for(let i=0;i<currentDigits.length;i++){
//~ ()>>>0 to make the 32bit number unsigned (it is 32bit because of the use of bitwise operations - but also default signed so realy only 31bit)
this.#digits[4*i]=(currentDigits[i]&0xFF)>>>0;//~ (2**8-1) = 255
this.#digits[4*i+1]=(currentDigits[i]&0xFF00)>>>0;//~ (2**16-1)-(2**8-1) = 65280
this.#digits[4*i+2]=(currentDigits[i]&0xFF0000)>>>0;//~ (2**24-1)-(2**16-1) = 16711680
this.#digits[4*i+3]=(currentDigits[i]&0xFF000000)>>>0;//~ (2**32-1)-(2**24-1) = 4294967296
}
break;
default:
//~ (because of the use of bitwise operations the numbers of the "base" var and each digit in bN can only get to 32bit (and allways needs `>>>` because of sign support of js bitwise operations))
/**@type {number[]} - digits in base `base`*/
let bN=Array.from(currentDigits,Number),
/**@type {string} - digits for base 256*/
b256__="";
for(let z=0;bN.length>1||bN[0]!==0;z=0){//~ bN > 0
for(let iBit=0;iBit<8;iBit++){
//~ if the base is odd AND bN is not smaller than its base (i.e. more than one digit), invert the standard behavior, which is → when bN is odd, push a bit to the end of b256__
if(((currentBase&1)&(bN.length>1?1:0))^(bN[0]&1))z+=1<<iBit;
//~ bN >>>= 1
if(bN.length===1&&bN[0]===0)break;
else if(bN.length===1&&bN[0]===1){
bN[0]=0;
break;
}else{
if((currentBase&1)===1){//~ if base is odd the carry is...special
/**@type {undefined[]|null[]} - carry if `[i]` is `null`*/
let numCarry=Array(bN.length).fill(undefined),
/**@type {boolean} - true if it's currently durring a carry*/
numCarryLast=false
for(let i=0;i<bN.length;i++){//~ bN>>>=1 and save carry
if((bN[i]&1)===1)numCarry[i]=null;
bN[i]>>>=1;
}
for(let i=numCarry.length-1;i>=0;i--){//~ apply carry
if(numCarryLast)bN[i]+=Math.floor(currentBase*.5);
if(numCarry[i]===null){
if(numCarryLast)bN[i]++;
numCarryLast=!numCarryLast;
}
}
//~ with rounding this would be added → if(numCarryLast){bN[0]=String(Number(bN[0])+1);}
}else{
bN[0]>>>=1;
for(let i=1;i<bN.length;i++){
if((bN[i]&1)===1)bN[i-1]+=Math.floor(currentBase*.5);
bN[i]>>>=1;
}
}
if(bN.length>1&&bN[bN.length-1]===0)bN.pop();
}
}
b256__+=String.fromCharCode(z);
}
this.#digits=b256__===""?Uint8Array.of(0):Uint8Array.from(b256__,v=>v.charCodeAt(0));
break;
}
this.#digits=BigIntType.#removeLeadingZeros(this.#digits);//~ should always return self (unmodified/original)
if(this.length>BigIntType.MAX_SIZE)throw new RangeError(`[new BigIntType] new number is longer than [MAX_SIZE]`);
}
/**
* __initialize iterator for `this` number__ \
* iterate through each digit in base 256 in reading order (starting with biggest numerical index - reverse of `digits`) \
* generator makes a copy of the digits array at start (modifications to `this` number during iteration do not affect this generator) \
* _( makes `this` able to be used in a `for-of` loop )_
* @readonly
* @returns {Generator<number,void,unknown>} generator/iterator to get the next digit (8 bit `Number`) in a loop
*/
get[Symbol.iterator](){return (function*(digits){for(let i=digits.length-1;i>=0;i--)yield digits[i];})(this.#digits.slice());}
/**
* __get the object (string) descriptor__
* @readonly
*/
get[Symbol.toStringTag](){return "BigIntType"}
/**
* __convert `this` number to the closest (numeric) primitive (`BigInt`)__
* @returns {bigint} `this` number as a `BigInt` number
*/
valueOf(){return this.toBigInt();}
/**
* __converts `this` number to `Number` type__ \
* _may be +/- `Infinity`_
* @returns {Number} `this` number as a `Number` type
*/
toNumber(){return this.isFinite()?Number.parseInt(this.toString(0x10),0x10):(this.#sign?Infinity:-Infinity);}
/**
* __converts `this` number to `BigInt` type__
* @returns {bigint} `this` number as a `BigInt` type
*/
toBigInt(){
if(this.#sign)return BigInt(this.toString(0x10));
const out=BigInt(this.abs().toString(0x10))*-1n;
this.neg();
return out;
}
/**
* __convert `this` number to string__
* @description
* - output:
* - base `2` will have the prefix `"0b"`
* - base `8` will have the prefix `"0o"`
* - base `16` will have the prefix `"0x"`
* - base `"braille"` will be a string with `'⠀'`-`'⣿'` (Unicode Braille Pattern `0x2800`-`0x28FF`)
* - bases `2` to `10` use `'0'`-`'9'`
* - bases `11` to `36` use `'0'`-`'9'` and `'A'`-`'Z'`
* - bases `37` and above will be comma-separated lists of numbers, where each number represents the numerical value (in base 10) of the character at that location
* * base `1` is not feasible at all because it gets way too big way too quick!
* * `[!]` every base except "braille" would be impossible to generate eventually \
* because of strings maximum length of `2**53-1` (`9007199254740991`) \
* characters (`MAX_SAVE_INTEGER`)
* * `[!]` also see https://stackoverflow.com/a/65570725/13282166 as example \
* of a realistic limit of string size would be in modern browsers \
* (spoiler: it's way less than the spec limit)
* @param {string|number} base
* * base of `num` as a number or string ( case insensitive ) - _default `16`_
* - base braille ← `0` or `"braille"` ( must be a string with `'⠀'`-`'⣿'` (Unicode Braille Pattern `0x2800`-`0x28FF`) )
* - base 2 ← `'b'`, `"bin"`, `"bits"`, `"binary"`, or `"1bit"`
* - base 3 ← `"ternary"` or `"trinary"`
* - base 4 ← `'q'`, `"quaternary"`, or `"2bit"`
* - base 5 ← `"quinary"` or `"pental"`
* - base 6 ← `"senary"`, `"heximal"`, or `"seximal"`
* - base 7 ← `"septenary"`
* - base 8 ← `'o'`, `"oct"`, `"octal"`, or `"3bit"`
* - base 9 ← `"nonary"`
* - base 10 ← `'d'`, `"dec"`, `"decimal"`, or `"denary"`
* - base 11 ← `"undecimal"`
* - base 12 ← `"duodecimal"`, `"dozenal"`, or `"uncial"`
* - base 13 ← `"tridecimal"`
* - base 14 ← `"tetradecimal"`
* - base 15 ← `"pentadecimal"`
* - base 16 ← `'h'`, `"hex"`, `"hexadecimal"`, `"sexadecimal"`, or `"4bit"`
* - base 17 ← `"heptadecimal"`
* - base 18 ← `"octodecimal"`
* - base 19 ← `"enneadecimal"`
* - base 20 ← `"vigesimal"`
* - base 21 ← `"unvigesimal"`
* - base 22 ← `"duovigesimal"`
* - base 23 ← `"trivigesimal"`
* - base 24 ← `"tetravigesimal"`
* - base 25 ← `"pentavigesimal"`
* - base 26 ← `"hexavigesimal"`
* - base 27 ← `"heptavigesimal septemvigesimal"`
* - base 28 ← `"octovigesimal"`
* - base 29 ← `"enneavigesimal"`
* - base 30 ← `"trigesimal"`
* - base 31 ← `"untrigesimal"`
* - base 32 ← `"duotrigesimal"` or `"5bit"`
* - base 33 ← `"tritrigesimal"`
* - base 34 ← `"tetratrigesimal"`
* - base 35 ← `"pentatrigesimal"`
* - base 36 ← `'t'`, `"txt"`, `"text"`, or `"hexatrigesimal"`
* - base 37 ← `"heptatrigesimal"`
* - base 38 ← `"octotrigesimal"`
* - base 39 ← `"enneatrigesimal"`
* - base 40 ← `"quadragesimal"`
* - base 42 ← `"duoquadragesimal"`
* - base 45 ← `"pentaquadragesimal"`
* - base 47 ← `"septaquadragesimal"`
* - base 48 ← `"octoquadragesimal"`
* - base 49 ← `"enneaquadragesimal"`
* - base 50 ← `"quinquagesimal"`
* - base 52 ← `"duoquinquagesimal"`
* - base 54 ← `"tetraquinquagesimal"`
* - base 56 ← `"hexaquinquagesimal"`
* - base 57 ← `"heptaquinquagesimal"`
* - base 58 ← `"octoquinquagesimal"`
* - base 60 ← `"sexagesimal"` or `"sexagenary"`
* - base 62 ← `"duosexagesimal"`
* - base 64 ← `"tetrasexagesimal"` or `"6bit"`
* - base 72 ← `"duoseptuagesimal"`
* - base 80 ← `"octogesimal"`
* - base 81 ← `"unoctogesimal"`
* - base 85 ← `"pentoctogesimal"`
* - base 89 ← `"enneaoctogesimal"`
* - base 90 ← `"nonagesimal"`
* - base 91 ← `"unnonagesimal"`
* - base 92 ← `"duononagesimal"`
* - base 93 ← `"trinonagesimal"`
* - base 94 ← `"tetranonagesimal"`
* - base 95 ← `"pentanonagesimal"`
* - base 96 ← `"hexanonagesimal"`
* - base 97 ← `"septanonagesimal"`
* - base 100 ← `"centesimal"`
* - base 120 ← `"centevigesimal"`
* - base 121 ← `"centeunvigesimal"`
* - base 125 ← `"centepentavigesimal"`
* - base 128 ← `"centeoctovigesimal"` or `"7bit"`
* - base 144 ← `"centetetraquadragesimal"`
* - base 169 ← `"centenovemsexagesimal"`
* - base 185 ← `"centepentoctogesimal"`
* - base 196 ← `"centehexanonagesimal"`
* - base 200 ← `"duocentesimal"`
* - base 210 ← `"duocentedecimal"`
* - base 216 ← `"duocentehexidecimal"`
* - base 225 ← `"duocentepentavigesimal"`
* - base 256 ← `"duocentehexaquinquagesimal"`, `"byte"`, or `"8bit"`
* - base 300 ← `"trecentesimal"`
* - base 360 ← `"trecentosexagesimal"`
* - base 512 ← `"9bit"`
* - base 1024 ← `"10bit"`
* - base 2048 ← `"11bit"`
* - base 4096 ← `"12bit"`
* - base 8192 ← `"13bit"`
* - base 16384 ← `"14bit"`
* - base 32768 ← `"15bit"`
* - base 65536 ← `"16bit"`
* - base 131072 ← `"17bit"`
* - base 262144 ← `"18bit"`
* - base 524288 ← `"19bit"`
* - base 1048576 ← `"20bit"`
* - base 2097152 ← `"21bit"`
* - base 4194304 ← `"22bit"`
* - base 8388608 ← `"23bit"`
* - base 16777216 ← `"24bit"`
* - base 33554432 ← `"25bit"`
* - base 67108864 ← `"26bit"`
* - base 134217728 ← `"27bit"`
* - base 268435456 ← `"28bit"`
* - base 536870912 ← `"29bit"`
* - base 1073741824 ← `"30bit"`
* - base 2147483648 ← `"31bit"`
* - base 4294967296 ← `"32bit"`
* - any base within 2 to 4294967296 (incl.) can also be a number or a string representing that number
* @returns {string} `this` number as string (in base `base`)
* @throws {SyntaxError} if `base` is not an available option _( outside the range of [0;2-4294967296] (incl.) )_
* @throws {RangeError} - _if the string could not be allocated ( system-specific & memory size )_
*/
toString(base=0x10){
let out="";
base=BigIntType.#strBase(base);
if(Number.isNaN(base)||base===1)throw new SyntaxError("[toString] base is not an available option");
switch(base){
case 0://~ braille-pattern
for(let i=this.length-1;i>=0;i--)out+=String.fromCharCode(0x2800+this.#digits[i]);
break;
case 2://~ [2] 8* digits are 1 8bit digit
out="0b"+this.#digits[this.length-1].toString(2);
for(let i=this.length-2;i>=0;i--)out+=this.#digits[i].toString(2).padStart(8,'0');
break;
case 4://~ [4] 3* digits are 1 8bit digit
out=this.#digits[this.length-1].toString(4);
for(let i=this.length-2;i>=0;i--)out+=this.#digits[i].toString(4).padStart(4,'0');
break;
case 0x10://~ [16] 2* digits are 1 8bit digit
out="0x"+this.#digits[this.length-1].toString(0x10).toUpperCase();
for(let i=this.length-2;i>=0;i--)out+=this.#digits[i].toString(0x10).toUpperCase().padStart(2,'0');
break;
case 0x100://~ [256]
for(let i=this.length-1;i>=0;i--)out+=String(this.#digits[i])+(i!==0?',':'');
break;
case 0x10000://~ [65536] each digit is two 8bit digits (2**(2*8))
for(let i=0;i<this.length;i+=2)
out=String(
this.#digits[i]
+(this.#digits[i+1]<<8)
)+(i!==0?',':'')+out;
break;
case 0x1000000://~ [16777216] each digit is three 8bit digits (2**(3*8))
for(let i=0;i<this.length;i+=3)
out=String(
this.#digits[i]
+(this.#digits[i+1]<<8)
+(this.#digits[i+2]<<0x10)
)+(i!==0?',':'')+out;
break;
case 0x100000000://~ [4294967296] each digit is four 8bit digits (2**(4*8))
for(let i=0;i<this.length;i+=4)
out=String(
//~ ()>>>0 to make the 32bit number unsigned (it is 32bit because of the use of bitwise operations - but also default signed so realy only 31bit)
(this.#digits[i]>>>0)
+((this.#digits[i+1]<<8)>>>0)
+((this.#digits[i+2]<<0x10)>>>0)
+((this.#digits[i+3]<<0x18)>>>0)
)+(i!==0?',':'')+out;
break;
default:
if(base>=0xB&&base<=0x24){//~ i.e. use `Number.parseInt(X,base)` and `N.toString(base).toUpperCase()`
/**@type {string[]} - digit-array for output in base `base`*/
let bN=['0'];
for(let i=this.length-1;i>=0;i--)
for(let j=7;j>=0;j--){
//~ go through each bit ( pos = digits[i]&(1<<j) )
if(bN.length>1||bN[0]!=='0')//~ ( bN > 0 ) → bN <<= 1
for(let k=0,o=false,z=0;k<bN.length||o;k++){
if(bN[k]==='0'&&!o)continue;
z=(Number.parseInt(bN[k]??0,base)<<1)+(o?1:0);
if(z>=base){
bN[k]=(z-base).toString(base).toUpperCase();
o=true;
}else{
bN[k]=z.toString(base).toUpperCase();
o=false;
}
}
//~ add current bit to the beginning of bN and if the base is odd handle potential digit overflow
if((this.#digits[i]&(1<<j))!==0){
if(base&1){
const bN_0=Number.parseInt(bN[0],base)+1;
if(bN_0>=base){
bN[0]=(bN_0%base).toString(base).toUpperCase();
for(let k=1,o=true,z=0;k<bN.length;k++){//~ handle overflow
if(bN[k]==='0'&&!o)continue;
z=(Number.parseInt(bN[k]??0,base)<<1)+(o?1:0);
if(z>=base){
bN[k]=(z-base).toString(base).toUpperCase();
o=true;
}else{
bN[k]=z.toString(base).toUpperCase();
o=false;
}
}
}else bN[0]=(bN_0).toString(base).toUpperCase();
}else bN[0]=(Number.parseInt(bN[0],base)|1).toString(base).toUpperCase();
}
}
out=bN.reverse().join('');
}else{//~ just use `Number(X)` and `String(N)`
/** @type {string[]} - digit-array for output in base `base` */
let bN=['0'];
for(let i=this.length-1;i>=0;i--)
for(let j=7;j>=0;j--){
//~ go through each bit ( pos = digits[i]&(1<<j) )
if(bN.length>1||bN[0]!=='0')//~ ( bN > 0 ) → bN <<= 1
for(let k=0,o=false,z=0;k<bN.length||o;k++){
if(bN[k]==='0'&&!o)continue;
z=(Number(bN[k]??0)<<1)+(o?1:0);
if(z>=base){
bN[k]=String(z-base);
o=true;
}else{
bN[k]=String(z);
o=false;
}
}
//~ add current bit to the beginning of bN and if the base is odd handle potential digit overflow
if(((this.#digits[i]&(1<<j))!==0)){
if(base&1){
const bN_0=Number(bN[0])+1;
if(bN_0>=base){
bN[0]=String(bN_0%base);
for(let k=1,o=true,z=0;k<bN.length;k++){//~ handle overflow
if(bN[k]==='0'&&!o)continue;
z=(Number(bN[k]??0)<<1)+(o?1:0);
if(z>=base){
bN[k]=String(z-base);
o=true;