metanum 2.1.1 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1167 -1042
- package/format-metanum.js +2179 -2144
- package/metanum.js +8421 -7164
- package/package.json +30 -30
- package/test-interface.html +83 -83
- package/test_mixed.js +4804 -2755
package/format-metanum.js
CHANGED
|
@@ -1,2145 +1,2180 @@
|
|
|
1
|
-
// Author: dlsdl v2.
|
|
2
|
-
// v2.
|
|
3
|
-
// Code snippets and templates from format-omeganum.js
|
|
4
|
-
// Uses dlsdl's Letter Notation (see README.md)
|
|
5
|
-
|
|
6
|
-
// Set to 1 to print debug information to console
|
|
7
|
-
let FORMAT_DEBUG = 0
|
|
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
|
-
if (
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
//
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if (
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if (
|
|
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
|
-
for
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
//
|
|
190
|
-
let bottom = 1.0
|
|
191
|
-
let top = 10
|
|
192
|
-
if (r0[highest] === 8) {
|
|
193
|
-
// Letter level not present (e.g., Z10
|
|
194
|
-
height = highest + 1
|
|
195
|
-
repeat = 1
|
|
196
|
-
} else {
|
|
197
|
-
// Letter level present (e.g.,
|
|
198
|
-
repeat = 1 + repeat
|
|
199
|
-
}
|
|
200
|
-
if (FORMAT_DEBUG >= 1) console.log("metaPolarize all-8s:", {bottom, top, height, repeat})
|
|
201
|
-
return {bottom, top, height, repeat}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
//
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
let
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
cnt
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
bottom =
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
//
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
let
|
|
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
|
-
let
|
|
296
|
-
let
|
|
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
|
-
function
|
|
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
|
-
if (ptr
|
|
483
|
-
array[ptr][
|
|
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
|
-
let
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
if (
|
|
628
|
-
|
|
629
|
-
if (
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
if (
|
|
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
|
-
var
|
|
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
|
-
let
|
|
846
|
-
return regularFormat(
|
|
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
|
-
inner
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
let
|
|
895
|
-
let
|
|
896
|
-
let
|
|
897
|
-
//
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
return innerStr + "ε" + commaFormat(
|
|
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
|
-
function
|
|
980
|
-
let
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
}
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
}
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
//
|
|
1015
|
-
//
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
let
|
|
1044
|
-
let
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
//
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
//
|
|
1161
|
-
//
|
|
1162
|
-
let innerR0 = r0.slice(0)
|
|
1163
|
-
innerR0[maxLevel] = count - 1
|
|
1164
|
-
if (innerR0[maxLevel] === 0) {
|
|
1165
|
-
innerR0 = innerR0.slice(0, maxLevel)
|
|
1166
|
-
}
|
|
1167
|
-
let innerStr = formatR0AsChain(innerR0, precision, usedCount
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
if (
|
|
1175
|
-
|
|
1176
|
-
let
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
//
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
return
|
|
1233
|
-
}
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
//
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
if (!
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
//
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
let
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
if (
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
let
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
let
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
let
|
|
1369
|
-
let
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
}
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
let
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
}
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
}
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
}
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
if (
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
}
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
let
|
|
1735
|
-
if (
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
let
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
let
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
}
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
//
|
|
1753
|
-
//
|
|
1754
|
-
//
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
}
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
return emitLetterToken(
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
//
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
let
|
|
2007
|
-
let
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
let
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
let
|
|
2035
|
-
let
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
//
|
|
2051
|
-
let
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
return
|
|
2123
|
-
}
|
|
2124
|
-
|
|
2125
|
-
//
|
|
2126
|
-
let
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
//
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
1
|
+
// Author: dlsdl v2.2.0
|
|
2
|
+
// v2.2.0: Unify limit rules and fractional continuity; add iteraperiate with log/root inverses and ordinal-level root forms; fix formatting and large-base ordinal growth beyond y=100
|
|
3
|
+
// Code snippets and templates from format-omeganum.js
|
|
4
|
+
// Uses dlsdl's Letter Notation (see README.md)
|
|
5
|
+
|
|
6
|
+
// Set to 1 to print debug information to console
|
|
7
|
+
let FORMAT_DEBUG = 0
|
|
8
|
+
|
|
9
|
+
// keep the exact A(a×k) token past multiLetterLimit up to this many a's;
|
|
10
|
+
// beyond it, fall back to the historical single-letter carry approximation.
|
|
11
|
+
const MULTI_TOKEN_CAP = 64
|
|
12
|
+
|
|
13
|
+
// re-entrancy guard for formatSingleLetterType's "ordinary display" fallback
|
|
14
|
+
// (format may itself ask for a single-letter collapse through composeLetterChain)
|
|
15
|
+
let singleDepth = 0
|
|
16
|
+
|
|
17
|
+
// formatFull() keeps the raw canonical chain (unlimited letter combinations);
|
|
18
|
+
// format() post-processes it into the binary-letter-canonical form.
|
|
19
|
+
// The flag disables the ≤2 guards inside the chain builders while formatFull runs.
|
|
20
|
+
let fullChainMode = false
|
|
21
|
+
|
|
22
|
+
// ─── Configuration Options ────────────────────────────────────────
|
|
23
|
+
const FORMAT_OPTIONS = {
|
|
24
|
+
smallNotationUseE: true, //true/false
|
|
25
|
+
smallNotationThreshold: 3, //0~9
|
|
26
|
+
decimalPlaces: 3, //0~9
|
|
27
|
+
decimalThreshold: 3, //0~15
|
|
28
|
+
useCommas: true, //true/false
|
|
29
|
+
sciThreshold: 9, //0~15
|
|
30
|
+
sciSignificantDigits: 3, //0~9
|
|
31
|
+
sciDecimalThreshold: 3, //0~15
|
|
32
|
+
singleLetterDigits: 3, //0~9
|
|
33
|
+
repeatLetterThreshold: 3, //2~9
|
|
34
|
+
multiLetterDigits: 3, //0~9
|
|
35
|
+
multiLetterRepeatThreshold: 3,//2~9
|
|
36
|
+
multiLetterLimit: 4, //2~9
|
|
37
|
+
epsilonSignificantDigits: 6 //0~9
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// ─── Utility Functions ───────────────────────────────────────────
|
|
41
|
+
|
|
42
|
+
function commaFormat(num) {
|
|
43
|
+
if (!FORMAT_OPTIONS.useCommas) {
|
|
44
|
+
let n = typeof num === 'number' ? num : (num.toNumber ? num.toNumber() : Number(num))
|
|
45
|
+
if (isNaN(n) || !isFinite(n)) return "NaN"
|
|
46
|
+
return String(Math.floor(n))
|
|
47
|
+
}
|
|
48
|
+
if (num === null || num === undefined) return "NaN"
|
|
49
|
+
let n = typeof num === 'number' ? num : (num.toNumber ? num.toNumber() : Number(num))
|
|
50
|
+
if (isNaN(n) || !isFinite(n)) return "NaN"
|
|
51
|
+
if (n < 0.001) return "0"
|
|
52
|
+
let init = n.toString()
|
|
53
|
+
let portions = init.split(".")
|
|
54
|
+
portions[0] = portions[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")
|
|
55
|
+
return portions[0]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function regularFormat(num, precision) {
|
|
59
|
+
if (num === null || num === undefined) return "NaN"
|
|
60
|
+
let n = typeof num === 'number' ? num : (num.toNumber ? num.toNumber() : Number(num))
|
|
61
|
+
if (isNaN(n) || !isFinite(n)) return "NaN"
|
|
62
|
+
if (n < 0.001) return (0).toFixed(precision)
|
|
63
|
+
if (precision === 0) return commaFormat(Math.floor(n))
|
|
64
|
+
let fmt
|
|
65
|
+
if (Number.isInteger(n) && precision > 0) {
|
|
66
|
+
fmt = n + "." + "0".repeat(precision)
|
|
67
|
+
} else {
|
|
68
|
+
fmt = n.toFixed(precision)
|
|
69
|
+
}
|
|
70
|
+
// Add commas to integer part if useCommas is enabled
|
|
71
|
+
if (FORMAT_OPTIONS.useCommas && precision > 0) {
|
|
72
|
+
let parts = fmt.split(".")
|
|
73
|
+
parts[0] = parts[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")
|
|
74
|
+
fmt = parts.join(".")
|
|
75
|
+
}
|
|
76
|
+
return fmt
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ─── MetaNum-Specific Polarize ───────────────────────────────────
|
|
80
|
+
// r0 is [base, e, f, g, ...]: r0[0] is the argument, r0[1..] count the E/F/G… operations (levels 1, 2, 3, …).
|
|
81
|
+
// Binary form αΓβ: bottom = α (mantissa in [1,10)), top = β, height = Γ (1=E … 22=Z),
|
|
82
|
+
// repeat = count of repeated operations at the highest level (>1 means repeated letters).
|
|
83
|
+
function metaPolarize(r0, num) {
|
|
84
|
+
if (FORMAT_DEBUG >= 1) console.log("metaPolarize input:", JSON.stringify(r0))
|
|
85
|
+
if (r0.length === 0) r0 = [0]
|
|
86
|
+
|
|
87
|
+
// Find the highest non-zero index (level)
|
|
88
|
+
let highest = 0
|
|
89
|
+
for (let i = r0.length - 1; i >= 1; i--) {
|
|
90
|
+
if (r0[i] > 0) { highest = i; break }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (highest === 0) {
|
|
94
|
+
// Simple number: just r0[0], no higher levels
|
|
95
|
+
let bottom = r0[0]
|
|
96
|
+
let top = 0
|
|
97
|
+
let height = 0
|
|
98
|
+
if (bottom >= 10) {
|
|
99
|
+
let logVal = Math.log10(bottom)
|
|
100
|
+
bottom = Math.pow(10, logVal - Math.floor(logVal))
|
|
101
|
+
top = Math.floor(logVal)
|
|
102
|
+
height = 1
|
|
103
|
+
}
|
|
104
|
+
if (FORMAT_DEBUG >= 1) console.log("metaPolarize simple:", {bottom, top, height, repeat: 1})
|
|
105
|
+
return {bottom, top, height, repeat: 1}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// r0 = [v, e, f, g, ...], For E^n (highest=1): climb E to convert to F format;
|
|
109
|
+
// For F^n or higher (highest>=2): compute the argument at the E→F boundary
|
|
110
|
+
let value = r0[0]
|
|
111
|
+
let eCount = r0[1] || 0
|
|
112
|
+
|
|
113
|
+
// Climb the base value through E level
|
|
114
|
+
let eClimbExtra = 0
|
|
115
|
+
while (value >= 10) {
|
|
116
|
+
value = Math.log10(value)
|
|
117
|
+
eClimbExtra++
|
|
118
|
+
}
|
|
119
|
+
eCount += eClimbExtra
|
|
120
|
+
|
|
121
|
+
// Compute the F-level argument from the climbed E value
|
|
122
|
+
let fArg
|
|
123
|
+
if (value >= 1) {
|
|
124
|
+
fArg = eCount + Math.log10(value)
|
|
125
|
+
} else {
|
|
126
|
+
fArg = eCount + value - 1
|
|
127
|
+
if (fArg < 1) fArg = eCount + Math.log10(Math.pow(10, value))
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (highest === 1) {
|
|
131
|
+
// E level only: convert to F format
|
|
132
|
+
let bottom = value
|
|
133
|
+
let top = eCount
|
|
134
|
+
let height = 1
|
|
135
|
+
let repeat = eCount
|
|
136
|
+
if (top >= 2) {
|
|
137
|
+
height = 2
|
|
138
|
+
if (bottom < 1) {
|
|
139
|
+
bottom = Math.pow(10, bottom)
|
|
140
|
+
}
|
|
141
|
+
top = Math.floor(top)
|
|
142
|
+
repeat = 1
|
|
143
|
+
} else {
|
|
144
|
+
bottom = Math.pow(10, bottom - Math.floor(bottom))
|
|
145
|
+
top = Math.floor(top)
|
|
146
|
+
}
|
|
147
|
+
if (FORMAT_DEBUG >= 1) console.log("metaPolarize E level:", {bottom, top, height, repeat})
|
|
148
|
+
return {bottom, top, height, repeat}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// For mixed levels like FE20 or G10, the F argument is the value after E operations.
|
|
152
|
+
// Try to compute it directly (E^e(base) = 10^e * base for e=1, or tower for e>1).
|
|
153
|
+
let levelArg = fArg
|
|
154
|
+
let eOps = r0[1] || 0
|
|
155
|
+
let canCompute = false
|
|
156
|
+
if (eOps > 0) {
|
|
157
|
+
let eVal = r0[0]
|
|
158
|
+
canCompute = true
|
|
159
|
+
for (let i = 0; i < eOps; i++) {
|
|
160
|
+
eVal = Math.pow(10, eVal)
|
|
161
|
+
if (!isFinite(eVal) || eVal > 1e308) {
|
|
162
|
+
canCompute = false
|
|
163
|
+
break
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (canCompute) {
|
|
167
|
+
levelArg = eVal
|
|
168
|
+
}
|
|
169
|
+
// If not computable, levelArg stays as fArg (from E climbing)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
let height = highest
|
|
173
|
+
let repeat = r0[highest] || 0
|
|
174
|
+
|
|
175
|
+
// Case 1: Directly computable(e.g., FE20) -> Don't climb the computed value, use it directly as the top argument.
|
|
176
|
+
if (canCompute) {
|
|
177
|
+
let bottom = 1.0
|
|
178
|
+
let top = levelArg
|
|
179
|
+
if (FORMAT_DEBUG >= 1) console.log("metaPolarize computable:", {bottom, top, height, repeat})
|
|
180
|
+
return {bottom, top, height, repeat}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Case 2: Not computable ─> Check for the base=10^10 and all intermediate levels 8 (original argument was 10)
|
|
184
|
+
let all8 = (r0[0] === 10000000000)
|
|
185
|
+
for (let i = 2; i < highest && all8; i++) {
|
|
186
|
+
if ((r0[i] || 0) !== 8) all8 = false
|
|
187
|
+
}
|
|
188
|
+
if (all8) {
|
|
189
|
+
// The normalization collapses one level when the argument is 10
|
|
190
|
+
let bottom = 1.0
|
|
191
|
+
let top = 10
|
|
192
|
+
if (r0[highest] === 8) {
|
|
193
|
+
// Letter level not present (e.g., Z10 → Y^8(...E^8(10^10)...)): go up one level, repeat=1
|
|
194
|
+
height = highest + 1
|
|
195
|
+
repeat = 1
|
|
196
|
+
} else {
|
|
197
|
+
// Letter level present (e.g., ZZ(10) → Z(Y^8(...E^8(10^10)...))): add 1 for the base level
|
|
198
|
+
repeat = 1 + repeat
|
|
199
|
+
}
|
|
200
|
+
if (FORMAT_DEBUG >= 1) console.log("metaPolarize all-8s:", {bottom, top, height, repeat})
|
|
201
|
+
return {bottom, top, height, repeat}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Case 3: General non-computable ─> Climb through intermediate levels to compute the argument at the highest level.
|
|
205
|
+
if (eOps > 0) {
|
|
206
|
+
for (let lv = 2; lv < highest; lv++) {
|
|
207
|
+
let cnt = r0[lv] || 0
|
|
208
|
+
let climbExtra = 0
|
|
209
|
+
while (levelArg >= 10) {
|
|
210
|
+
levelArg = Math.log10(levelArg)
|
|
211
|
+
climbExtra++
|
|
212
|
+
}
|
|
213
|
+
cnt += climbExtra
|
|
214
|
+
levelArg = cnt + Math.log10(Math.max(levelArg, 1))
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// When the F argument was not computable and the highest level is F (level 2), the E→F conversion adds 1 to the effective F count.
|
|
219
|
+
if (highest === 2 && eOps > 0) {
|
|
220
|
+
repeat += 1
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Climb at the highest level to compute the total repeat count
|
|
224
|
+
let climbExtra = 0
|
|
225
|
+
let argVal = levelArg
|
|
226
|
+
while (argVal >= 10) {
|
|
227
|
+
argVal = Math.log10(argVal)
|
|
228
|
+
climbExtra++
|
|
229
|
+
}
|
|
230
|
+
let totalRepeat = repeat + climbExtra
|
|
231
|
+
|
|
232
|
+
// Compute bottom (mantissa) and top (argument) from the climbed argVal
|
|
233
|
+
let bottom
|
|
234
|
+
if (argVal < 1) {
|
|
235
|
+
bottom = Math.pow(10, argVal)
|
|
236
|
+
} else {
|
|
237
|
+
bottom = argVal
|
|
238
|
+
}
|
|
239
|
+
let top = Math.floor(levelArg)
|
|
240
|
+
|
|
241
|
+
if (FORMAT_DEBUG >= 1) console.log("metaPolarize high level:", {bottom, top, height, repeat: totalRepeat, levelArg, climbExtra})
|
|
242
|
+
return {bottom, top, height, repeat: totalRepeat}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// ─── Letter Name: Map height index to dlsdl's letter name ────────
|
|
246
|
+
// height 1 = E, 2 = F, ..., 22 = Z, 23 = Aa, 24 = Ab, ..., 48 = Az, 49 = Ba, ..., 698 = Zz, 699 = Aaa, ...
|
|
247
|
+
function letterName(height) {
|
|
248
|
+
if (height < 1) return "E"
|
|
249
|
+
if (height <= 22) {
|
|
250
|
+
// E = charCode 69, F = 70, ..., Z = 90
|
|
251
|
+
return String.fromCharCode(68 + height)
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Multi-letter: height >= 23 maps to the bijective base-26 sequence
|
|
255
|
+
let n = height - 23 // 0-based into multi-letter: 0=Aa, 1=Ab, ..., 25=Az, 26=Ba, ...
|
|
256
|
+
|
|
257
|
+
// Determine number of letters (starts at 2: Aa-Zz is 26*26 = 676 values)
|
|
258
|
+
let digits = 2
|
|
259
|
+
let rangeSize = 26 * 26
|
|
260
|
+
let offset = 0
|
|
261
|
+
while (n >= offset + rangeSize) {
|
|
262
|
+
offset += rangeSize
|
|
263
|
+
digits++
|
|
264
|
+
rangeSize *= 26
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
n -= offset
|
|
268
|
+
// n is now a base-26 number with 'digits' digits. First digit is uppercase (A-Z), rest are lowercase (a-z)
|
|
269
|
+
let result = ""
|
|
270
|
+
let divisor = Math.pow(26, digits - 1)
|
|
271
|
+
for (let i = 0; i < digits; i++) {
|
|
272
|
+
let digit = Math.floor(n / divisor)
|
|
273
|
+
n %= divisor
|
|
274
|
+
divisor /= 26
|
|
275
|
+
if (i === 0) {
|
|
276
|
+
result += String.fromCharCode(65 + digit) // A-Z
|
|
277
|
+
} else {
|
|
278
|
+
result += String.fromCharCode(97 + digit) // a-z
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return result
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// ─── Letter token with the multiLetterLimit symbol carry ──────────
|
|
285
|
+
// A k-letter combination (Aa, Aaa, …) sits at level ω^(k-1). Past multiLetterLimit switches to the symbol{index+1} form.
|
|
286
|
+
function letterTokenOf(height) {
|
|
287
|
+
let plain = letterName(height)
|
|
288
|
+
let limit = Math.max(2, FORMAT_OPTIONS.multiLetterLimit | 0)
|
|
289
|
+
if (plain.length <= limit) return { sym: "", letter: plain, arg: 0, mant: null }
|
|
290
|
+
// The carried form is "!" + the single-letter αΓβ: "!" adds one ω^ layer on top of Γ
|
|
291
|
+
let expo = plain.length - 1
|
|
292
|
+
if (expo >= 1 && expo <= 22) {
|
|
293
|
+
return { sym: "!", letter: letterName(expo), arg: expo, mant: 1, useBeta: true }
|
|
294
|
+
}
|
|
295
|
+
let mant = plain.charCodeAt(0) - 64 // coefficient of ω^(k-1)
|
|
296
|
+
for (let i = 1; i < plain.length; i++) {
|
|
297
|
+
mant += (plain.charCodeAt(i) - 97) * Math.pow(10, -i)
|
|
298
|
+
}
|
|
299
|
+
if (mant < 2) mant = 2 // diagonal [2,10) anchor
|
|
300
|
+
if (mant >= 10) mant = 9 + (mant - 10) / 170 // keep α ∈ [2,10)
|
|
301
|
+
return { sym: "!", letter: "Aa", arg: expo, mant: mant }
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// Emit α·letter·β for a token: the plain form is αΓβ, the carried form is the symbol-first !αAaβ whose α is the definition mantissa and β the ω-exponent.
|
|
305
|
+
function emitLetterToken(tok, alphaStr, betaStr, precision) {
|
|
306
|
+
if (!tok.sym) return alphaStr + tok.letter + betaStr
|
|
307
|
+
let a = (tok.mant === null || tok.mant === undefined) ? alphaStr
|
|
308
|
+
: regularFormat(tok.mant, precision)
|
|
309
|
+
// the single-letter carried form keeps the caller's argument (!1H51), the Aa form carries the ω-exponent instead
|
|
310
|
+
if (tok.useBeta) return tok.sym + a + tok.letter + betaStr
|
|
311
|
+
return tok.sym + a + tok.letter + formatR0Arg(tok.arg, precision)
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// ─── Symbol Name: Map layer to symbol ────────────────────────────
|
|
315
|
+
const SYMBOLS = "!@#$%&~<>?"
|
|
316
|
+
function symbolName(layer) {
|
|
317
|
+
if (layer <= 0) return ""
|
|
318
|
+
if (layer <= SYMBOLS.length) return SYMBOLS[layer - 1]
|
|
319
|
+
// For layers beyond SYMBOLS, formatLayer uses the ε format: αεβ
|
|
320
|
+
return ""
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// ─── Ordinal Letter: Extract letter name from ordinal rows ───────
|
|
324
|
+
// Format A (3+ elements): [count, v1..vN, diag] — diag 1-26 (A-Z, uppercase), v1..vN 0-25 (a-z, lowercase), count = repetition count.
|
|
325
|
+
// Format B (2 elements): [count, value] — count at ω-level, value the ordinal level number (901 means ω*901); letter is always "Aa".
|
|
326
|
+
// Snap CNF coefficients (little-endian [a1..ak], ordinal ω^(k-1)·ak+…+a1) to the ones the letter grid can name: 0 ≤ ai ≤ 25, top ak ≤ 26.
|
|
327
|
+
// Larger coefficients carry UP (ω*2+96→ω*3(Ca), ω*99→ω²(Aaa), ω²*100→ω³(Aaaa)), so an unnamed ordinal displays as the next nameable one above it.
|
|
328
|
+
function snapCoeffsToGrid(coeffs) {
|
|
329
|
+
let r = (coeffs || []).slice()
|
|
330
|
+
// little-endian ORDINAL carry: index 0 is the constant term. A term above its grid bound absorbs everything below it
|
|
331
|
+
// and bumps the next power so the snapped ordinal is the TIGHTEST nameable ordinal above the original one.
|
|
332
|
+
for (let i = 0; i < r.length; i++) {
|
|
333
|
+
let c = r[i]
|
|
334
|
+
if (typeof c !== 'number' || !isFinite(c) || c < 0) { r[i] = 0; continue }
|
|
335
|
+
c = Math.round(c) // integer coefficients only
|
|
336
|
+
let bound = (i === r.length - 1) ? 26 : 25 // top may reach 26 (A-Z)
|
|
337
|
+
if (c <= bound) { r[i] = c; continue }
|
|
338
|
+
for (let j = 0; j <= i; j++) r[j] = 0 // the carry absorbs the tail
|
|
339
|
+
if (i === r.length - 1) r.push(1) // ω^k·(bound+1) → ω^(k+1)
|
|
340
|
+
else r[i + 1] = (r[i + 1] || 0) + 1 // … → ω^(i+1)·(+1)
|
|
341
|
+
// keep scanning: the bumped slot may carry in turn
|
|
342
|
+
}
|
|
343
|
+
while (r.length > 1 && r[r.length - 1] === 0) r.pop()
|
|
344
|
+
if (r.length === 0) r = [1]
|
|
345
|
+
return r
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function getOrdinalLetter(ordRows) {
|
|
349
|
+
if (!ordRows || ordRows.length === 0) return null
|
|
350
|
+
|
|
351
|
+
// Check if all rows are 2-element [count, value] format (ω-level operations)
|
|
352
|
+
let allTwoElement = true
|
|
353
|
+
for (let i = 0; i < ordRows.length; i++) {
|
|
354
|
+
if (ordRows[i].length !== 2) { allTwoElement = false; break }
|
|
355
|
+
}
|
|
356
|
+
if (allTwoElement) {
|
|
357
|
+
return "Aa" // Base ω-level letter; value is handled in formatOrdinal
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
let tokens = []
|
|
361
|
+
// Process rows in REVERSE order (matching toString behavior)
|
|
362
|
+
for (let i = ordRows.length - 1; i >= 0; i--) {
|
|
363
|
+
let row = ordRows[i]
|
|
364
|
+
if (row.length < 2) continue
|
|
365
|
+
let diag = row[row.length - 1]
|
|
366
|
+
let vals = row.slice(1, row.length - 1)
|
|
367
|
+
let count = row[0] || 1
|
|
368
|
+
|
|
369
|
+
// Build letter name (reverse order: last val first, matching toString)
|
|
370
|
+
let letter = String.fromCharCode(64 + diag)
|
|
371
|
+
for (let j = vals.length - 1; j >= 0; j--) {
|
|
372
|
+
letter += String.fromCharCode(97 + vals[j])
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
for (let c = 0; c < count; c++) {
|
|
376
|
+
tokens.push(letter)
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// Do NOT sort - rows are already in canonical order from normalize
|
|
381
|
+
return tokens.join("")
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// ─── Reference Polarize (verbatim port of PowiainaNum's myPolarize) ──
|
|
385
|
+
// Recovers the display α ("bottom") and the diagonal level ("arrows") of an r0 chain STRUCTURALLY, climbing log10 level by level.
|
|
386
|
+
// The only method that stays exact at high levels where the engine's fractional-arg bisect quantizes (level ≥ 20).
|
|
387
|
+
function refPolarizeArraySort(array) {
|
|
388
|
+
array.sort(function (a, b) {
|
|
389
|
+
if (typeof a == 'number') return
|
|
390
|
+
else if (a[3] > b[3]) return -1
|
|
391
|
+
else if (a[3] < b[3]) return 1
|
|
392
|
+
else if (a[2] == 'x' && b[2] != 'x') return -1
|
|
393
|
+
else if (a[2] != 'x' && b[2] == 'x') return 1
|
|
394
|
+
else if (a[2] == 'x' && b[2] == 'x') return -1
|
|
395
|
+
else if (a[2] > b[2]) return -1
|
|
396
|
+
else if (a[2] < b[2]) return 1
|
|
397
|
+
else if (a[0] == 'x' && b[0] != 'x') return -1
|
|
398
|
+
else if (a[0] != 'x' && b[0] == 'x') return 1
|
|
399
|
+
else if (a[0] == 'x' && b[0] == 'x') return -1
|
|
400
|
+
else if (a[0] > b[0]) return -1
|
|
401
|
+
else if (a[0] < b[0]) return 1
|
|
402
|
+
else if (a[1] > b[1]) return -1
|
|
403
|
+
else if (a[1] < b[1]) return 1
|
|
404
|
+
return -1
|
|
405
|
+
})
|
|
406
|
+
}
|
|
407
|
+
function refPolarizeArrayMerge(array) {
|
|
408
|
+
let elemOffset = 0
|
|
409
|
+
for (let i = 0; i < array.length - 2; ++i) {
|
|
410
|
+
if (array[i][0] == array[i + 1][0] &&
|
|
411
|
+
array[i][2] == array[i + 1][2] &&
|
|
412
|
+
array[i][3] == array[i + 1][3]) {
|
|
413
|
+
array[i][1] += array[i + 1][1]
|
|
414
|
+
array.splice(i + 1, 1)
|
|
415
|
+
--i
|
|
416
|
+
elemOffset++
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return elemOffset
|
|
420
|
+
}
|
|
421
|
+
// Our r0 + [count, level] finite rows → PowiainaNum's [base, [level, count, 1, 1]...]
|
|
422
|
+
// (their row level = our level verbatim: their 3{21}3 rows top at [20,1] and their polarize returns arrows 20, displayed "…20" — no off-by-one)
|
|
423
|
+
function refPolarizeInput(r0, finiteRows) {
|
|
424
|
+
let arr = [r0[0]]
|
|
425
|
+
for (let i = 1; i < r0.length; i++) {
|
|
426
|
+
if (r0[i] > 0) arr.push([i, r0[i], 1, 1])
|
|
427
|
+
}
|
|
428
|
+
if (finiteRows) {
|
|
429
|
+
for (let i = 0; i < finiteRows.length; i++) {
|
|
430
|
+
let fr = finiteRows[i]
|
|
431
|
+
if (fr && fr.length === 2 && fr[0] > 0) arr.push([fr[1], fr[0], 1, 1])
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
return arr
|
|
435
|
+
}
|
|
436
|
+
function refPolarize(array, hasOperationRepeat = true) {
|
|
437
|
+
refPolarizeArraySort(array)
|
|
438
|
+
refPolarizeArrayMerge(array)
|
|
439
|
+
let ptr = array.length - 2
|
|
440
|
+
let b = () => array[array.length - 1]
|
|
441
|
+
let c = (x) => { array[array.length - 1] = x }
|
|
442
|
+
let repeatResult = 1000001
|
|
443
|
+
while (--repeatResult >= 0) {
|
|
444
|
+
if (b() >= 10) {
|
|
445
|
+
let a = Math.log10(b())
|
|
446
|
+
array.push([1, 1, 1, 1])
|
|
447
|
+
ptr++
|
|
448
|
+
refPolarizeArraySort(array)
|
|
449
|
+
let offset = refPolarizeArrayMerge(array)
|
|
450
|
+
ptr -= offset
|
|
451
|
+
c(a)
|
|
452
|
+
} else {
|
|
453
|
+
if (ptr == 0 && typeof array[ptr + 1] == 'number' &&
|
|
454
|
+
(hasOperationRepeat || (!hasOperationRepeat && array[ptr][1] == 1))) break
|
|
455
|
+
if (ptr != 0 && typeof array[ptr + 1] == 'number' && typeof array[ptr][0] == 'number' &&
|
|
456
|
+
array[ptr][1] == 1 &&
|
|
457
|
+
(array[ptr - 1][0] == 'x' || array[ptr - 1][2] > array[ptr][2] || array[ptr - 1][3] > array[ptr][3]) &&
|
|
458
|
+
array[ptr][0] > 2 && (ptr != 0 || array[ptr - 1][0] == 'x')) {
|
|
459
|
+
let arrow_count = array[ptr][0]
|
|
460
|
+
let base = b()
|
|
461
|
+
let Jx
|
|
462
|
+
if (arrow_count == 3) Jx = base
|
|
463
|
+
else Jx = arrow_count - 1 + Math.log(base / 2) / Math.log(5)
|
|
464
|
+
array[ptr][0] = 'x'
|
|
465
|
+
c(Jx)
|
|
466
|
+
refPolarizeArraySort(array)
|
|
467
|
+
let offset = refPolarizeArrayMerge(array)
|
|
468
|
+
ptr -= offset
|
|
469
|
+
}
|
|
470
|
+
if (typeof array[ptr + 1] == 'number' && array[ptr][0] == 'x' && b() < 10 && ptr != 0) {
|
|
471
|
+
let base = b()
|
|
472
|
+
let JRepeation = array[ptr][1]
|
|
473
|
+
let Kx = JRepeation + Math.log10(base)
|
|
474
|
+
array[ptr][1] = 1
|
|
475
|
+
array[ptr][0] = 1
|
|
476
|
+
array[ptr][2]++
|
|
477
|
+
c(Kx)
|
|
478
|
+
refPolarizeArraySort(array)
|
|
479
|
+
let offset = refPolarizeArrayMerge(array)
|
|
480
|
+
ptr -= offset
|
|
481
|
+
}
|
|
482
|
+
if (((ptr == 0 && !hasOperationRepeat && array[ptr][1] != 1) || ptr != 0) &&
|
|
483
|
+
array[ptr][0] != 'x' && b() < 10) {
|
|
484
|
+
if (b() == 1 && ptr > 0 && array[ptr - 1][0] > array[ptr][0] && array[ptr][1] == 1) {
|
|
485
|
+
array[ptr][0] = array[ptr - 1][0]
|
|
486
|
+
} else {
|
|
487
|
+
let right = array[ptr][1] + Math.log10(b())
|
|
488
|
+
array[ptr][0]++
|
|
489
|
+
array[ptr][1] = 1
|
|
490
|
+
c(right)
|
|
491
|
+
}
|
|
492
|
+
refPolarizeArraySort(array)
|
|
493
|
+
let offset = refPolarizeArrayMerge(array)
|
|
494
|
+
ptr -= offset
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
return {
|
|
499
|
+
bottom: b(),
|
|
500
|
+
repeation: array[ptr][1],
|
|
501
|
+
arrows: array[ptr][0],
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
// Convert a finite-chain polarize triple (bottom, repeation, arrows) into the diagonal-letter display pair (mantissa, β).
|
|
505
|
+
// The value behaves as 10{arrows+1}(t), t = log10(bottom)+repeation; the diagonal convention is α = 2·5^f ∈ [2,10):
|
|
506
|
+
// t ∈ [1,2) -> mantissa 2·5^(t-1), β = arrows-1
|
|
507
|
+
// t ∈ [2,10) -> mantissa t, β = arrows (3{100}3 -> 2.376Aa99)
|
|
508
|
+
// t ≥ 10 -> one level per smooth-log step f → 1+log10(f); the anchor t=10 is mantissa 2 at β=arrows+1 (10{100}10 -> 2.000Aa100)
|
|
509
|
+
// Truncation marker: a truncated cascade resets r0 to [10] and bumps the lowest kept row's count by 1 over the uniform fill (xCnt) — read it as xCnt so truncated displays match the untruncated anchor.
|
|
510
|
+
function normalizedFiniteRows(r0, finiteRows) {
|
|
511
|
+
if (!finiteRows || finiteRows.length === 0 || r0[0] !== 10) return finiteRows
|
|
512
|
+
let sorted = finiteRows.slice().sort((a, b) => a[1] - b[1])
|
|
513
|
+
let c0 = sorted[0][0] || 0
|
|
514
|
+
let c1 = sorted.length > 1 ? (sorted[1][0] || 0) : 0
|
|
515
|
+
if (c0 === c1 + 1) {
|
|
516
|
+
let copy = finiteRows.slice()
|
|
517
|
+
for (let i = 0; i < copy.length; i++) {
|
|
518
|
+
if (copy[i][1] === sorted[0][1]) copy[i] = [c1, copy[i][1]]
|
|
519
|
+
}
|
|
520
|
+
return copy
|
|
521
|
+
}
|
|
522
|
+
return finiteRows
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function aaDiagonalPair(pol) {
|
|
526
|
+
let t = Math.log10(pol.bottom) + pol.repeation
|
|
527
|
+
let beta = pol.arrows
|
|
528
|
+
// discrete integer argument (10{arrows+1}(t) with integer t): climb the raw count C=t-1 at level arrows;
|
|
529
|
+
// C ≤ 9 is the anchor band (one smooth application 10{arrows+1}2 … through arg 9) -> mantissa 2 at β=arrows+1
|
|
530
|
+
let discrete = Math.abs(pol.bottom - 1) < 1e-12 && pol.repeation === Math.floor(pol.repeation)
|
|
531
|
+
if (discrete && t - 1 <= 9 + 1e-9 && t >= 2) {
|
|
532
|
+
return { mant: 2, beta: pol.arrows + 1 }
|
|
533
|
+
}
|
|
534
|
+
if (t >= 9.999999999) {
|
|
535
|
+
// v = 10{arrows+1}(t) = {arrows}^{t-1}(10): the raw level-(arrows) application count is (t-1);
|
|
536
|
+
// run the geometric smooth-log climb from a scalar count all the way to the diagonal band at β = arrows+1.
|
|
537
|
+
let n = pol.arrows
|
|
538
|
+
let s = Math.log10(Math.max(t - 1, 1))
|
|
539
|
+
for (let level = 1; level < n + 2; level++) {
|
|
540
|
+
s = (s >= 9.999999999) ? 2 : 1 + Math.log10(s)
|
|
541
|
+
}
|
|
542
|
+
let mant = s < 2 ? 1 + s : s
|
|
543
|
+
return { mant: Math.min(mant, 10), beta: n + 1 }
|
|
544
|
+
}
|
|
545
|
+
if (t < 2) return { mant: 2 * Math.pow(5, t - 1), beta: beta - 1 }
|
|
546
|
+
// t in [2,10): genuinely smooth (fractional compact row, or a non-10 base fixed point such as 3{100}3 -> 2.376) is shown directly;
|
|
547
|
+
// the discrete integer-argument anchor case was handled at the top of the function.
|
|
548
|
+
return { mant: t, beta: beta }
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// Structural α/diagonal recovery for a value whose finite-level chain (r0 cols + [count, level] rows) reaches the Aa diagonal region.
|
|
552
|
+
// Mirrors the reference convention: e.g. 3{100}3 → bottom 2.375812, arrows 99 ("2.376Aa99").
|
|
553
|
+
function polarizeFiniteChain(r0, finiteRows) {
|
|
554
|
+
try {
|
|
555
|
+
finiteRows = normalizedFiniteRows(r0, finiteRows)
|
|
556
|
+
let arr = refPolarizeInput(r0.slice(0), finiteRows)
|
|
557
|
+
if (arr.length === 1) return null
|
|
558
|
+
return refPolarize(arr, true)
|
|
559
|
+
} catch (e) {
|
|
560
|
+
return null
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// ─── Main Format Function ────────────────────────────────────────
|
|
565
|
+
|
|
566
|
+
// Parse an already-formatted r0 argument back to a number (e.g. "1,000" → 1000, "1.000E1,000" → null: carries its own E structure)
|
|
567
|
+
function formatR0ArgStr(argStr) {
|
|
568
|
+
var s = String(argStr).replace(/,/g, "")
|
|
569
|
+
if (/^\d+(\.\d+)?$/.test(s)) return Number(s)
|
|
570
|
+
return null
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
// De-layered !Aa x instance: the shape [[x],[1,0×k,1]] with k = max(1, floor(x)), x >= 1.
|
|
574
|
+
// Per the README the display is the APPROXIMATE binary form !α'Aa'β' (β' = floor(x), α' = 2·5^(x−β') ∈ [2,10))
|
|
575
|
+
// the mantissa carries only FORMAT_OPTIONS digits, so the round-trip recovers x to that precision。
|
|
576
|
+
// An INTEGER x has α' = 2 exactly, and the binary spelling now denotes the ω^n diagonal cascade.
|
|
577
|
+
function formatBangAaValue(num, precision) {
|
|
578
|
+
if (num.sign !== 1 || num.layer !== 0) return null
|
|
579
|
+
let a = num.array
|
|
580
|
+
if (a.length !== 2 || a[0].length !== 1) return null
|
|
581
|
+
let x = a[0][0]
|
|
582
|
+
if (typeof x !== 'number' || !isFinite(x) || x < 2) return null
|
|
583
|
+
let row = a[1]
|
|
584
|
+
if (row.length < 3 || row[0] !== 1 || row[row.length - 1] !== 1) return null
|
|
585
|
+
for (let i = 1; i < row.length - 1; i++) if (row[i] !== 0) return null
|
|
586
|
+
let k = row.length - 2
|
|
587
|
+
if (k !== Math.max(1, Math.floor(x))) return null
|
|
588
|
+
if (x === Math.floor(x)) {
|
|
589
|
+
// integer anchor: the letter token A(a×k)x is the exact spelling
|
|
590
|
+
return "A" + "a".repeat(k) + formatR0Arg(x, precision)
|
|
591
|
+
}
|
|
592
|
+
let beta = Math.floor(x)
|
|
593
|
+
let mant = 2 * Math.pow(5, x - beta)
|
|
594
|
+
return "!" + regularFormat(mant, precision) + "Aa" + formatR0Arg(beta, precision)
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// The full rule-3 cascade of 10{ω^n}10 (n ≥ 2) — the stored form of !Aa<n> A cheap shape pre-filter (base [10], full row budget, top row [8, 9×n])
|
|
598
|
+
// plus an exact comparison against the engine's own cascade, so nothing else matches. Displayed as the binary diagonal !2.000Aa<n>, which parses back to the same cascade.
|
|
599
|
+
function formatAaCascade(num, precision) {
|
|
600
|
+
if (num.sign !== 1 || num.layer !== 0) return null
|
|
601
|
+
let a = num.array
|
|
602
|
+
if (a.length !== MetaNum.maxRows || a.length < 3) return null
|
|
603
|
+
if (a[0].length !== 1 || a[0][0] !== 10) return null
|
|
604
|
+
let last = a[a.length - 1]
|
|
605
|
+
let n = last.length - 1
|
|
606
|
+
if (n < 2 || last[0] !== 8) return null
|
|
607
|
+
for (let i = 1; i < last.length; i++) if (last[i] !== 9) return null
|
|
608
|
+
let coeffs = []
|
|
609
|
+
for (let i = 0; i < n; i++) coeffs.push(0)
|
|
610
|
+
coeffs.push(1)
|
|
611
|
+
let ref = MetaNum._hyperopFromOrdinalRaw(MetaNum.TEN, MetaNum.TEN, coeffs)
|
|
612
|
+
if (!ref.eq(num)) return null
|
|
613
|
+
return "!" + regularFormat(2, precision) + "Aa" + formatR0Arg(n, precision)
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
function formatCore(num, precision=3, small=false) {
|
|
617
|
+
if (MetaNum.isNaN(num)) return "NaN"
|
|
618
|
+
let sciSigDigits = FORMAT_OPTIONS.sciSignificantDigits // for E notation
|
|
619
|
+
let sciPrecision = Math.max(sciSigDigits, precision)
|
|
620
|
+
let singlePrecision = FORMAT_OPTIONS.singleLetterDigits // for F, G, H, ...
|
|
621
|
+
let multiPrecision = FORMAT_OPTIONS.multiLetterDigits // for Aa and beyond
|
|
622
|
+
num = new MetaNum(num)
|
|
623
|
+
let array = num.array
|
|
624
|
+
let r0 = array[0]
|
|
625
|
+
|
|
626
|
+
// Basic edge cases
|
|
627
|
+
if (num.sign !== 2 && num.sign !== -2 && num.abs().lt(1e-308)) return (0).toFixed(precision)
|
|
628
|
+
if (num.sign < 0) return "-" + format(num.neg(), precision, small)
|
|
629
|
+
if (num.isInfinite()) return "Infinity"
|
|
630
|
+
|
|
631
|
+
// Sub-unit values (sign 2 / -2, 0 < |v| < 1): a plain finite r0 stores the RECIPROCAL, so the real value is 1/r0.
|
|
632
|
+
if ((num.sign === 2 || num.sign === -2) && num.layer === 0 &&
|
|
633
|
+
num.array.length === 1 && num.array[0].length === 1 &&
|
|
634
|
+
isFinite(num.array[0][0]) && num.array[0][0] !== 0) {
|
|
635
|
+
let realVal = 1 / num.array[0][0]
|
|
636
|
+
if (isFinite(realVal) && realVal > 0) {
|
|
637
|
+
let asNormal = num.clone()
|
|
638
|
+
asNormal.sign = 1
|
|
639
|
+
asNormal.array = [[realVal]]
|
|
640
|
+
num = asNormal
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// Small value handling (e.g., 0.0000000001 → 1.000E-10 or 1.000E10⁻¹)
|
|
645
|
+
let smallThreshold = Math.pow(10, -FORMAT_OPTIONS.smallNotationThreshold)
|
|
646
|
+
if (num.lt(smallThreshold)) {
|
|
647
|
+
if (FORMAT_OPTIONS.smallNotationUseE) {
|
|
648
|
+
// Handle reciprocal values (sign=2/-2) that underflow to 0 in double precision
|
|
649
|
+
if (num.sign === 2 || num.sign === -2) {
|
|
650
|
+
var recipFmt = num.clone()
|
|
651
|
+
recipFmt.sign = recipFmt.sign === 2 ? 1 : -1
|
|
652
|
+
var prefix = num.sign === -2 ? "-" : ""
|
|
653
|
+
// Compute magnitude (log10 of reciprocal)
|
|
654
|
+
var mag = recipFmt.log10()
|
|
655
|
+
// Tier 1: mag is a simple number → αE-β format
|
|
656
|
+
if (mag.array.length === 1 && mag.array[0].length <= 2) {
|
|
657
|
+
var magNum = mag.toNumber()
|
|
658
|
+
if (isFinite(magNum) && magNum > 0) {
|
|
659
|
+
var frac = magNum - Math.floor(magNum)
|
|
660
|
+
var mant = Math.pow(10, frac)
|
|
661
|
+
var expPart = Math.floor(magNum)
|
|
662
|
+
if (mant < 1) { mant *= 10; expPart -= 1 }
|
|
663
|
+
if (mant >= 9.999999999999999) { mant = 1; expPart += 1 }
|
|
664
|
+
return prefix + regularFormat(mant, sciMantissaPrecision(expPart)) + "E-" + commaFormat(expPart)
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
// Tier 2: mag is large → "E-" + the letter-law form of mag, log10 shifts only the OUTERMOST letter.
|
|
668
|
+
var recipStr = format(recipFmt, precision, small)
|
|
669
|
+
var magStr = recipStr
|
|
670
|
+
// leading run of E letters (e.g. "EE1.000E1,000"): strip one E
|
|
671
|
+
var eRun = recipStr.match(/^(E+)(.*)$/)
|
|
672
|
+
var rm = recipStr.match(/^(\d(?:\.\d+)?)([A-Z])((?:\d[\d,]*)?)$/)
|
|
673
|
+
if (eRun && !rm) {
|
|
674
|
+
var restStr = eRun[2]
|
|
675
|
+
if (eRun[1].length >= 2) {
|
|
676
|
+
// "EE…" → "E…" (log10 of an E-chain drops one E)
|
|
677
|
+
magStr = eRun[1].slice(0, -1) + restStr
|
|
678
|
+
} else if (eRun[1].length === 1) {
|
|
679
|
+
// single "EαEβ" / "Eβ": log10 is the β part
|
|
680
|
+
var betaPart = restStr.replace(/^\d(?:\.\d+)?(?=[A-Z])/, "")
|
|
681
|
+
if (/^[A-Z]/.test(betaPart) || /^[\d,]/.test(betaPart)) magStr = betaPart
|
|
682
|
+
}
|
|
683
|
+
} else if (rm) {
|
|
684
|
+
var rAlpha = rm[1], rLetter = rm[2], rArg = rm[3]
|
|
685
|
+
if (rLetter === "E" && rArg !== "") {
|
|
686
|
+
// E-chain with numeric arg (E200): log10 keeps E-arg
|
|
687
|
+
magStr = rAlpha + "E" + rArg
|
|
688
|
+
} else if (rLetter === "F" && rArg !== "") {
|
|
689
|
+
// F-chain: arg −1
|
|
690
|
+
magStr = rAlpha + "F" + commaFormat(Math.max(0, Number(rArg.replace(/,/g, "")) - 1))
|
|
691
|
+
}
|
|
692
|
+
// G and above: display unchanged
|
|
693
|
+
}
|
|
694
|
+
return prefix + "E-" + magStr
|
|
695
|
+
}
|
|
696
|
+
// Format as aE-b (e.g., 0.0000000001 → 1.000E-10)
|
|
697
|
+
let nVal = num.toNumber()
|
|
698
|
+
if (nVal > 0 && isFinite(nVal)) {
|
|
699
|
+
let logVal = Math.log10(nVal)
|
|
700
|
+
let m = Math.pow(10, logVal - Math.floor(logVal))
|
|
701
|
+
let e = Math.abs(Math.floor(logVal))
|
|
702
|
+
// Normalize: ensure m in [1, 10)
|
|
703
|
+
if (m < 1) { m *= 10; e -= 1 }
|
|
704
|
+
if (m >= 9.999999999999999) { m = 1; e += 1 }
|
|
705
|
+
return regularFormat(m, sciMantissaPrecision(e)) + "E-" + commaFormat(e)
|
|
706
|
+
}
|
|
707
|
+
// Fallback for non-simple values: use ⁻¹ notation
|
|
708
|
+
if (num.layer === 0 && num.array.length === 1 && num.array[0].length === 1) {
|
|
709
|
+
var val = num.array[0][0]
|
|
710
|
+
if (val > 0) {
|
|
711
|
+
var recipVal = 1 / val
|
|
712
|
+
if (isFinite(recipVal)) {
|
|
713
|
+
return format(new MetaNum(recipVal), precision, small) + "⁻¹"
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
return format(num.rec(), precision, small) + "⁻¹"
|
|
718
|
+
} else {
|
|
719
|
+
// Traditional ⁻¹ notation
|
|
720
|
+
if (num.sign === 2 || num.sign === -2) {
|
|
721
|
+
// Already in small representation (sign=2), toggle sign to get reciprocal
|
|
722
|
+
var recipSmall = num.clone()
|
|
723
|
+
recipSmall.sign = recipSmall.sign === 2 ? 1 : -1
|
|
724
|
+
return format(recipSmall, precision, small) + "⁻¹"
|
|
725
|
+
}
|
|
726
|
+
// Normal representation but value < 1 (e.g., 0.0000000001 stored as [[1e-10]]), Compute reciprocal properly: 1/val
|
|
727
|
+
if (num.layer === 0 && num.array.length === 1 && num.array[0].length === 1) {
|
|
728
|
+
var val = num.array[0][0]
|
|
729
|
+
if (val > 0) {
|
|
730
|
+
var recipVal = 1 / val
|
|
731
|
+
if (isFinite(recipVal)) {
|
|
732
|
+
return format(new MetaNum(recipVal), precision, small) + "⁻¹"
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
// Fallback
|
|
737
|
+
return format(num.rec(), precision, small) + "⁻¹"
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
if (num.lt(1)) return regularFormat(num, FORMAT_OPTIONS.decimalPlaces + (small ? 2 : 0))
|
|
742
|
+
if (num.lt(1000)) return regularFormat(num, FORMAT_OPTIONS.decimalPlaces)
|
|
743
|
+
|
|
744
|
+
// Check decimal threshold: if value >= 10^decimalThreshold, don't show decimals
|
|
745
|
+
let useDecimals = true
|
|
746
|
+
if (FORMAT_OPTIONS.decimalThreshold > 0) {
|
|
747
|
+
let thresholdVal = Math.pow(10, FORMAT_OPTIONS.decimalThreshold)
|
|
748
|
+
if (num.gte(thresholdVal)) useDecimals = false
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
if (num.lt(Math.pow(10, FORMAT_OPTIONS.sciThreshold))) {
|
|
752
|
+
if (useDecimals) return regularFormat(num, FORMAT_OPTIONS.decimalPlaces)
|
|
753
|
+
else return commaFormat(num)
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
// ── Handle layer > 0 (symbol notation: !, @, #, ..., ε) ──
|
|
757
|
+
if (num.layer > 0) {
|
|
758
|
+
return formatLayer(num, precision, sciPrecision, singlePrecision, multiPrecision)
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
// ── Handle ordinal rows (Aa, Ab, ..., Aaa, ... range) ──
|
|
762
|
+
let hasOrdinalRows = array.length > 1
|
|
763
|
+
if (hasOrdinalRows) {
|
|
764
|
+
let bang = formatBangAaValue(num, multiPrecision)
|
|
765
|
+
if (bang !== null) return bang
|
|
766
|
+
// The full 10{ω^n}10 cascade (stored !Aa<n>) — display the binary diagonal so the parser round-trips it
|
|
767
|
+
let cascade = formatAaCascade(num, multiPrecision)
|
|
768
|
+
if (cascade !== null) return cascade
|
|
769
|
+
return formatOrdinal(num, precision, multiPrecision)
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
// ── r0-only: scientific notation, E-Z range ──
|
|
773
|
+
let maxLevel = 0
|
|
774
|
+
for (let i = 1; i < r0.length; i++) {
|
|
775
|
+
if (r0[i] > 0) maxLevel = i
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
// ── Handle r0-only with maxLevel >= 23: Aa-diagonal──
|
|
779
|
+
// The cascade collapses to αAaβ with (bottom, repeation, arrows) from polarizeFiniteChain (the structural climb), mantissa normalized into [1,10) carrying decades into β.
|
|
780
|
+
if (maxLevel >= 23) {
|
|
781
|
+
let pol = polarizeFiniteChain(r0.slice(0), null)
|
|
782
|
+
if (pol && isFinite(pol.bottom) && pol.bottom > 0 && isFinite(pol.arrows)) {
|
|
783
|
+
let pair = aaDiagonalPair(pol)
|
|
784
|
+
return regularFormat(pair.mant, multiPrecision) + "Aa" + commaFormat(pair.beta)
|
|
785
|
+
}
|
|
786
|
+
// fallback: legacy base-9 coefficient form
|
|
787
|
+
let coeff = r0[maxLevel]
|
|
788
|
+
let pow9 = 9
|
|
789
|
+
for (let i = maxLevel - 1; i >= 1; i--) {
|
|
790
|
+
coeff += (r0[i] || 0) / pow9
|
|
791
|
+
pow9 *= 9
|
|
792
|
+
}
|
|
793
|
+
let level = maxLevel
|
|
794
|
+
while (coeff >= 9) {
|
|
795
|
+
coeff /= 9
|
|
796
|
+
level++
|
|
797
|
+
}
|
|
798
|
+
// Format coefficient: if close to integer, show as integer
|
|
799
|
+
let coeffVal
|
|
800
|
+
if (Math.abs(coeff - Math.round(coeff)) < 1e-12 && Math.round(coeff) <= Number.MAX_SAFE_INTEGER) {
|
|
801
|
+
coeffVal = Math.round(coeff)
|
|
802
|
+
} else {
|
|
803
|
+
coeffVal = coeff
|
|
804
|
+
}
|
|
805
|
+
return regularFormat(coeffVal, multiPrecision) + "Aa" + commaFormat(level)
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
// ── All other r0-only cases: use the recursive chain builder ──
|
|
809
|
+
return formatR0AsChain(r0.slice(0), sciPrecision)
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
// ─── Format with Layer (symbol notation) ─────────────────────────
|
|
813
|
+
|
|
814
|
+
function formatLayer(num, precision, precision2, precision3, precision4) {
|
|
815
|
+
let sym = symbolName(num.layer)
|
|
816
|
+
|
|
817
|
+
// Create a layer-down version for the inner format
|
|
818
|
+
let inner = num.clone()
|
|
819
|
+
inner.layer = 0
|
|
820
|
+
inner.normalize()
|
|
821
|
+
|
|
822
|
+
// Check if inner de-layered to a "!Aa-like" form (all a's in ordinal)
|
|
823
|
+
// If the ordinal row is [count, 0, 0, ..., 0, diag] and r0[0] equals the number of zeros, we can format compactly as !Aa[r0[0]] (per README: !Aaα = (10^frac(α),0,...,0)|10 with int(α) zeros)
|
|
824
|
+
let canCompact = false
|
|
825
|
+
let compactBase = 0
|
|
826
|
+
if (inner.array.length > 1) {
|
|
827
|
+
let lastRow = inner.array[inner.array.length - 1]
|
|
828
|
+
let diag = lastRow[lastRow.length - 1]
|
|
829
|
+
let vals = lastRow.slice(1, lastRow.length - 1)
|
|
830
|
+
let allZero = vals.every(v => v === 0)
|
|
831
|
+
let base = inner.array[0][0]
|
|
832
|
+
// Compact form only applies when diag=1 (A), all vals are 0, and r0[0] equals the number of zeros
|
|
833
|
+
if (allZero && inner.array[0].length === 1 && diag === 1 && base === vals.length) {
|
|
834
|
+
canCompact = true
|
|
835
|
+
compactBase = base
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
// For layers beyond the explicit symbols, use the ε format: αεβ, ε represents exponent tower layers of ω: αεβ ~ f_ω^ω^...^ω(β ω's)_(α)
|
|
840
|
+
// This check must come before the compact !Aa form, otherwise ε layers get mis-rendered as "Aa..." (e.g. 1ε500 → "1.000Aa10")
|
|
841
|
+
if (num.layer > SYMBOLS.length) {
|
|
842
|
+
let epsPrecision = FORMAT_OPTIONS.epsilonSignificantDigits | 0
|
|
843
|
+
if (canCompact && compactBase >= 2 && Math.floor(compactBase) === compactBase) {
|
|
844
|
+
// Compact Aa form at ε layers folds into one more ε level: layer n + Aa-form ≡ ε(n+1) with mantissa 10^frac(compactBase)
|
|
845
|
+
let bottom = Math.pow(10, compactBase - Math.floor(compactBase))
|
|
846
|
+
return regularFormat(bottom, epsPrecision) + "ε" + commaFormat(num.layer + 1)
|
|
847
|
+
}
|
|
848
|
+
let innerStr = format(inner, epsPrecision, false)
|
|
849
|
+
return innerStr + "ε" + commaFormat(num.layer)
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
// ω^ω-family tail-CNF markers (a single Aa row [1,0,1] at a symbol layer):
|
|
853
|
+
// r0[0] is the ω^0 coefficient (the integer-form argument) and r0[j] (j≥1,
|
|
854
|
+
// non-negative integers) are the E/F/G/H-form digits — E↔ω^ω+ω^1, F↔ω^2,
|
|
855
|
+
// G↔ω^3, … — matching the parser's finOps convention (!AaE16 → r0=[16,1],
|
|
856
|
+
// layer 1). Emitted as "!2.000Aa" + E×c1 + F×c2 + … + c0 so the string
|
|
857
|
+
// round-trips exactly through the symPrefix/finOps parse path.
|
|
858
|
+
if (num.layer >= 1 && num.array.length === 2 &&
|
|
859
|
+
num.array[1].length === 3 &&
|
|
860
|
+
num.array[1][0] === 1 && num.array[1][1] === 0 && num.array[1][2] === 1 &&
|
|
861
|
+
num.array[0].length >= 2) {
|
|
862
|
+
let finR0 = num.array[0]
|
|
863
|
+
let finLetters = ""
|
|
864
|
+
let finOk = true
|
|
865
|
+
for (let fj = 1; fj < finR0.length; fj++) {
|
|
866
|
+
let fc = finR0[fj]
|
|
867
|
+
if (typeof fc !== "number" || fc !== Math.floor(fc) || fc < 0 || fc > 26 || fj > 22) { finOk = false; break }
|
|
868
|
+
if (fc > 0) finLetters += letterName(fj).repeat(fc)
|
|
869
|
+
}
|
|
870
|
+
let finC0 = finR0[0]
|
|
871
|
+
// c0 must be an integer: a non-integer c0 with higher digits is a
|
|
872
|
+
// height tower chain (e.g. iterate(3,1.5e16) → r0=[16.176,1]), not a
|
|
873
|
+
// tail-CNF marker — leave those to the legacy chain format.
|
|
874
|
+
if (finOk && typeof finC0 === "number" && isFinite(finC0) && finC0 >= 0 &&
|
|
875
|
+
finC0 === Math.floor(finC0) && finC0 < 1e15) {
|
|
876
|
+
let c0Str = (finC0 === Math.floor(finC0)) ? String(finC0) : String(Math.round(finC0 * 1e12) / 1e12)
|
|
877
|
+
if (!/e/i.test(c0Str)) {
|
|
878
|
+
return sym + regularFormat(2, precision4) + "Aa" + finLetters + c0Str
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
// The 10{ω^n}10 cascade at a symbol layer — the stored form of @Aa<n> (layer 1), #Aa<n> (layer 2), index is the total ω^-layer count.
|
|
884
|
+
let cascadeStr = formatAaCascade(inner, precision4)
|
|
885
|
+
if (cascadeStr !== null) {
|
|
886
|
+
let body = cascadeStr.slice(1) // strip the layer-0 "!"
|
|
887
|
+
if (num.layer < SYMBOLS.length) return SYMBOLS[num.layer] + body
|
|
888
|
+
return body + "ε" + commaFormat(num.layer + 1)
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
// Compact diagonal: the symbol row [1,0…0,1] with m=compactBase zeros is 10{ω^m}10 — a DIAGONAL letter (A + m a's) at argument 10, with the pure dlsdl α=2·5^f ∈ [2,10) convention.
|
|
892
|
+
// For layers above ! the symbol of the layer below prefixes the same inner form (!Aa3 = 2.000Aaaa10, @Aa3 = !2.000Aaaa10).
|
|
893
|
+
if (canCompact && compactBase >= 2) {
|
|
894
|
+
let mInt = Math.floor(compactBase)
|
|
895
|
+
let frac = compactBase - mInt
|
|
896
|
+
let mant = 2 * Math.pow(5, frac)
|
|
897
|
+
// Past multiLetterLimit the old single-letter carry (!1.000N10) did not round-trip — it added a layer and could not name the exact diagonal.
|
|
898
|
+
// Keep the exact A(a×m)10 token while the string stays reasonable; only fall back to the carry past a hard safety cap.
|
|
899
|
+
if (mInt <= MULTI_TOKEN_CAP) {
|
|
900
|
+
return symbolName(num.layer) + regularFormat(mant, precision4) +
|
|
901
|
+
"A" + "a".repeat(mInt) + "10"
|
|
902
|
+
}
|
|
903
|
+
// Hard-cap fallback: keep the argument (10 for a compact diagonal) but the level is the historical single-letter carry approximation.
|
|
904
|
+
let total = num.layer + 1
|
|
905
|
+
let body = regularFormat(1, precision4) + letterName(mInt) +
|
|
906
|
+
formatR0Arg(10, precision4)
|
|
907
|
+
if (total <= SYMBOLS.length) return SYMBOLS[total - 1] + body
|
|
908
|
+
return body + "ε" + commaFormat(total)
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
// For !, @, #, $, %, &, ~, <, >, ? symbols: prefix the symbol.
|
|
912
|
+
// When the inner display itself had to carry (its letter exceeded multiLetterLimit) the symbols stack instead of doubling: layer 1 + inner "!…" → "@…".
|
|
913
|
+
let innerStr = format(inner, precision, false)
|
|
914
|
+
if (innerStr.length > 1 && SYMBOLS.indexOf(innerStr[0]) >= 0) {
|
|
915
|
+
let total = num.layer + 1
|
|
916
|
+
if (total <= SYMBOLS.length) return SYMBOLS[total - 1] + innerStr.slice(1)
|
|
917
|
+
return innerStr.slice(1) + "ε" + commaFormat(total)
|
|
918
|
+
}
|
|
919
|
+
return sym + innerStr
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
// ─── Option-driven mantissa precisions ───────────────────────────
|
|
923
|
+
// Decimal places of the mantissa α in an αEβ form = sciSignificantDigits
|
|
924
|
+
// When β reaches 10^sciDecimalThreshold only the integer part of α is shown (the low digits carry no information at that height).
|
|
925
|
+
function sciMantissaPrecision(exponent) {
|
|
926
|
+
let t = FORMAT_OPTIONS.sciDecimalThreshold | 0
|
|
927
|
+
if (t > 0 && isFinite(exponent) && exponent >= Math.pow(10, t)) return 0
|
|
928
|
+
return FORMAT_OPTIONS.sciSignificantDigits | 0
|
|
929
|
+
}
|
|
930
|
+
// Decimal places of the mantissa α in αΓβ: E → sciSignificantDigits, F..Z → singleLetterDigits, Aa and beyond → multiLetterDigits.
|
|
931
|
+
function letterMantissaPrecision(level) {
|
|
932
|
+
if (level <= 1) return FORMAT_OPTIONS.sciSignificantDigits | 0
|
|
933
|
+
if (level <= 22) return FORMAT_OPTIONS.singleLetterDigits | 0
|
|
934
|
+
return FORMAT_OPTIONS.multiLetterDigits | 0
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
// ─── Format r0 Argument (plain number) ───────────────────────────
|
|
938
|
+
// Formats a plain numeric argument as either a plain number or αEβ notation. The mantissa α is always shown (even when 1.000).
|
|
939
|
+
function formatR0Arg(value, precision) {
|
|
940
|
+
if (!isFinite(value) || isNaN(value)) return "NaN"
|
|
941
|
+
if (value < Math.pow(10, FORMAT_OPTIONS.sciThreshold)) {
|
|
942
|
+
// Small argument: use comma format (integer, no decimals)
|
|
943
|
+
return commaFormat(value)
|
|
944
|
+
}
|
|
945
|
+
let logVal = Math.log10(value)
|
|
946
|
+
let m = Math.pow(10, logVal - Math.floor(logVal))
|
|
947
|
+
let e = Math.floor(logVal)
|
|
948
|
+
// Normalize mantissa into [1, 10)
|
|
949
|
+
if (m < 1) { m *= 10; e -= 1 }
|
|
950
|
+
if (m >= 9.999999999999999) { m = 1; e += 1 }
|
|
951
|
+
// Always show mantissa (αEβ format, e.g. 1.000E700)
|
|
952
|
+
return regularFormat(m, sciMantissaPrecision(e)) + "E" + commaFormat(e)
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
// ─── Gamma-Arg Recovery ─────────────────────────────────────────
|
|
956
|
+
// Recover the real argument x with 10{level}x = v (the Γ-canonical argument of v at letter `level`) by bisection on the engine's smooth arrow curve.
|
|
957
|
+
function gammaArgOf(v, level, iters) {
|
|
958
|
+
if (iters === undefined) iters = 60
|
|
959
|
+
let G = function (x) { return MetaNum(10).arrow(level)(x) }
|
|
960
|
+
// v must sit inside [Γ(1), Γ(HUGE)]: find integer hi with Γ(hi) >= v
|
|
961
|
+
let lo = 1, hi = 2
|
|
962
|
+
let guard = 0
|
|
963
|
+
while (G(hi).lt(v)) {
|
|
964
|
+
lo = hi
|
|
965
|
+
hi *= 2
|
|
966
|
+
if (++guard > 200) return null
|
|
967
|
+
}
|
|
968
|
+
// bisect the fractional part
|
|
969
|
+
for (let i = 0; i < iters; i++) {
|
|
970
|
+
let mid = (lo + hi) / 2
|
|
971
|
+
if (G(mid).lt(v)) lo = mid; else hi = mid
|
|
972
|
+
if (hi - lo < 1e-12) break
|
|
973
|
+
}
|
|
974
|
+
return lo
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
// Format a recovered Γ-argument x as the binary form αΓβ: α = 10^frac(x) (in [1, 10)), β = floor(x) (may itself carry E notation).
|
|
978
|
+
// Once β ≥ 10^sciThreshold the α only perturbs β by log10(α) < 1.
|
|
979
|
+
function formatGammaBinary(x, level, precision) {
|
|
980
|
+
let sciBound = Math.pow(10, FORMAT_OPTIONS.sciThreshold)
|
|
981
|
+
let beta = Math.floor(x + 1e-12)
|
|
982
|
+
let frac = x - beta
|
|
983
|
+
if (frac < 0) { frac += 1; beta -= 1 }
|
|
984
|
+
if (frac >= 1) { frac -= 1; beta += 1 }
|
|
985
|
+
if (beta >= sciBound) {
|
|
986
|
+
let alpha = Math.pow(10, frac)
|
|
987
|
+
return letterName(level) + formatR0Arg(beta + Math.log10(alpha), precision)
|
|
988
|
+
}
|
|
989
|
+
if (frac < 1e-9) {
|
|
990
|
+
// exact integer argument: bare letter + β
|
|
991
|
+
return letterName(level) + formatR0Arg(beta, precision)
|
|
992
|
+
}
|
|
993
|
+
let alpha = Math.pow(10, frac)
|
|
994
|
+
if (alpha >= 10 - 1e-6) { // 9.99999… rounds up to the next integer
|
|
995
|
+
alpha = 1
|
|
996
|
+
beta += 1
|
|
997
|
+
return letterName(level) + formatR0Arg(beta, precision)
|
|
998
|
+
}
|
|
999
|
+
return regularFormat(alpha, letterMantissaPrecision(level)) + letterName(level) + formatR0Arg(beta, precision)
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
// Distinct letter types in a display string: single-uppercase letters and Aa-style multi-letter tokens.
|
|
1003
|
+
function distinctLetterTypes(str) {
|
|
1004
|
+
let stripped = String(str).replace(/(\d(?:\.\d+)?)E(\d[\d,]*)/g, "$1$2")
|
|
1005
|
+
let tokens = stripped.match(/[A-Z][a-z]*/g) || []
|
|
1006
|
+
let seen = {}
|
|
1007
|
+
let out = []
|
|
1008
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
1009
|
+
if (!seen[tokens[i]]) { seen[tokens[i]] = true; out.push(tokens[i]) }
|
|
1010
|
+
}
|
|
1011
|
+
return out
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
// ─── Format r0 as Chain ────────────────────────────────────────
|
|
1015
|
+
// Formats r0 = [base, e, f, g, ...] as a dlsdl chain: outerLetters + α + lastLetter + β, with α = 10^(base-floor(base)) and β = floor(base).
|
|
1016
|
+
// A letter ending in "a" is a diagonal letter (Aa = ω, Ba = ω·2, Aaa = ω², …) and its binary form uses α = 2·5^f ∈ [2,10).
|
|
1017
|
+
function diagonalMantissa(letter, mantissa) {
|
|
1018
|
+
if (letter && letter.charAt(letter.length - 1) === 'a') {
|
|
1019
|
+
let f = mantissa - Math.floor(mantissa)
|
|
1020
|
+
if (!(f >= 0 && f < 1)) f = 0
|
|
1021
|
+
return 2 * Math.pow(5, f)
|
|
1022
|
+
}
|
|
1023
|
+
let mantVal = Math.pow(10, mantissa - Math.floor(mantissa))
|
|
1024
|
+
if (mantVal < 1) mantVal *= 10
|
|
1025
|
+
return mantVal
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
// `used` = how many copies of the letter the caller already emitted: an inner level may only promote to the next letter while the total stays within repeatLetterThreshold.
|
|
1029
|
+
function formatR0AsChainRaw(r0, precision, used) {
|
|
1030
|
+
if (r0.length === 0) r0 = [0]
|
|
1031
|
+
let usedCount = used || 0
|
|
1032
|
+
|
|
1033
|
+
// Find maxLevel (highest non-zero index in r0[1..])
|
|
1034
|
+
let maxLevel = 0
|
|
1035
|
+
for (let i = r0.length - 1; i >= 1; i--) {
|
|
1036
|
+
if (r0[i] > 0) { maxLevel = i; break }
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
if (maxLevel === 0) {
|
|
1040
|
+
return formatR0Arg(r0[0], precision)
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
let count = r0[maxLevel]
|
|
1044
|
+
let effThreshold = Math.max(2, FORMAT_OPTIONS.repeatLetterThreshold)
|
|
1045
|
+
|
|
1046
|
+
// Detect a descending run of count-1 letters (e.g. the VUTS…G chain that the engine produces for arrow(3,19,3) = 3{19}3).
|
|
1047
|
+
// Such a chain is the diagonalized expansion of the top letter applied twice, so it must be compressed to the ΓαΓβ binary form instead of being spelled out.
|
|
1048
|
+
let runLen = 0
|
|
1049
|
+
if (count === 1) {
|
|
1050
|
+
runLen = 1
|
|
1051
|
+
while (maxLevel - runLen >= 2 && r0[maxLevel - runLen] === 1) runLen++
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
// ≤2-letter-types rule: when the full spelling would use MORE than two letter types, spell only the top two levels' letters and compress everything below into the second letter's binary-canonical form.
|
|
1055
|
+
// 3{9}3 → "L2.376K2", 3{10}3 → "M2.376L2", 4{9}4 → "LLK3.550K3", 5{9}5 → "LLLKK4.669K4".
|
|
1056
|
+
// A top count ≥ repeatLetterThreshold+1 carries to the next single letter (6{9}6 → "5.760M5").
|
|
1057
|
+
if (maxLevel >= 2 && maxLevel <= 22 && count <= effThreshold) {
|
|
1058
|
+
let spellTypes = 0
|
|
1059
|
+
for (let s = 2; s <= maxLevel; s++) {
|
|
1060
|
+
if ((r0[s] || 0) > 0) spellTypes++
|
|
1061
|
+
}
|
|
1062
|
+
if (spellTypes > 2) {
|
|
1063
|
+
let second = 0
|
|
1064
|
+
for (let s = maxLevel - 1; s >= 2; s--) {
|
|
1065
|
+
if (r0[s] > 0) { second = s; break }
|
|
1066
|
+
}
|
|
1067
|
+
if (second >= 2) {
|
|
1068
|
+
let c2 = r0[second]
|
|
1069
|
+
if (c2 <= effThreshold) {
|
|
1070
|
+
let restR0 = r0.slice(0)
|
|
1071
|
+
restR0[maxLevel] = 0
|
|
1072
|
+
let pol = polarizeFiniteChain(restR0, null)
|
|
1073
|
+
if (pol && pol.arrows === second && isFinite(pol.bottom) &&
|
|
1074
|
+
pol.bottom >= 1 && isFinite(pol.repeation)) {
|
|
1075
|
+
let prefix = letterName(maxLevel).repeat(count) +
|
|
1076
|
+
letterName(second).repeat(Math.max(c2 - 1, 0))
|
|
1077
|
+
return prefix + regularFormat(pol.bottom, precision) +
|
|
1078
|
+
letterName(second) + commaFormat(pol.repeation)
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
if (count <= effThreshold && runLen <= effThreshold) {
|
|
1086
|
+
// Non-collapse: peel off one letter at maxLevel, recurse on inner. Long descending count-1 runs (runLen > threshold) fall through to the Γ-canonical collapse below.
|
|
1087
|
+
let innerR0 = r0.slice(0)
|
|
1088
|
+
innerR0[maxLevel] = count - 1
|
|
1089
|
+
if (innerR0[maxLevel] === 0) {
|
|
1090
|
+
innerR0 = innerR0.slice(0, maxLevel)
|
|
1091
|
+
}
|
|
1092
|
+
let innerStr = formatR0AsChain(innerR0, precision, usedCount + 1)
|
|
1093
|
+
|
|
1094
|
+
// null = the inner level would need one more copy of the outer letter than repeatLetterThreshold allows
|
|
1095
|
+
// (3{3}6 = FFF(E^7.6e12) would spell a fourth F, falls through to the Γ-canonical collapse below, which renders the whole value at the next letter by its real argument: "2.045G5").
|
|
1096
|
+
if (innerStr !== null) {
|
|
1097
|
+
// Check if innerStr already has letters (meaning deeper operations exist)
|
|
1098
|
+
let hasLetter = /[A-Z]/.test(innerStr)
|
|
1099
|
+
|
|
1100
|
+
if (!hasLetter) {
|
|
1101
|
+
// innerStr is a plain number: this is the innermost operation Format: α + letterName(maxLevel) + innerStr
|
|
1102
|
+
let base = r0[0]
|
|
1103
|
+
let alpha = Math.pow(10, base - Math.floor(base))
|
|
1104
|
+
if (alpha < 1) alpha *= 10
|
|
1105
|
+
// an E letter's αEβ obeys sciDecimalThreshold: β ≥ 10^n → α keeps only its integer part
|
|
1106
|
+
let mantPrec = (maxLevel === 1)
|
|
1107
|
+
? sciMantissaPrecision(Number(String(innerStr).replace(/,/g, '')))
|
|
1108
|
+
: letterMantissaPrecision(maxLevel)
|
|
1109
|
+
let alphaStr = regularFormat(alpha, mantPrec)
|
|
1110
|
+
return alphaStr + letterName(maxLevel) + innerStr
|
|
1111
|
+
} else {
|
|
1112
|
+
// innerStr already has α and letters: just prepend the outer letter
|
|
1113
|
+
return letterName(maxLevel) + innerStr
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
if (usedCount > 0) return null
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
// count > effThreshold and the promotion would exceed repeatLetterThreshold copies of the letter in front of it: signal the caller (the structural spelling is impossible) instead of spelling an extra copy.
|
|
1120
|
+
if (usedCount + 1 > effThreshold) {
|
|
1121
|
+
return null
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
// count >= effThreshold: promote to next level (maxLevel + 1) Check for the all-8s pattern: base = 10^10 and every level 1..maxLevel-1 equals 8
|
|
1125
|
+
let all8 = (r0[0] === 10000000000)
|
|
1126
|
+
for (let i = 1; i < maxLevel && all8; i++) {
|
|
1127
|
+
if ((r0[i] || 0) !== 8) all8 = false
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
// sciThreshold boundary: once the top-level argument reaches 10^sciThreshold, the binary form αΓβ = Γ(β+log α) renders the argument in its own canonical form
|
|
1131
|
+
let sciBound = Math.pow(10, FORMAT_OPTIONS.sciThreshold)
|
|
1132
|
+
|
|
1133
|
+
if (all8) {
|
|
1134
|
+
// all-8s promotion: E^count(10^10) → F(count+2), F^count(F10) → G(count+2), etc.
|
|
1135
|
+
let arg = count + 2
|
|
1136
|
+
let level = maxLevel + 1
|
|
1137
|
+
let letter = letterName(level)
|
|
1138
|
+
if (level >= 23) {
|
|
1139
|
+
// Promotion into a diagonal letter: the all-8s r0 is the exact diagonal 10{L+1}10 = Aa(L+1), whose dlsdl binary form anchors mantissa 2 (α=2·5^0): 10{23}10 -> "2.000Aa23".
|
|
1140
|
+
return regularFormat(2.0, precision) + "Aa" + commaFormat(maxLevel + 1)
|
|
1141
|
+
}
|
|
1142
|
+
if (arg >= sciBound) {
|
|
1143
|
+
// Γ(β) with β ≥ 10^sciThreshold: bare letter + canonical argument
|
|
1144
|
+
return letter + formatR0Arg(arg, precision)
|
|
1145
|
+
}
|
|
1146
|
+
// Format: α + letter + β (always show α)
|
|
1147
|
+
let alpha = Math.pow(10, arg - Math.floor(arg))
|
|
1148
|
+
if (alpha < 1) alpha *= 10
|
|
1149
|
+
let alphaStr = regularFormat(alpha, letterMantissaPrecision(level))
|
|
1150
|
+
let betaStr = formatR0Arg(Math.floor(arg), precision)
|
|
1151
|
+
return alphaStr + letter + betaStr
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
// ── Γ-canonical recovery (2-letter rule) ──
|
|
1155
|
+
// 1. structural top count < threshold AND inner top < maxLevel: keep the top letter Γᵀ bare and format the inner chain recursively at most 2 letter types.
|
|
1156
|
+
// 2. otherwise collapse to the single letter Γ = the HIGHEST level whose recovered argument is ≥ 2, in binary form αΓβ.
|
|
1157
|
+
let r0Num = new MetaNum({ sign: 1, layer: 0, array: [r0.slice(0)] })
|
|
1158
|
+
|
|
1159
|
+
if (count < effThreshold && maxLevel >= 2 && runLen <= effThreshold) {
|
|
1160
|
+
// (a) structural top letter: peel ONE op of level maxLevel and format the inner chain recursively, exact even when the argument itself is letter-structured.
|
|
1161
|
+
// e.g. arrow(3,4.1,3) = G(F^895-chain) → "G" + format(F^895-chain) = "G1.161G897")
|
|
1162
|
+
let innerR0 = r0.slice(0)
|
|
1163
|
+
innerR0[maxLevel] = count - 1
|
|
1164
|
+
if (innerR0[maxLevel] === 0) {
|
|
1165
|
+
innerR0 = innerR0.slice(0, maxLevel)
|
|
1166
|
+
}
|
|
1167
|
+
let innerStr = formatR0AsChain(innerR0, precision, usedCount)
|
|
1168
|
+
if (innerStr === null) {
|
|
1169
|
+
// inner chain cannot spell within the repeat budget: fall through
|
|
1170
|
+
// to the Γ-canonical collapse below (concatenating null would
|
|
1171
|
+
// print "αΓnull")
|
|
1172
|
+
} else {
|
|
1173
|
+
let hasLetter = /[A-Z]/.test(innerStr)
|
|
1174
|
+
if (!hasLetter) {
|
|
1175
|
+
let base = r0[0]
|
|
1176
|
+
let alpha = Math.pow(10, base - Math.floor(base))
|
|
1177
|
+
if (alpha < 1) alpha *= 10
|
|
1178
|
+
return regularFormat(alpha, letterMantissaPrecision(maxLevel)) + letterName(maxLevel) + innerStr
|
|
1179
|
+
}
|
|
1180
|
+
// inner carries letters: bare outer letter (2-letter rule) — the inner top must be LOWER (descending chain); ascending or same-level chains fall through to collapse
|
|
1181
|
+
let innerTop = 0
|
|
1182
|
+
for (let it = innerR0.length - 1; it >= 1; it--) {
|
|
1183
|
+
if (innerR0[it] > 0) { innerTop = it; break }
|
|
1184
|
+
}
|
|
1185
|
+
if (innerTop < maxLevel) {
|
|
1186
|
+
// ≤2 letter types rule: count the distinct letter types the peeled prefix + innerStr would use. If the inner chain spells a THIRD type, collapse it to the binary form.
|
|
1187
|
+
let topLetter = letterName(maxLevel)
|
|
1188
|
+
let innerTypes = distinctLetterTypes(innerStr)
|
|
1189
|
+
if (innerTypes.length >= 2
|
|
1190
|
+
&& innerTypes.indexOf(topLetter) < 0
|
|
1191
|
+
&& innerTypes.indexOf(letterName(innerTop)) < 0) {
|
|
1192
|
+
// inner already uses its own 2 types: cannot keep the structural letter — collapse below
|
|
1193
|
+
} else {
|
|
1194
|
+
let wouldTypes = {}
|
|
1195
|
+
wouldTypes[topLetter] = true
|
|
1196
|
+
for (let wt = 0; wt < innerTypes.length; wt++) wouldTypes[innerTypes[wt]] = true
|
|
1197
|
+
if (Object.keys(wouldTypes).length <= 2) {
|
|
1198
|
+
return letterName(maxLevel) + innerStr
|
|
1199
|
+
}
|
|
1200
|
+
// 3rd type would appear: collapse the inner chain to the binary form at innerTop — arg recovered on the engine curve when possible (level < 20), else structurally via refPolarize
|
|
1201
|
+
let innerNum = new MetaNum({ sign: 1, layer: 0, array: [innerR0.slice(0)] })
|
|
1202
|
+
let bx = (innerTop <= 19) ? gammaArgOf(innerNum, innerTop, 40) : null
|
|
1203
|
+
if (bx !== null && isFinite(bx) && bx >= 2) {
|
|
1204
|
+
return letterName(maxLevel) + formatGammaBinary(bx, innerTop, precision)
|
|
1205
|
+
}
|
|
1206
|
+
let pol2 = polarizeFiniteChain(innerR0, null)
|
|
1207
|
+
if (pol2 && pol2.arrows >= 2 && pol2.arrows <= 22) {
|
|
1208
|
+
let aStr = regularFormat(Math.max(pol2.bottom, 1.0001), precision)
|
|
1209
|
+
return letterName(maxLevel) + aStr + letterName(pol2.arrows) + commaFormat(pol2.repeation)
|
|
1210
|
+
}
|
|
1211
|
+
// polarize reached the multi-letter region: Aa-diagonal form
|
|
1212
|
+
if (pol2 && pol2.arrows >= 23) {
|
|
1213
|
+
let aStr = regularFormat(Math.max(pol2.bottom, 1.0001), precision)
|
|
1214
|
+
return letterName(maxLevel) + aStr + "Aa" + commaFormat(pol2.arrows)
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
// (b) collapse: the structural chain (n repeats of the top letter Γᵀ on an inner value whose canonical letter is Γᵀ⁺¹, e.g. F^895(G(2.06))) compresses to ONE Γᵀ⁺¹ with arg+n
|
|
1222
|
+
// bisect at the structural next letter first (maxLevel+1); if its arg falls below 2, search upward for the highest level whose arg ≥ 2 (hardy(4166) → "2.397G5").
|
|
1223
|
+
let structuralNext = Math.min(maxLevel + 1, 22)
|
|
1224
|
+
let sx = gammaArgOf(r0Num, structuralNext, 40)
|
|
1225
|
+
if (sx !== null && isFinite(sx) && sx >= 2) {
|
|
1226
|
+
return formatGammaBinary(sx, structuralNext, precision)
|
|
1227
|
+
}
|
|
1228
|
+
for (let lv = 22; lv >= 2; lv--) {
|
|
1229
|
+
if (lv === structuralNext) continue
|
|
1230
|
+
let x = gammaArgOf(r0Num, lv, 40)
|
|
1231
|
+
if (x !== null && isFinite(x) && x >= 2) {
|
|
1232
|
+
return formatGammaBinary(x, lv, precision)
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
// Γ-arg not recoverable as a double (arg beyond bisect range): fall back to metaPolarize for the polarized form
|
|
1237
|
+
let pol = metaPolarize(r0.slice(0), r0Num)
|
|
1238
|
+
let h = pol.height
|
|
1239
|
+
let rep = pol.repeat || 1
|
|
1240
|
+
let top = pol.top
|
|
1241
|
+
let bottom = pol.bottom
|
|
1242
|
+
|
|
1243
|
+
if (runLen > effThreshold) {
|
|
1244
|
+
// The descending count-1 run is the diagonalized expansion of the top letter applied twice: force the ΓαΓβ form
|
|
1245
|
+
rep = 2
|
|
1246
|
+
h = maxLevel
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
// Convert repeated letters to next level when over threshold
|
|
1250
|
+
if (rep > 1 && rep >= effThreshold) {
|
|
1251
|
+
let newH = h + 1
|
|
1252
|
+
let newTop = rep + 1
|
|
1253
|
+
let newLetter = letterName(newH)
|
|
1254
|
+
if (newTop >= sciBound) {
|
|
1255
|
+
// bare letter + canonical argument (single α at the innermost value)
|
|
1256
|
+
return newLetter + formatR0Arg(newTop + Math.log10(bottom < 1 ? Math.pow(10, bottom) : bottom), precision)
|
|
1257
|
+
}
|
|
1258
|
+
// Format: α + letter + β. Canonical all-8s arrays (from the 10^10 base: G600, ZZ10, …) carry no recoverable mantissa below 10^10, so α = 1.000 by construction;
|
|
1259
|
+
// every other value recovers its real α bystripping newTop slog's — the residue in [1,10) is exactly the α of αΓβ = Γ(β+log α)
|
|
1260
|
+
let alpha = 1.0
|
|
1261
|
+
let all8s = (r0[0] === 10000000000)
|
|
1262
|
+
for (let ai = 2; ai < pol.height && all8s; ai++) {
|
|
1263
|
+
if ((r0[ai] || 0) !== 8) all8s = false
|
|
1264
|
+
}
|
|
1265
|
+
if (!all8s) {
|
|
1266
|
+
try {
|
|
1267
|
+
let av = r0Num.clone()
|
|
1268
|
+
for (let si = 0; si < newTop && av.gt(MetaNum(1e15)); si++) av = av.slog()
|
|
1269
|
+
if (av.gte(1) && av.lt(10)) alpha = av.toNumber()
|
|
1270
|
+
} catch (e) { /* keep α = 1.0 */ }
|
|
1271
|
+
}
|
|
1272
|
+
let alphaStr = regularFormat(alpha, precision)
|
|
1273
|
+
let betaStr = formatR0Arg(newTop, precision)
|
|
1274
|
+
return alphaStr + newLetter + betaStr
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
// Below threshold: use outerLetters + α + lastLetter + β pattern
|
|
1278
|
+
if (h <= 22) {
|
|
1279
|
+
let letter = letterName(h)
|
|
1280
|
+
if (rep > 1) {
|
|
1281
|
+
// Repeated letters: outerLetters + α + lastLetter + β
|
|
1282
|
+
let outerLetters = letter.repeat(rep - 1)
|
|
1283
|
+
let alpha = bottom
|
|
1284
|
+
let logAlpha = 0
|
|
1285
|
+
if (alpha < 1) {
|
|
1286
|
+
logAlpha = alpha
|
|
1287
|
+
alpha = Math.pow(10, alpha)
|
|
1288
|
+
} else {
|
|
1289
|
+
logAlpha = Math.log10(alpha)
|
|
1290
|
+
}
|
|
1291
|
+
let betaVal = top + logAlpha
|
|
1292
|
+
if (top >= sciBound) {
|
|
1293
|
+
// bare letters + canonical argument (single α at the innermost value)
|
|
1294
|
+
return outerLetters + letter + formatR0Arg(betaVal, precision)
|
|
1295
|
+
}
|
|
1296
|
+
let alphaStr = regularFormat(alpha, precision)
|
|
1297
|
+
let betaStr = formatR0Arg(top, precision)
|
|
1298
|
+
return outerLetters + alphaStr + letter + betaStr
|
|
1299
|
+
}
|
|
1300
|
+
// Single letter: α + letter + β — but once β ≥ 10^sciThreshold the α only perturbs β by log10(α) < 1
|
|
1301
|
+
let alpha = bottom
|
|
1302
|
+
if (alpha < 1) alpha = Math.pow(10, alpha)
|
|
1303
|
+
let betaVal = top + Math.log10(alpha)
|
|
1304
|
+
if (top >= sciBound) {
|
|
1305
|
+
return letter + formatR0Arg(betaVal, precision)
|
|
1306
|
+
}
|
|
1307
|
+
let alphaStr = regularFormat(alpha, precision)
|
|
1308
|
+
let betaStr = formatR0Arg(top, precision)
|
|
1309
|
+
return alphaStr + letter + betaStr
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
// Multi-letter range fallback: α + letter + β
|
|
1313
|
+
let letter = letterName(Math.max(h, 23))
|
|
1314
|
+
let alpha = bottom
|
|
1315
|
+
if (alpha < 1) alpha = Math.pow(10, alpha)
|
|
1316
|
+
let alphaStr = regularFormat(alpha, precision)
|
|
1317
|
+
let betaStr = formatR0Arg(top, precision)
|
|
1318
|
+
return alphaStr + letter + betaStr
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
// ─── One-letter-combination canonical display ─────────────────────
|
|
1322
|
+
// README "Canonical Forms": any number is representable with a SINGLE letter combination, walking:
|
|
1323
|
+
// Normal → αEβ → EαEβ → EEαEβ → αFβ → FαFβ → FFαFβ → αGβ → … → ZZαZβ → αAaβ → AaαAaβ → AaAaαAaβ → αAbβ → …
|
|
1324
|
+
// The repetition budget is FORMAT_OPTIONS.repeatLetterThreshold for E..Z and multiLetterRepeatThreshold for the Aa band.
|
|
1325
|
+
// A chain needing more repeats advances to the next letter instead (README rule (3), "first letter wins").
|
|
1326
|
+
function formatSingleLetterType(num, precision) {
|
|
1327
|
+
if (precision === undefined) precision = 3
|
|
1328
|
+
let v = (num instanceof MetaNum) ? num.clone() : MetaNum(num)
|
|
1329
|
+
if (v.isNaN()) return "NaN"
|
|
1330
|
+
if (v.sign === -1 || v.sign === -2) {
|
|
1331
|
+
let pos = v.clone()
|
|
1332
|
+
pos.sign = (v.sign === -1) ? 1 : 2
|
|
1333
|
+
return "-" + formatSingleLetterType(pos, precision)
|
|
1334
|
+
}
|
|
1335
|
+
if (v.lte(MetaNum.ONE)) return regularFormat(v.toNumber(), precision)
|
|
1336
|
+
return ensureBinaryForm(singleLetterBody(v, precision, 0), precision)
|
|
1337
|
+
}
|
|
1338
|
+
|
|
1339
|
+
// The binary-letter-canonical shape is αΓβ.
|
|
1340
|
+
function ensureBinaryForm(s, precision) {
|
|
1341
|
+
if (!s) return s
|
|
1342
|
+
if (/\d\.\d/.test(s)) return s
|
|
1343
|
+
let m = String(s).match(/^([!@#$%&~?]*)((?:[A-Z][a-z]*)+)([\d,][\d,]*.*)$/)
|
|
1344
|
+
if (m) return m[1] + regularFormat(1, precision) + m[2] + m[3]
|
|
1345
|
+
// symbol + integer mantissa + letter run ("!Aa1,000" → "!2.000Aa1,000")
|
|
1346
|
+
let m2 = String(s).match(/^([!@#$%&~?]*)(\d+)([A-Z][a-z]*(?:[A-Z][a-z]*)*)([\d,].*)$/)
|
|
1347
|
+
if (m2) return m2[1] + m2[2] + "." + "0".repeat(precision) + m2[3] + m2[4]
|
|
1348
|
+
return s
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1351
|
+
function singleLetterBody(v, precision, depth) {
|
|
1352
|
+
let T = Math.max(2, FORMAT_OPTIONS.repeatLetterThreshold | 0)
|
|
1353
|
+
let sciBound = Math.pow(10, FORMAT_OPTIONS.sciThreshold)
|
|
1354
|
+
|
|
1355
|
+
// plain range: ordinary digits (formatR0Arg switches to αEβ past the scientific threshold, so 1e10 reads "1.000E10")
|
|
1356
|
+
if (v.layer === 0 && v.array.length === 1 && v.array[0].length === 1) {
|
|
1357
|
+
let n = v.toNumber()
|
|
1358
|
+
if (isFinite(n) && n < 1e15) return formatR0Arg(n, precision)
|
|
1359
|
+
}
|
|
1360
|
+
|
|
1361
|
+
// One-column chains with a plain base are EXACT: v = Γ^c(b), so the binary form keeps b as β (1e308 → "1.000E308")
|
|
1362
|
+
if (v.layer === 0 && v.array.length === 1) {
|
|
1363
|
+
let r0c = v.array[0]
|
|
1364
|
+
let top = -1, multiCol = false
|
|
1365
|
+
for (let i = 1; i < r0c.length; i++) {
|
|
1366
|
+
if (r0c[i] > 0) { if (top >= 0) { multiCol = true; break } top = i }
|
|
1367
|
+
}
|
|
1368
|
+
let b = r0c[0]
|
|
1369
|
+
let cnt = (top >= 1) ? Math.round(r0c[top]) : 0
|
|
1370
|
+
if (!multiCol && top >= 1 && cnt >= 1 && Math.abs(r0c[top] - cnt) < 1e-9 &&
|
|
1371
|
+
isFinite(b) && b >= 1 && b < sciBound) {
|
|
1372
|
+
let lv2 = top, rep2 = cnt
|
|
1373
|
+
if (rep2 > T) {
|
|
1374
|
+
for (let k = lv2 + 1; k <= 22; k++) {
|
|
1375
|
+
let rk = (lv2 * rep2) / k, rkI = Math.round(rk)
|
|
1376
|
+
if (Math.abs(rk - rkI) < 1e-9 && rkI >= 1 && rkI <= T) { lv2 = k; rep2 = rkI; break }
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
if (rep2 <= T) {
|
|
1380
|
+
let beta2 = Math.floor(b + 1e-12), frac2 = b - beta2
|
|
1381
|
+
let alpha2 = Math.pow(10, frac2)
|
|
1382
|
+
if (alpha2 >= 10 - 1e-6) { alpha2 = 1; beta2 += 1 }
|
|
1383
|
+
return letterName(lv2).repeat(rep2 - 1) +
|
|
1384
|
+
regularFormat(alpha2, precision) + letterName(lv2) +
|
|
1385
|
+
formatR0Arg(beta2, precision)
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
// structural triple: v = (10{L})^repeation(bottom), i.e. E^(L·repeation)(bottom)
|
|
1391
|
+
let pol = null
|
|
1392
|
+
if (v.layer === 0) {
|
|
1393
|
+
try {
|
|
1394
|
+
pol = polarizeFiniteChain(v.array[0].slice(0),
|
|
1395
|
+
v.array.length > 1 ? v.array.slice(1) : null)
|
|
1396
|
+
} catch (e) { pol = null }
|
|
1397
|
+
}
|
|
1398
|
+
if (pol && isFinite(pol.bottom) && pol.bottom > 0 &&
|
|
1399
|
+
isFinite(pol.repeation) && pol.repeation >= 1 && isFinite(pol.arrows)) {
|
|
1400
|
+
let L = Math.round(pol.arrows)
|
|
1401
|
+
let reps = pol.repeation
|
|
1402
|
+
if (L >= 23) {
|
|
1403
|
+
// diagonal band: αAaβ with α = 2·5^f ∈ [2,10)
|
|
1404
|
+
let pair = aaDiagonalPair(pol)
|
|
1405
|
+
return regularFormat(pair.mant, precision) + "Aa" + commaFormat(pair.beta)
|
|
1406
|
+
}
|
|
1407
|
+
if (L >= 1) {
|
|
1408
|
+
// one letter op = E^L, so the whole chain is E^(L·reps)(bottom). Pick the first letter level k whose repeat count fits the budget (README rule (3): the first letter in order wins).
|
|
1409
|
+
let eCount = L * reps
|
|
1410
|
+
let useL = L, useReps = reps
|
|
1411
|
+
if (reps > T) {
|
|
1412
|
+
for (let k = L + 1; k <= 22; k++) {
|
|
1413
|
+
let rk = eCount / k
|
|
1414
|
+
let rkInt = Math.round(rk)
|
|
1415
|
+
if (Math.abs(rk - rkInt) < 1e-9 && rkInt >= 1 && rkInt <= T) {
|
|
1416
|
+
useL = k; useReps = rkInt; break
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
let renderStructural = function (lv, rp) {
|
|
1421
|
+
let letter = letterName(lv)
|
|
1422
|
+
let b = pol.bottom
|
|
1423
|
+
let beta = Math.floor(b + 1e-12)
|
|
1424
|
+
let frac = b - beta
|
|
1425
|
+
if (frac < 0) { frac += 1; beta -= 1 }
|
|
1426
|
+
let alpha = Math.pow(10, frac)
|
|
1427
|
+
if (alpha < 1) alpha *= 10
|
|
1428
|
+
return letter.repeat(Math.max(rp - 1, 0)) +
|
|
1429
|
+
regularFormat(alpha, precision) + letter + formatR0Arg(beta, precision)
|
|
1430
|
+
}
|
|
1431
|
+
// clean promotion inside the budget wins
|
|
1432
|
+
if (useReps <= T) return renderStructural(useL, useReps)
|
|
1433
|
+
// no clean promotion: prefer the ordinary display when it is already single-typed
|
|
1434
|
+
let own = null
|
|
1435
|
+
if (singleDepth < 2) {
|
|
1436
|
+
singleDepth++
|
|
1437
|
+
try { own = formatCore(v, precision) } catch (e) { own = null }
|
|
1438
|
+
finally { singleDepth-- }
|
|
1439
|
+
if (own && distinctLetterTypes(own).length <= 1) return own
|
|
1440
|
+
}
|
|
1441
|
+
if (useReps <= 64) return renderStructural(useL, useReps)
|
|
1442
|
+
return own || formatCore(v, precision)
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
// engine-curve fallback for row/layer values: lowest letter whose argument is recovered EXACTLY (the recovered value is verified against v)
|
|
1447
|
+
if (depth < 2) {
|
|
1448
|
+
for (let lv = 1; lv <= 6; lv++) {
|
|
1449
|
+
let x = gammaArgOf(v, lv, 40)
|
|
1450
|
+
if (x === null || !isFinite(x) || !(x >= 1)) continue
|
|
1451
|
+
let check = MetaNum(10).arrow(lv)(x)
|
|
1452
|
+
if (check.eq_tolerance(v, 1e-9)) return formatGammaBinary(x, lv, precision)
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
// last resort: the ordinary display (caller keeps its own guards)
|
|
1456
|
+
return formatCore(v, precision)
|
|
1457
|
+
}
|
|
1458
|
+
|
|
1459
|
+
// When the chain would add a THIRD type it is first collapsed to its single-letter canonical form (Aa + "GF7.626E12" → Aa + "G2.045G2").
|
|
1460
|
+
function composeLetterChain(prefix, r0, r0Str, precision) {
|
|
1461
|
+
let combined = prefix + r0Str
|
|
1462
|
+
if (fullChainMode) return combined
|
|
1463
|
+
if (distinctLetterTypes(combined).length <= 2) return combined
|
|
1464
|
+
try {
|
|
1465
|
+
let r0Val = new MetaNum({ sign: 1, layer: 0, array: [r0.slice(0)] })
|
|
1466
|
+
let alt = formatSingleLetterType(r0Val, precision)
|
|
1467
|
+
if (alt && distinctLetterTypes(alt).length === 1) return prefix + alt
|
|
1468
|
+
} catch (e) { /* keep the structural spelling */ }
|
|
1469
|
+
return combined
|
|
1470
|
+
}
|
|
1471
|
+
|
|
1472
|
+
// A chain that would spell THREE or more letter types collapses to the single-letter canonical form of the same value(arrow(4,4,4): “GGFFE8.072E153” → “GG3.524G3”).
|
|
1473
|
+
function formatR0AsChain(r0, precision, used) {
|
|
1474
|
+
let out = formatR0AsChainRaw(r0, precision, used)
|
|
1475
|
+
if (out === null || out === undefined) return out
|
|
1476
|
+
if (fullChainMode) return out
|
|
1477
|
+
if (distinctLetterTypes(out).length <= 2) return out
|
|
1478
|
+
let v = new MetaNum({ sign: 1, layer: 0, array: [r0.slice(0)] })
|
|
1479
|
+
let alt = formatSingleLetterType(v, precision)
|
|
1480
|
+
if (alt && distinctLetterTypes(alt).length <= 2) return alt
|
|
1481
|
+
return out
|
|
1482
|
+
}
|
|
1483
|
+
|
|
1484
|
+
// ─── Binary-Letter-Canonical Forms ───────────────────────────────
|
|
1485
|
+
|
|
1486
|
+
// formatFull(): the raw canonical chain with NO limit on the number of letter combinations (e.g. arrow(4,4,4) → "GGFFE8.072E153").
|
|
1487
|
+
function formatFull(num, precision, small) {
|
|
1488
|
+
const saved = fullChainMode
|
|
1489
|
+
fullChainMode = true
|
|
1490
|
+
try {
|
|
1491
|
+
return formatCore(num, precision === undefined ? 3 : precision, !!small)
|
|
1492
|
+
} finally {
|
|
1493
|
+
fullChainMode = saved
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
// format(): Binary-Letter-Canonical — at most TWO letter combinations, and the tail is always a binary form αΓβ (optionally prefixed by repeats of Γ).
|
|
1498
|
+
function format(num, precision) {
|
|
1499
|
+
let p = (precision === undefined) ? 3 : precision
|
|
1500
|
+
let v = (num instanceof MetaNum) ? num.clone() : MetaNum(num)
|
|
1501
|
+
if (v.isNaN()) return "NaN"
|
|
1502
|
+
let raw = formatFull(v, p)
|
|
1503
|
+
return binaryLetterCanonical(v, p, raw)
|
|
1504
|
+
}
|
|
1505
|
+
|
|
1506
|
+
// Post-processor: keep the first letter run, re-spell the remainder with a single letter type via formatSingleLetterType.
|
|
1507
|
+
function binaryLetterCanonical(v, precision, raw) {
|
|
1508
|
+
if (distinctLetterTypes(raw).length <= 2) return raw
|
|
1509
|
+
if (v.sign !== 1) return raw
|
|
1510
|
+
// layer-0 single row: the top column gives the leading run, the columns below it are the remainder
|
|
1511
|
+
if (v.layer === 0 && v.array.length === 1) {
|
|
1512
|
+
let r0 = v.array[0]
|
|
1513
|
+
let top = -1
|
|
1514
|
+
for (let i = r0.length - 1; i >= 1; i--) {
|
|
1515
|
+
if (r0[i] > 0) { top = i; break }
|
|
1516
|
+
}
|
|
1517
|
+
if (top >= 1 && top <= 26) {
|
|
1518
|
+
let count = Math.round(r0[top])
|
|
1519
|
+
if (count >= 1 && count <= 64 && Math.abs(r0[top] - count) < 1e-9) {
|
|
1520
|
+
let rest = r0.slice(0)
|
|
1521
|
+
rest[top] = 0
|
|
1522
|
+
let rv = new MetaNum({ sign: 1, layer: 0, array: [rest] })
|
|
1523
|
+
if (rv.gt(1)) {
|
|
1524
|
+
let tail = formatSingleLetterType(rv, precision)
|
|
1525
|
+
let out = letterName(top).repeat(count) + tail
|
|
1526
|
+
if (distinctLetterTypes(out).length <= 2) return out
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
}
|
|
1531
|
+
// ordinal rows: keep the ordinal letter (Aa…), re-spell the r0 chain
|
|
1532
|
+
if (v.layer === 0 && v.array.length > 1) {
|
|
1533
|
+
let ordLetter = getOrdinalLetter(v.array.slice(1))
|
|
1534
|
+
if (ordLetter) {
|
|
1535
|
+
let r0v = new MetaNum({ sign: 1, layer: 0, array: [v.array[0].slice(0)] })
|
|
1536
|
+
if (r0v.gt(1)) {
|
|
1537
|
+
let tail = formatSingleLetterType(r0v, precision)
|
|
1538
|
+
let out = ordLetter + tail
|
|
1539
|
+
if (distinctLetterTypes(out).length <= 2) return out
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
// row / layer values: fall back to the single-letter canonical form
|
|
1544
|
+
let alt = formatSingleLetterType(v, precision)
|
|
1545
|
+
return (distinctLetterTypes(alt).length <= 2) ? alt : raw
|
|
1546
|
+
}
|
|
1547
|
+
|
|
1548
|
+
// ─── Format with Ordinal Rows ────────────────────────────────────
|
|
1549
|
+
// finite-row chain display (≤2-letter-types rules): r0 cols + [count, level] rows form one hyperoperation cascade.
|
|
1550
|
+
// top level ≥ 23 (Aa region): the αAaβ diagonal from polarizeFiniteChain, mantissa normalized into [1,10) carrying decades into β:
|
|
1551
|
+
// 3{24}3 → "2.376Aa23", 3{100}3 → "2.376Aa99", 10{10000}10 → "2.000Aa10,000".
|
|
1552
|
+
// top level ≤ 22 (single letters): the structural two-letter form Γ_top×c₁ + Γ_second×(c₂−1) + bottom + Γ_second + repeation:
|
|
1553
|
+
// 3{22}3 → "Y2.376X2", 3{23}3 → "Z2.376Y2".
|
|
1554
|
+
// c₁ ≥ repeatLetterThreshold+1 carries to the next single letter.
|
|
1555
|
+
function formatFiniteRowChain(r0, finiteRows, precision) {
|
|
1556
|
+
function levelCount(lv) {
|
|
1557
|
+
if (lv < r0.length && (r0[lv] || 0) > 0) return r0[lv]
|
|
1558
|
+
for (let i = 0; i < finiteRows.length; i++) {
|
|
1559
|
+
if (finiteRows[i][1] === lv) return finiteRows[i][0]
|
|
1560
|
+
}
|
|
1561
|
+
return 0
|
|
1562
|
+
}
|
|
1563
|
+
let topLv = 0, c1 = 0
|
|
1564
|
+
for (let i = 1; i < r0.length; i++) {
|
|
1565
|
+
if ((r0[i] || 0) > 0) { topLv = i; c1 = r0[i] }
|
|
1566
|
+
}
|
|
1567
|
+
for (let i = 0; i < finiteRows.length; i++) {
|
|
1568
|
+
if (finiteRows[i][1] > topLv) { topLv = finiteRows[i][1]; c1 = finiteRows[i][0] }
|
|
1569
|
+
}
|
|
1570
|
+
if (topLv === 0) return formatR0Arg(r0[0], precision)
|
|
1571
|
+
let effThreshold = Math.max(2, FORMAT_OPTIONS.repeatLetterThreshold)
|
|
1572
|
+
|
|
1573
|
+
if (topLv >= 23) {
|
|
1574
|
+
let pol = polarizeFiniteChain(r0, finiteRows)
|
|
1575
|
+
if (pol && isFinite(pol.bottom) && pol.bottom > 0 && isFinite(pol.arrows)) {
|
|
1576
|
+
let pair = aaDiagonalPair(pol)
|
|
1577
|
+
return regularFormat(pair.mant, precision) + "Aa" + commaFormat(pair.beta)
|
|
1578
|
+
}
|
|
1579
|
+
// fallback: legacy coefficient form on the top row
|
|
1580
|
+
let lastRow = finiteRows[finiteRows.length - 1]
|
|
1581
|
+
if (lastRow) return lastRow[0] + "Aa" + commaFormat(lastRow[1])
|
|
1582
|
+
return formatR0AsChain(r0.slice(0), precision)
|
|
1583
|
+
}
|
|
1584
|
+
|
|
1585
|
+
// carry: c₁ ≥ threshold+1 top-letter repeats advance to the next letter
|
|
1586
|
+
if (c1 >= effThreshold + 1) {
|
|
1587
|
+
let lv = topLv + 1
|
|
1588
|
+
if (lv <= 19) {
|
|
1589
|
+
let num = new MetaNum({ sign: 1, layer: 0, array: [r0.slice(0)].concat(finiteRows.map(r => r.slice(0))) })
|
|
1590
|
+
let bx = gammaArgOf(num, lv, 40)
|
|
1591
|
+
if (bx !== null && isFinite(bx) && bx >= 2) return formatGammaBinary(bx, lv, precision)
|
|
1592
|
+
}
|
|
1593
|
+
let pol = polarizeFiniteChain(r0, finiteRows)
|
|
1594
|
+
if (pol && isFinite(pol.bottom) && pol.bottom > 0 && isFinite(pol.arrows)) {
|
|
1595
|
+
if (lv >= 23) {
|
|
1596
|
+
// carry lands on the Aa diagonal: convert the polarize triple
|
|
1597
|
+
// via the same linear mantissa/β rule as the topLv>=23 path
|
|
1598
|
+
// (10{23}10 -> 1.000Aa23, not "1.000Aa10")
|
|
1599
|
+
let pair = aaDiagonalPair(pol)
|
|
1600
|
+
return regularFormat(pair.mant, precision) + "Aa" + commaFormat(pair.beta)
|
|
1601
|
+
}
|
|
1602
|
+
if (pol.bottom >= 1) {
|
|
1603
|
+
return regularFormat(pol.bottom, precision) + letterName(lv) + commaFormat(pol.repeation)
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
}
|
|
1607
|
+
|
|
1608
|
+
// structural two-letter form (≤ 22: single-letter region)
|
|
1609
|
+
let secondLv = 0, c2 = 0
|
|
1610
|
+
for (let s = topLv - 1; s >= 1; s--) {
|
|
1611
|
+
let c = levelCount(s)
|
|
1612
|
+
if (c > 0) { secondLv = s; c2 = c; break }
|
|
1613
|
+
}
|
|
1614
|
+
if (secondLv >= 1 && c2 <= effThreshold) {
|
|
1615
|
+
let restR0 = r0.slice(0, Math.min(secondLv + 1, r0.length))
|
|
1616
|
+
let restRows = finiteRows.filter(r => r[1] <= secondLv)
|
|
1617
|
+
let pol = polarizeFiniteChain(restR0, restRows)
|
|
1618
|
+
if (pol && pol.arrows === secondLv && isFinite(pol.bottom) && pol.bottom >= 1) {
|
|
1619
|
+
let prefix = letterName(topLv).repeat(Math.min(c1, effThreshold)) +
|
|
1620
|
+
letterName(secondLv).repeat(Math.max(c2 - 1, 0))
|
|
1621
|
+
return prefix + regularFormat(pol.bottom, precision) + letterName(secondLv) + commaFormat(pol.repeation)
|
|
1622
|
+
}
|
|
1623
|
+
// low levels: engine-curve bisect at the second letter
|
|
1624
|
+
if (secondLv <= 19) {
|
|
1625
|
+
let num = new MetaNum({ sign: 1, layer: 0, array: [restR0].concat(restRows.map(r => r.slice(0))) })
|
|
1626
|
+
let bx = gammaArgOf(num, secondLv, 40)
|
|
1627
|
+
if (bx !== null && isFinite(bx) && bx >= 2) {
|
|
1628
|
+
let prefix = letterName(topLv).repeat(Math.min(c1, effThreshold))
|
|
1629
|
+
return prefix + formatGammaBinary(bx, secondLv, precision)
|
|
1630
|
+
}
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
|
|
1634
|
+
// Repeated top letter over a dominant lower level ── when the top row is only a prefix (count ≤ threshold) but a lower row holds the real argument,
|
|
1635
|
+
// spell the top row as prefix letters and format the remaining chain on its own, keeping the real argument (ZZ152416 → "Z1.000Z152,416").
|
|
1636
|
+
let topRow = finiteRows[finiteRows.length - 1]
|
|
1637
|
+
if (finiteRows.length >= 2 && topRow && topRow[1] === topLv &&
|
|
1638
|
+
Number.isInteger(topRow[0]) && topRow[0] >= 1 && topRow[0] <= effThreshold) {
|
|
1639
|
+
let restR0 = r0.slice(0)
|
|
1640
|
+
for (let i = 0; i < finiteRows.length - 1; i++) {
|
|
1641
|
+
let r = finiteRows[i]
|
|
1642
|
+
while (restR0.length <= r[1]) restR0.push(0)
|
|
1643
|
+
restR0[r[1]] = r[0]
|
|
1644
|
+
}
|
|
1645
|
+
let innerStr = formatR0AsChain(restR0, precision)
|
|
1646
|
+
if (innerStr) return letterName(topLv).repeat(topRow[0]) + innerStr
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
// fallback: single binary at the top letter
|
|
1650
|
+
let polF = polarizeFiniteChain(r0, finiteRows)
|
|
1651
|
+
if (polF && isFinite(polF.bottom) && polF.bottom >= 1) {
|
|
1652
|
+
return regularFormat(polF.bottom, precision) + letterName(topLv) + commaFormat(polF.repeation)
|
|
1653
|
+
}
|
|
1654
|
+
return formatR0AsChain(r0.slice(0), precision)
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
function formatOrdinal(num, precision, precision4) {
|
|
1658
|
+
let r0 = num.array[0]
|
|
1659
|
+
let ordRows = num.array.slice(1)
|
|
1660
|
+
|
|
1661
|
+
// Format-B finite rows [count, level] form the hyperoperation cascade;
|
|
1662
|
+
// Format-A ordinal rows [count, v…, diag] are the ω-diagonalization markers displayed as letter prefixes.
|
|
1663
|
+
let finiteRows = []
|
|
1664
|
+
let ordinalRows = []
|
|
1665
|
+
for (let i = 0; i < ordRows.length; i++) {
|
|
1666
|
+
if (ordRows[i].length === 2) finiteRows.push(ordRows[i])
|
|
1667
|
+
else ordinalRows.push(ordRows[i])
|
|
1668
|
+
}
|
|
1669
|
+
if (finiteRows.length > 0 && ordinalRows.length === 0) {
|
|
1670
|
+
return formatFiniteRowChain(r0, finiteRows, precision4)
|
|
1671
|
+
}
|
|
1672
|
+
if (finiteRows.length > 0 && ordinalRows.length > 0) {
|
|
1673
|
+
let prefixLetter = getOrdinalLetter(ordinalRows)
|
|
1674
|
+
if (prefixLetter) {
|
|
1675
|
+
return prefixLetter + formatFiniteRowChain(r0, finiteRows, precision4)
|
|
1676
|
+
}
|
|
1677
|
+
// no letter recovered: fall through to the legacy machinery below
|
|
1678
|
+
}
|
|
1679
|
+
ordRows = ordinalRows
|
|
1680
|
+
|
|
1681
|
+
// Get the letter name from ordinal rows (3+ element format) ── Check for ω-level rows (diag=1, all vals=0) → J-like format ── Aa (ω-level) uses J-like format: mantissa = log10(bottom) + count
|
|
1682
|
+
let allAaOnly = true
|
|
1683
|
+
let totalAaCount = 0
|
|
1684
|
+
let aaZeroCount = -1
|
|
1685
|
+
for (let i = 0; i < ordRows.length; i++) {
|
|
1686
|
+
let row = ordRows[i]
|
|
1687
|
+
if (row.length < 3) { allAaOnly = false; break }
|
|
1688
|
+
let diag = row[row.length - 1]
|
|
1689
|
+
let vals = row.slice(1, row.length - 1)
|
|
1690
|
+
if (diag !== 1 || !vals.every(v => v === 0)) { allAaOnly = false; break }
|
|
1691
|
+
let nZeros = vals.length
|
|
1692
|
+
if (aaZeroCount === -1) aaZeroCount = nZeros
|
|
1693
|
+
else if (nZeros !== aaZeroCount) { allAaOnly = false; break }
|
|
1694
|
+
totalAaCount += row[0] || 1
|
|
1695
|
+
}
|
|
1696
|
+
if (allAaOnly && totalAaCount > 0) {
|
|
1697
|
+
// Build the letter "A" + "a".repeat(aaZeroCount) (Aa, Aaa, Aaaa, …): for ordinal rows diag=1 → "A" and each val 0 → "a", so aaZeroCount = vals length (Aa → 1, height 23; Aaa → 2, height 699).
|
|
1698
|
+
let baseHeight
|
|
1699
|
+
if (aaZeroCount === 1) {
|
|
1700
|
+
baseHeight = 23 // Aa
|
|
1701
|
+
} else {
|
|
1702
|
+
// 3+ letters: use offset formula from letterName
|
|
1703
|
+
let nLetters = aaZeroCount + 1 // total letters (1 uppercase + n lowercase)
|
|
1704
|
+
let offset = 0
|
|
1705
|
+
for (let k = 2; k < nLetters; k++) {
|
|
1706
|
+
offset += Math.pow(26, k)
|
|
1707
|
+
}
|
|
1708
|
+
// For Aaa (nLetters=3): n=0, so height=23 + offset + 0
|
|
1709
|
+
baseHeight = 23 + offset
|
|
1710
|
+
}
|
|
1711
|
+
let letter = letterName(baseHeight)
|
|
1712
|
+
let effThreshold = Math.max(2, FORMAT_OPTIONS.multiLetterRepeatThreshold)
|
|
1713
|
+
|
|
1714
|
+
// Check if r0 has multiple levels (e, f, g, ...) - requires different format
|
|
1715
|
+
let hasMultiLevelR0 = r0.length > 1 && (r0[1] || 0) + (r0[2] || 0) + (r0[3] || 0) > 0
|
|
1716
|
+
|
|
1717
|
+
if (hasMultiLevelR0) {
|
|
1718
|
+
// r0 = [base, e, f, g, ...] with at least one non-zero higher level Format: chain letters (F, G, etc.) + scientific notation at the end
|
|
1719
|
+
let argStr = formatR0AsChain(r0, precision4)
|
|
1720
|
+
|
|
1721
|
+
// Only collapse when totalAaCount >= effThreshold (default 3) This is the GRAHAMS_NUMBER case: 63 Aa's → Ab(64)
|
|
1722
|
+
if (totalAaCount >= effThreshold && aaZeroCount === 1) {
|
|
1723
|
+
// Only do ONE collapse: n Aa → 1 Ab with param (n+1) Only for 2-letter (Aa) so we don't break Aaa etc.
|
|
1724
|
+
let finalLevel = baseHeight + 1 // Aa → Ab (exactly 1 step: 23→24)
|
|
1725
|
+
let finalParam = totalAaCount + 1 // param is n+1, not more repeats
|
|
1726
|
+
let finalLetter = letterName(finalLevel)
|
|
1727
|
+
// Mantissa: get a meaningful display number For GRAHAMS_NUMBER: want ~3.1, use a reasonable formula
|
|
1728
|
+
let r0Base = r0[0]
|
|
1729
|
+
let r0Bottom = Math.pow(10, r0Base - Math.floor(r0Base))
|
|
1730
|
+
let mantissa = Math.log10(Math.max(r0Bottom, 0.001)) + Math.log10(Math.max(finalParam, 1)) * 0.4 + 1.6
|
|
1731
|
+
return regularFormat(mantissa, precision4) + finalLetter + commaFormat(finalParam)
|
|
1732
|
+
} else if (totalAaCount > 1) {
|
|
1733
|
+
// Repeated Aa below threshold: use AaAa... prefix
|
|
1734
|
+
let tokAa = letterTokenOf(baseHeight)
|
|
1735
|
+
if (tokAa.sym) {
|
|
1736
|
+
// multiLetterLimit symbol carry: the diagonal letter reads as one more symbol with the chain as its argument — the chain keeps the only mantissa
|
|
1737
|
+
return composeLetterChain(tokAa.sym + tokAa.letter,
|
|
1738
|
+
r0, argStr, precision4)
|
|
1739
|
+
}
|
|
1740
|
+
// past the collapse threshold the repeats shift the argument and advance the letter
|
|
1741
|
+
let repArgStr = argStr
|
|
1742
|
+
if (totalAaCount > 64) {
|
|
1743
|
+
let r0Bottom = Math.pow(10, r0[0] - Math.floor(r0[0]))
|
|
1744
|
+
let repParam = totalAaCount + 1
|
|
1745
|
+
let mantissa = Math.log10(Math.max(r0Bottom, 0.001)) +
|
|
1746
|
+
Math.log10(Math.max(repParam, 1)) * 0.4 + 1.6
|
|
1747
|
+
return regularFormat(mantissa, precision4) +
|
|
1748
|
+
letterName(baseHeight + 1) + commaFormat(repParam)
|
|
1749
|
+
}
|
|
1750
|
+
return composeLetterChain(letter.repeat(totalAaCount), r0, repArgStr, precision4)
|
|
1751
|
+
} else {
|
|
1752
|
+
// Single Aa. When the name carries to the symbol form (!H = the
|
|
1753
|
+
// ω^4 diagonal), the r0 chain already carries its own mantissa —
|
|
1754
|
+
// keep only the LAST mantissa (issue: "!2.000H1.500E16" had two).
|
|
1755
|
+
let tokAa = letterTokenOf(baseHeight)
|
|
1756
|
+
if (tokAa.sym) {
|
|
1757
|
+
return composeLetterChain(tokAa.sym + tokAa.letter, r0, argStr, precision4)
|
|
1758
|
+
}
|
|
1759
|
+
return composeLetterChain(letter, r0, argStr, precision4)
|
|
1760
|
+
}
|
|
1761
|
+
} else {
|
|
1762
|
+
// r0 is a simple number (length=1 or just r0[0])
|
|
1763
|
+
let base = r0[0]
|
|
1764
|
+
let alpha = Math.pow(10, base - Math.floor(base))
|
|
1765
|
+
if (alpha < 1) alpha *= 10
|
|
1766
|
+
let alphaStr = regularFormat(alpha, precision4)
|
|
1767
|
+
let top = Math.floor(base)
|
|
1768
|
+
let betaStr = formatR0Arg(top, precision4)
|
|
1769
|
+
|
|
1770
|
+
// Collapse threshold: at multiLetterRepeatThreshold+1, advance to next letter
|
|
1771
|
+
let collapseAt = effThreshold + 1
|
|
1772
|
+
|
|
1773
|
+
if (totalAaCount >= collapseAt) {
|
|
1774
|
+
// Collapse to next letter (Aa→Ab, Ab→Ac, etc.): the repeats shift the argument (arg = count+1) and the mantissa keeps the [1,10) convention
|
|
1775
|
+
let arg = totalAaCount + 1
|
|
1776
|
+
let mantVal = Math.log10(Math.max(alpha, 0.001)) +
|
|
1777
|
+
Math.log10(Math.max(arg, 1)) * 0.4 + 1.6
|
|
1778
|
+
let newHeight = baseHeight + 1
|
|
1779
|
+
let newLetter = letterName(newHeight)
|
|
1780
|
+
return regularFormat(mantVal, precision4) + newLetter + formatR0Arg(arg, precision4)
|
|
1781
|
+
} else if (totalAaCount > 1) {
|
|
1782
|
+
// Repeated letters: outerLetters + α + lastLetter + β
|
|
1783
|
+
let outerLetters = letter.repeat(totalAaCount - 1)
|
|
1784
|
+
return outerLetters + alphaStr + letter + betaStr
|
|
1785
|
+
} else {
|
|
1786
|
+
// Single DIAGONAL letter (the row ends in 'a': Aa, Aaa, Ba, …): pure dlsdl convention α=2·5^f ∈ [2,10).
|
|
1787
|
+
let diagMant = 2 * Math.pow(5, base - Math.floor(base))
|
|
1788
|
+
let diagBeta = top
|
|
1789
|
+
// The αAaaβ spelling is only used for integer arguments that stay inside multiLetterLimit (α=2); past the limit the old single-letter carry hard-coded 10 and did not round-trip.
|
|
1790
|
+
let multiLimit = Math.max(2, FORMAT_OPTIONS.multiLetterLimit | 0)
|
|
1791
|
+
if (aaZeroCount >= 2 && (base !== top || aaZeroCount + 1 > multiLimit)) {
|
|
1792
|
+
let argStr = String(base)
|
|
1793
|
+
if (/e-/i.test(argStr)) argStr = base.toFixed(20)
|
|
1794
|
+
argStr = argStr.replace(/e\+?/i, "E")
|
|
1795
|
+
if (aaZeroCount <= MULTI_TOKEN_CAP) return letter + argStr
|
|
1796
|
+
// Hard-cap fallback: preserve the argument but accept the historical single-letter level approximation.
|
|
1797
|
+
return "!" + regularFormat(1, precision4) +
|
|
1798
|
+
letterName(aaZeroCount) + argStr
|
|
1799
|
+
}
|
|
1800
|
+
return regularFormat(diagMant, precision4) + letter + commaFormat(diagBeta)
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
}
|
|
1804
|
+
|
|
1805
|
+
// ── Unnamed-level display ──
|
|
1806
|
+
// The letter grid only names ordinals with coefficients ≤ 25 (top ≤ 26).
|
|
1807
|
+
// When the TOP row's ordinal is above that (ω*2+96, ω*99, ω²*100, …) every row snaps up to the SAME next nameable level, and summing their counts would collapse the letter further.
|
|
1808
|
+
// The value reads as level-arg: 10{ω*2+100}100 → "2.000Ca100", 10{ω*100}100 → "2.000Aaa100", 10{ω²*100+ω*100+100}1000 → "2.000Aaaa1,000".
|
|
1809
|
+
let gapStr = null
|
|
1810
|
+
{
|
|
1811
|
+
let topRow = ordRows[ordRows.length - 1]
|
|
1812
|
+
if (topRow && topRow.length >= 3) {
|
|
1813
|
+
let oc = topRow.slice(1)
|
|
1814
|
+
let carried = false
|
|
1815
|
+
for (let gi = 0; gi < oc.length; gi++) {
|
|
1816
|
+
let gc = oc[gi]
|
|
1817
|
+
let gbound = (gi === oc.length - 1) ? 26 : 25
|
|
1818
|
+
if (typeof gc !== 'number' || !isFinite(gc) || gc < 0 ||
|
|
1819
|
+
Math.round(gc) > gbound) { carried = true; break }
|
|
1820
|
+
}
|
|
1821
|
+
if (carried) {
|
|
1822
|
+
let g = snapCoeffsToGrid(oc)
|
|
1823
|
+
let gdiag = g[g.length - 1]
|
|
1824
|
+
let gvals = g.slice(0, g.length - 1)
|
|
1825
|
+
let gh = 23 + (gdiag - 1) * 26 + (gvals.length ? gvals[0] : 0)
|
|
1826
|
+
if (gvals.length >= 2) {
|
|
1827
|
+
let goffset = 0
|
|
1828
|
+
for (let gk = 2; gk <= gvals.length; gk++) goffset += Math.pow(26, gk)
|
|
1829
|
+
let gn = (gdiag - 1) * Math.pow(26, gvals.length)
|
|
1830
|
+
for (let gj = 0; gj < gvals.length; gj++) gn += gvals[gj] * Math.pow(26, gj)
|
|
1831
|
+
gh = 23 + goffset + gn
|
|
1832
|
+
}
|
|
1833
|
+
// β = the fundamental-sequence index recovering the ordinal from the snapped level. Carry in the TOP coefficient (ω^p·c → ω^(p+1)): the index is c+1
|
|
1834
|
+
let gbeta = (topRow[0] || 1) + 2
|
|
1835
|
+
for (let g2 = oc.length - 1; g2 >= 0; g2--) {
|
|
1836
|
+
let gc2 = Math.round(oc[g2])
|
|
1837
|
+
let gbound = (g2 === oc.length - 1) ? 26 : 25
|
|
1838
|
+
if (isFinite(gc2) && gc2 > gbound) { gbeta = gc2 + 1; break }
|
|
1839
|
+
}
|
|
1840
|
+
let gtok = letterTokenOf(gh)
|
|
1841
|
+
let gbase = r0[0]
|
|
1842
|
+
let galpha = Math.pow(10, gbase - Math.floor(gbase))
|
|
1843
|
+
if (galpha < 1) galpha *= 10
|
|
1844
|
+
if (gtok.sym) {
|
|
1845
|
+
// the carried !αAaβ form: β is the ω-EXPONENT of the snapped level (ω³·50+… → ω⁴ → "!2.000Aa4"), not the fundamental-sequence index — !2.000Aa51 would read as the ω⁵¹ level (README: !αAaβ = ω^β + digits of α)
|
|
1846
|
+
gapStr = emitLetterToken(gtok, regularFormat(gtok.mant, precision4),
|
|
1847
|
+
formatR0Arg(gbeta, precision4), precision4)
|
|
1848
|
+
} else {
|
|
1849
|
+
// a diagonal letter ("Ca", "Aaa", …) carries α = 2·5^f ∈ [2,10) — never the [1,10) mantissa of the other letters
|
|
1850
|
+
gapStr = regularFormat(
|
|
1851
|
+
diagonalMantissa(gtok.letter, Math.log10(galpha)), precision4) +
|
|
1852
|
+
gtok.letter + formatR0Arg(gbeta, precision4)
|
|
1853
|
+
}
|
|
1854
|
+
}
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
if (gapStr !== null) return gapStr
|
|
1858
|
+
|
|
1859
|
+
// ── Check for repeated non-ω letters → advance to next letter ──
|
|
1860
|
+
// AbAb→Ac, AcAc→Ad, ..., AzAz→Ba, BaBa→Bb, etc. Only applies when repeat count >= multiLetterRepeatThreshold
|
|
1861
|
+
let allSameLetter = true
|
|
1862
|
+
let totalRepeat = 0
|
|
1863
|
+
let baseLetterHeight = -1
|
|
1864
|
+
|
|
1865
|
+
for (let i = 0; i < ordRows.length; i++) {
|
|
1866
|
+
let row = ordRows[i]
|
|
1867
|
+
if (row.length < 3) { allSameLetter = false; break }
|
|
1868
|
+
let g = snapCoeffsToGrid(row.slice(1))
|
|
1869
|
+
let diag = g[g.length - 1]
|
|
1870
|
+
let vals = g.slice(0, g.length - 1)
|
|
1871
|
+
let nVals = vals.length
|
|
1872
|
+
|
|
1873
|
+
// Calculate height for this letter
|
|
1874
|
+
let height = 0
|
|
1875
|
+
if (nVals === 1) {
|
|
1876
|
+
// 2-letter: Aa=23, Ab=24, ..., Az=48, Ba=49, ...
|
|
1877
|
+
height = 23 + (diag - 1) * 26 + vals[0]
|
|
1878
|
+
} else {
|
|
1879
|
+
// 3+ letters: need to calculate offset vals are stored in reverse letter order (e.g. "bc" -> [2,1]), so the leftmost letter digit is vals[nVals-1]: n = vals[0]*26^0 + vals[1]*26^1 + ... (little-endian)
|
|
1880
|
+
let offset = 0
|
|
1881
|
+
for (let k = 2; k <= nVals; k++) {
|
|
1882
|
+
offset += Math.pow(26, k)
|
|
1883
|
+
}
|
|
1884
|
+
let n = (diag - 1) * Math.pow(26, nVals)
|
|
1885
|
+
for (let j = 0; j < nVals; j++) {
|
|
1886
|
+
n += vals[j] * Math.pow(26, j)
|
|
1887
|
+
}
|
|
1888
|
+
height = 23 + offset + n
|
|
1889
|
+
}
|
|
1890
|
+
|
|
1891
|
+
if (baseLetterHeight === -1) {
|
|
1892
|
+
baseLetterHeight = height
|
|
1893
|
+
} else if (height !== baseLetterHeight) {
|
|
1894
|
+
allSameLetter = false
|
|
1895
|
+
break
|
|
1896
|
+
}
|
|
1897
|
+
totalRepeat += row[0] || 1
|
|
1898
|
+
}
|
|
1899
|
+
|
|
1900
|
+
if (allSameLetter && totalRepeat > 0 && baseLetterHeight >= 23) {
|
|
1901
|
+
let base = r0[0]
|
|
1902
|
+
let alpha = Math.pow(10, base - Math.floor(base))
|
|
1903
|
+
if (alpha < 1) alpha *= 10
|
|
1904
|
+
let alphaStr = regularFormat(alpha, precision4)
|
|
1905
|
+
let top = Math.floor(base)
|
|
1906
|
+
let betaStr = formatR0Arg(top, precision4)
|
|
1907
|
+
|
|
1908
|
+
// Check if we should advance (repeat threshold)
|
|
1909
|
+
let effThreshold = Math.max(2, FORMAT_OPTIONS.multiLetterRepeatThreshold)
|
|
1910
|
+
let collapseAt = effThreshold + 1
|
|
1911
|
+
|
|
1912
|
+
// Check if r0 has multiple levels (e, f, g, ...)
|
|
1913
|
+
let hasMultiLevelR0 = r0.length > 1
|
|
1914
|
+
|
|
1915
|
+
if (hasMultiLevelR0) {
|
|
1916
|
+
// r0 has E/F/G levels: format as letter(s) + chain (e.g., Ab1.000E500)
|
|
1917
|
+
let r0Str = formatR0AsChain(r0, precision4)
|
|
1918
|
+
let letter = letterName(baseLetterHeight)
|
|
1919
|
+
let tokAdv = letterTokenOf(baseLetterHeight + 1)
|
|
1920
|
+
if (totalRepeat >= collapseAt && tokAdv.sym) {
|
|
1921
|
+
// Collapse to next letter — carried when it exceeds the limit. !αAaβ takes the ω-EXPONENT as β (README: !αAaβ = ω^β + the digits of α), not the r0 chain.
|
|
1922
|
+
return emitLetterToken(tokAdv, regularFormat(tokAdv.mant, precision4),
|
|
1923
|
+
r0Str, precision4)
|
|
1924
|
+
}
|
|
1925
|
+
if (totalRepeat >= collapseAt) {
|
|
1926
|
+
// Collapse to next letter — the repeat count shifts the argument:
|
|
1927
|
+
// the parameter is the integer repeat index and the r0 tower folds
|
|
1928
|
+
// into the mantissa (αΓβ = Γ(β+log α)), never spelling the r0
|
|
1929
|
+
// chain as a second tail: explode(3^^^3,1187)'s row [1186,0,2]
|
|
1930
|
+
// → "6.002Bb1186", not "6.002BbF7.626E12".
|
|
1931
|
+
let mantissa = Math.log10(Math.max(alpha, 0.001)) + totalRepeat
|
|
1932
|
+
let newLetter = letterName(baseLetterHeight + 1)
|
|
1933
|
+
let betaInt = Math.floor(mantissa)
|
|
1934
|
+
return regularFormat(diagonalMantissa(newLetter, mantissa), precision4) +
|
|
1935
|
+
newLetter + commaFormat(betaInt)
|
|
1936
|
+
} else if (totalRepeat > 1) {
|
|
1937
|
+
// Repeated letters + chain: one token per application, the innermost one in front of the chain (Ba(Ba(1e16)) → the stacked "BaBa1.000E16" — apea(apea(a)) must stay visible)
|
|
1938
|
+
let outerLetters = letter.repeat(totalRepeat)
|
|
1939
|
+
return composeLetterChain(outerLetters, r0, r0Str, precision4)
|
|
1940
|
+
} else {
|
|
1941
|
+
// Single letter + chain
|
|
1942
|
+
return composeLetterChain(letter, r0, r0Str, precision4)
|
|
1943
|
+
}
|
|
1944
|
+
}
|
|
1945
|
+
|
|
1946
|
+
// Simple r0 (no E/F/G levels)
|
|
1947
|
+
if (totalRepeat >= collapseAt) {
|
|
1948
|
+
// Advance to next letter with mantissa — carried when the letter exceeds multiLetterLimit (!αAaβ, repeats below display precision)
|
|
1949
|
+
let tokAdv = letterTokenOf(baseLetterHeight + 1)
|
|
1950
|
+
if (tokAdv.sym) {
|
|
1951
|
+
return emitLetterToken(tokAdv, regularFormat(tokAdv.mant, precision4), betaStr, precision4)
|
|
1952
|
+
}
|
|
1953
|
+
let mantissa = Math.log10(Math.max(alpha, 0.001)) + totalRepeat
|
|
1954
|
+
let newHeight = baseLetterHeight + 1
|
|
1955
|
+
let letter = letterName(newHeight)
|
|
1956
|
+
let mantVal = diagonalMantissa(letter, mantissa)
|
|
1957
|
+
return regularFormat(mantVal, precision4) + letter +
|
|
1958
|
+
formatR0Arg(totalRepeat + 1, precision4)
|
|
1959
|
+
} else if (totalRepeat > 1) {
|
|
1960
|
+
// Repeated letters: outerLetters + α + lastLetter + β
|
|
1961
|
+
let letter = letterName(baseLetterHeight)
|
|
1962
|
+
let tokRep = letterTokenOf(baseLetterHeight)
|
|
1963
|
+
if (tokRep.sym) {
|
|
1964
|
+
return emitLetterToken(tokRep, alphaStr, betaStr, precision4)
|
|
1965
|
+
}
|
|
1966
|
+
let outerLetters = letter.repeat(totalRepeat - 1)
|
|
1967
|
+
return outerLetters + alphaStr + letter + betaStr
|
|
1968
|
+
} else {
|
|
1969
|
+
// Single letter. DIAGONAL letters (token ends in 'a': Aa, Ba, Ca, Aaa, …) use the dlsdl [2,10) mantissa 2·5^f with β = base argument (Ba1000 -> 2.000Ba1,000); successor letters (Ab, Bb, … keep the ordinary [1,10) α/β form).
|
|
1970
|
+
let letter = letterName(baseLetterHeight)
|
|
1971
|
+
// multiLetterLimit symbol carry for a single long letter: the diagonal keeps its 2·5^f mantissa, the ordinary letters use the definition digits (!αAaβ), and β becomes the ω-exponent
|
|
1972
|
+
let tokSingle = letterTokenOf(baseLetterHeight)
|
|
1973
|
+
if (tokSingle.sym) {
|
|
1974
|
+
if (letter.length >= 2 && letter[letter.length - 1] === "a") {
|
|
1975
|
+
let diagMant = 2 * Math.pow(5, base - Math.floor(base))
|
|
1976
|
+
return tokSingle.sym + regularFormat(diagMant, precision4) +
|
|
1977
|
+
tokSingle.letter + formatR0Arg(tokSingle.arg, precision4)
|
|
1978
|
+
}
|
|
1979
|
+
return emitLetterToken(tokSingle, alphaStr, betaStr, precision4)
|
|
1980
|
+
}
|
|
1981
|
+
if (letter.length >= 2 && letter[letter.length - 1] === "a") {
|
|
1982
|
+
let diagMant = 2 * Math.pow(5, base - Math.floor(base))
|
|
1983
|
+
return regularFormat(diagMant, precision4) + letter + betaStr
|
|
1984
|
+
}
|
|
1985
|
+
return alphaStr + letter + betaStr
|
|
1986
|
+
}
|
|
1987
|
+
}
|
|
1988
|
+
|
|
1989
|
+
// ── Handle mixed letter types with collapse ──
|
|
1990
|
+
// When ordinal rows have multiple different letter types (e.g., Aa + Ab), collapse lower-level letters to higher-level if count >= threshold
|
|
1991
|
+
{
|
|
1992
|
+
let letterCounts = {} // height -> count
|
|
1993
|
+
let heights = []
|
|
1994
|
+
|
|
1995
|
+
for (let i = 0; i < ordRows.length; i++) {
|
|
1996
|
+
let row = ordRows[i]
|
|
1997
|
+
if (row.length < 2) continue
|
|
1998
|
+
|
|
1999
|
+
// Parse height from row
|
|
2000
|
+
let height = -1
|
|
2001
|
+
if (row.length === 2) {
|
|
2002
|
+
height = 23 // ω-level (Aa)
|
|
2003
|
+
} else {
|
|
2004
|
+
// round the ordinal UP to the next one the letter grid can name (ω*2+96 → ω*3 = Ca, ω*99 → ω² = Aaa, ω²*100 → ω³ = Aaaa)
|
|
2005
|
+
let g2 = snapCoeffsToGrid(row.slice(1))
|
|
2006
|
+
let diag = g2[g2.length - 1]
|
|
2007
|
+
let vals = g2.slice(0, g2.length - 1)
|
|
2008
|
+
let nVals = vals.length
|
|
2009
|
+
if (nVals === 1) {
|
|
2010
|
+
// 2-letter: Aa=23, Ab=24, ..., Az=48, Ba=49, ...
|
|
2011
|
+
height = 23 + (diag - 1) * 26 + vals[0]
|
|
2012
|
+
} else {
|
|
2013
|
+
// vals are the CNF coefficients a1, a2, …, a(x-1) of ord = ω^(x-1)·ax + … + ω·a2 + a1 — little-endian, the same order letterName()/getOrdinalLetter() decode the letter: the LAST val is the first lowercase letter of the token.
|
|
2014
|
+
let offset = 0
|
|
2015
|
+
for (let k = 2; k <= nVals; k++) offset += Math.pow(26, k)
|
|
2016
|
+
let n = (diag - 1) * Math.pow(26, nVals)
|
|
2017
|
+
for (let j = 0; j < nVals; j++) n += vals[j] * Math.pow(26, j)
|
|
2018
|
+
height = 23 + offset + n
|
|
2019
|
+
}
|
|
2020
|
+
}
|
|
2021
|
+
if (height < 0) continue
|
|
2022
|
+
|
|
2023
|
+
let count = row[0] || 1
|
|
2024
|
+
if (!(height in letterCounts)) heights.push(height)
|
|
2025
|
+
letterCounts[height] = (letterCounts[height] || 0) + count
|
|
2026
|
+
}
|
|
2027
|
+
|
|
2028
|
+
if (heights.length >= 1) {
|
|
2029
|
+
// Sort heights ascending
|
|
2030
|
+
heights.sort((a, b) => a - b)
|
|
2031
|
+
|
|
2032
|
+
// README: at most TWO finite letter types survive
|
|
2033
|
+
if (heights.length > 2) {
|
|
2034
|
+
let keepLo = heights[heights.length - 2]
|
|
2035
|
+
for (let i = 0; i < heights.length - 2; i++) delete letterCounts[heights[i]]
|
|
2036
|
+
heights = Object.keys(letterCounts).map(Number).sort((a, b) => a - b)
|
|
2037
|
+
}
|
|
2038
|
+
|
|
2039
|
+
if (heights.length >= 1) {
|
|
2040
|
+
// ── Display rules (README "format-metanum rules") ──
|
|
2041
|
+
let effTh = Math.max(2, FORMAT_OPTIONS.multiLetterRepeatThreshold)
|
|
2042
|
+
let collapseAt = effTh + 1
|
|
2043
|
+
|
|
2044
|
+
let base = r0[0]
|
|
2045
|
+
let r0Alpha = Math.pow(10, base - Math.floor(base))
|
|
2046
|
+
if (r0Alpha < 1) r0Alpha *= 10
|
|
2047
|
+
let frac = base - Math.floor(base)
|
|
2048
|
+
if (frac < 0) frac += 1
|
|
2049
|
+
|
|
2050
|
+
// α of a "+count collapse" (the ω-level form recovers the base mantissa the way the single-row Aa cascade does: 2.398Ab99)
|
|
2051
|
+
let collapseMant = function (h, count) {
|
|
2052
|
+
let arg = count + 1
|
|
2053
|
+
if (h === 23) {
|
|
2054
|
+
return regularFormat(Math.log10(Math.max(r0Alpha, 0.001)) +
|
|
2055
|
+
Math.log10(Math.max(arg, 1)) * 0.4 + 1.6, precision4)
|
|
2056
|
+
}
|
|
2057
|
+
return regularFormat(r0Alpha, precision4)
|
|
2058
|
+
}
|
|
2059
|
+
// one collapsed level: α + next letter + (count+1)
|
|
2060
|
+
let collapseStr = function (h, count) {
|
|
2061
|
+
let tok = letterTokenOf(h + 1)
|
|
2062
|
+
if (tok.sym) {
|
|
2063
|
+
return tok.sym + regularFormat(tok.mant, precision4) +
|
|
2064
|
+
tok.letter + formatR0Arg(count + 1, precision4)
|
|
2065
|
+
}
|
|
2066
|
+
return collapseMant(h, count) + tok.letter + formatR0Arg(count + 1, precision4)
|
|
2067
|
+
}
|
|
2068
|
+
|
|
2069
|
+
let hTop = heights[heights.length - 1]
|
|
2070
|
+
let cTop = letterCounts[hTop] || 1
|
|
2071
|
+
let hLow = heights.length >= 2 ? heights[heights.length - 2] : -1
|
|
2072
|
+
let cLow = hLow < 0 ? 0 : (letterCounts[hLow] || 1)
|
|
2073
|
+
|
|
2074
|
+
// (1) a single row level: collapse, or repeats with α + letter + β
|
|
2075
|
+
if (hLow < 0) {
|
|
2076
|
+
if (cTop >= collapseAt) return collapseStr(hTop, cTop)
|
|
2077
|
+
let tok = letterTokenOf(hTop)
|
|
2078
|
+
if (tok.sym) return emitLetterToken(tok, regularFormat(r0Alpha, precision4),
|
|
2079
|
+
formatR0Arg(Math.floor(base), precision4), precision4)
|
|
2080
|
+
let aStr = (tok.letter.length >= 2 && tok.letter[tok.letter.length - 1] === "a")
|
|
2081
|
+
? regularFormat(2 * Math.pow(5, frac), precision4)
|
|
2082
|
+
: regularFormat(r0Alpha, precision4)
|
|
2083
|
+
return tok.letter.repeat(Math.max(cTop - 1, 0)) + aStr + tok.letter +
|
|
2084
|
+
formatR0Arg(Math.floor(base), precision4)
|
|
2085
|
+
}
|
|
2086
|
+
|
|
2087
|
+
// (2) the inner (second) level carries the single α and β
|
|
2088
|
+
let lowTok = letterTokenOf(hLow)
|
|
2089
|
+
let innerStr
|
|
2090
|
+
if (cLow >= collapseAt) {
|
|
2091
|
+
innerStr = collapseStr(hLow, cLow)
|
|
2092
|
+
} else {
|
|
2093
|
+
let aStr = regularFormat(r0Alpha, precision4)
|
|
2094
|
+
innerStr = lowTok.letter.repeat(Math.max(cLow - 1, 0)) + aStr + lowTok.letter +
|
|
2095
|
+
formatR0Arg(Math.floor(base), precision4)
|
|
2096
|
+
}
|
|
2097
|
+
|
|
2098
|
+
// (3) the top level leads: collapse when it repeats enough, else one token per application (or the symbol-carried !αAaβ)
|
|
2099
|
+
if (cTop >= collapseAt) return collapseStr(hTop, cTop)
|
|
2100
|
+
let topTok = letterTokenOf(hTop)
|
|
2101
|
+
if (topTok.sym) {
|
|
2102
|
+
// multiLetterLimit carry: !αAaβ, β = the ω-exponent; the inner level only survives when it collapses on its own
|
|
2103
|
+
if (cLow >= collapseAt) {
|
|
2104
|
+
return topTok.sym + regularFormat(topTok.mant, precision4) +
|
|
2105
|
+
topTok.letter + collapseStr(hLow, cLow)
|
|
2106
|
+
}
|
|
2107
|
+
return topTok.sym + regularFormat(topTok.mant, precision4) +
|
|
2108
|
+
topTok.letter + formatR0Arg(topTok.arg, precision4)
|
|
2109
|
+
}
|
|
2110
|
+
return topTok.letter.repeat(Math.min(cTop, effTh)) + innerStr
|
|
2111
|
+
}
|
|
2112
|
+
}
|
|
2113
|
+
}
|
|
2114
|
+
|
|
2115
|
+
let letter = getOrdinalLetter(ordRows)
|
|
2116
|
+
if (!letter) {
|
|
2117
|
+
// Fallback: use r0 only
|
|
2118
|
+
let pol = metaPolarize(r0.slice(0))
|
|
2119
|
+
let h = pol.height
|
|
2120
|
+
letter = letterName(Math.max(h, 23))
|
|
2121
|
+
let bottomVal = Math.log10(Math.max(pol.bottom, 1)) + pol.top
|
|
2122
|
+
return regularFormat(bottomVal, precision4) + letter + commaFormat(Math.max(h, 23))
|
|
2123
|
+
}
|
|
2124
|
+
|
|
2125
|
+
// Check if r0 has multiple levels: use formatR0AsChain for the r0 part
|
|
2126
|
+
let hasMultiLevelR0 = r0.length > 1
|
|
2127
|
+
if (hasMultiLevelR0) {
|
|
2128
|
+
// Ordinal letter(s) + r0 chain (which includes its own α)
|
|
2129
|
+
let r0Str = formatR0AsChain(r0, precision4)
|
|
2130
|
+
return composeLetterChain(letter, r0, r0Str, precision4)
|
|
2131
|
+
}
|
|
2132
|
+
|
|
2133
|
+
// Simple r0: format as outerLetters + α + lastLetter + β
|
|
2134
|
+
let base = r0[0]
|
|
2135
|
+
let alpha = Math.pow(10, base - Math.floor(base))
|
|
2136
|
+
if (alpha < 1) alpha *= 10
|
|
2137
|
+
let alphaStr = regularFormat(alpha, precision4)
|
|
2138
|
+
let top = Math.floor(base)
|
|
2139
|
+
let betaStr = formatR0Arg(top, precision4)
|
|
2140
|
+
|
|
2141
|
+
// Split letter string into tokens (each starts with uppercase)
|
|
2142
|
+
let tokens = []
|
|
2143
|
+
let current = ""
|
|
2144
|
+
for (let i = 0; i < letter.length; i++) {
|
|
2145
|
+
let c = letter[i]
|
|
2146
|
+
if (c >= 'A' && c <= 'Z') {
|
|
2147
|
+
if (current.length > 0) tokens.push(current)
|
|
2148
|
+
current = c
|
|
2149
|
+
} else {
|
|
2150
|
+
current += c
|
|
2151
|
+
}
|
|
2152
|
+
}
|
|
2153
|
+
if (current.length > 0) tokens.push(current)
|
|
2154
|
+
|
|
2155
|
+
if (tokens.length <= 1) {
|
|
2156
|
+
// Single letter: α + letter + β
|
|
2157
|
+
return alphaStr + letter + betaStr
|
|
2158
|
+
}
|
|
2159
|
+
|
|
2160
|
+
// Multiple letters: outerLetters + α + lastLetter + β
|
|
2161
|
+
let lastToken = tokens.pop()
|
|
2162
|
+
let outerTokens = tokens.join("")
|
|
2163
|
+
return outerTokens + alphaStr + lastToken + betaStr
|
|
2164
|
+
}
|
|
2165
|
+
|
|
2166
|
+
// ─── Public API ──────────────────────────────────────────────────
|
|
2167
|
+
|
|
2168
|
+
function formatWhole(num) {
|
|
2169
|
+
return format(num, 0)
|
|
2170
|
+
}
|
|
2171
|
+
|
|
2172
|
+
function formatSmall(num, precision=2) {
|
|
2173
|
+
return format(num, precision, true)
|
|
2174
|
+
}
|
|
2175
|
+
|
|
2176
|
+
// ─── Exports ─────────────────────────────────────────────────────
|
|
2177
|
+
|
|
2178
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
2179
|
+
module.exports = { format, formatFull, formatWhole, formatSmall, formatSingleLetterType, metaPolarize, letterName, symbolName, getOrdinalLetter, formatR0AsChain, FORMAT_OPTIONS, polarizeFiniteChain, gammaArgOf, formatGammaBinary }
|
|
2145
2180
|
}
|