metanum 2.0.3 → 2.1.1

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/format-metanum.js CHANGED
@@ -1,5 +1,6 @@
1
- // format-metanum.js by dlsdl
2
- // Adapted from format-omeganum.js by cloudytheconqueror
1
+ // Author: dlsdl v2.1.1
2
+ // v2.1.1 Fix(format): exact token form for non-integer multi-a layers and past multiLetterLimit
3
+ // Code snippets and templates from format-omeganum.js
3
4
  // Uses dlsdl's Letter Notation (see README.md)
4
5
 
5
6
  // Set to 1 to print debug information to console
@@ -23,6 +24,10 @@ const FORMAT_OPTIONS = {
23
24
  epsilonSignificantDigits: 6 //0~9
24
25
  }
25
26
 
27
+ // #28: keep the exact A(a×k) token past multiLetterLimit up to this many a's;
28
+ // beyond it, fall back to the historical single-letter carry approximation.
29
+ const MULTI_TOKEN_CAP = 64
30
+
26
31
  // ─── Utility Functions ───────────────────────────────────────────
27
32
 
28
33
  function commaFormat(num) {
@@ -63,17 +68,10 @@ function regularFormat(num, precision) {
63
68
  }
64
69
 
65
70
  // ─── MetaNum-Specific Polarize ───────────────────────────────────
66
- // MetaNum's r0 is [base, e, f, g, h, ...] where:
67
- // r0[0] = base value (argument to the operations)
68
- // r0[1] = count of E operations (level 1)
69
- // r0[2] = count of F operations (level 2)
70
- // r0[3] = count of G operations (level 3), etc.
71
- //
72
- // In binary form αΓβ:
73
- // bottom = α (mantissa in [1, 10))
74
- // top = β (integer exponent)
75
- // height = Γ level (1=E, 2=F, 3=G, ..., 22=Z)
76
- // repeat = count of repeated operations at the highest level (>1 means repeated letters)
71
+ // r0 is [base, e, f, g, ...]: r0[0] is the argument, r0[1..] count the E/F/G…
72
+ // operations (levels 1, 2, 3, …). Binary form αΓβ: bottom = α (mantissa in
73
+ // [1,10)), top = β, height = Γ (1=E … 22=Z), repeat = count of repeated
74
+ // operations at the highest level (>1 means repeated letters).
77
75
  function metaPolarize(r0, num) {
78
76
  if (FORMAT_DEBUG >= 1) console.log("metaPolarize input:", JSON.stringify(r0))
79
77
  if (r0.length === 0) r0 = [0]
@@ -289,25 +287,20 @@ function letterName(height) {
289
287
  }
290
288
 
291
289
  // ─── Letter token with the multiLetterLimit symbol carry ──────────
292
- // A k-letter combination (Aa, Aaa, Aaaa, …) sits at level ω^(k-1). Once the
293
- // combination would be longer than FORMAT_OPTIONS.multiLetterLimit the
294
- // notation switches to the next one instead: the symbol form !αAaβ. Per its
295
- // definition (!1.2345Aa4 = 10{ω^4+ω^3*2+ω^2*3+ω*4+5}10) β is the top
296
- // ω-exponent (k-1) and α's digits are the CNF coefficients — exactly the
297
- // digits of the letter itself (uppercase index, then the lowercase indices),
298
- // which keeps the diagonal mantissa inside [2,10) for the cascades.
290
+ // A k-letter combination (Aa, Aaa, …) sits at level ω^(k-1). Past
291
+ // FORMAT_OPTIONS.multiLetterLimit the notation switches to the symbol form
292
+ // !αAaβ: β is the top ω-exponent (k-1), α's digits are the CNF coefficients
293
+ // (the letter's own digits), keeping the diagonal mantissa inside [2,10).
299
294
  function letterTokenOf(height) {
300
295
  let plain = letterName(height)
301
296
  let limit = Math.max(2, FORMAT_OPTIONS.multiLetterLimit | 0)
302
297
  if (plain.length <= limit) return { sym: "", letter: plain, arg: 0, mant: null }
303
- // The carried form is "!" + the SINGLE-letter form αΓβ: "!" adds one ω^
304
- // layer on top of the level Γ names, so a diagonal Aaa…a (= ω^k, too long
305
- // for multiLetterLimit) becomes !αHβ with H = the finite level k —
306
- // !2.000Aa4 = !1H10 = 10{ω^4}10, 2.000Aa4 = 1H10 = 10{4}10
307
- // and β is the argument, which keeps ω³*50 / ω³*100 / ω³*200 apart
308
- // (!1H51 / !1H101 / !1H201). Only once the single-letter chain itself is
309
- // exhausted (!ZZZYYY{maxSafeInteger}) does the notation fall back to the
310
- // coarser !αAaβ form.
298
+ // The carried form is "!" + the single-letter αΓβ: "!" adds one ω^ layer
299
+ // on top of Γ, so a diagonal Aaa…a (= ω^k, too long for multiLetterLimit)
300
+ // becomes !αHβ with H = the finite level k (!2.000Aa4 = !1H10 = 10{ω^4}10
301
+ // vs 2.000Aa4 = 1H10 = 10{4}10); β is the argument (ω³*50/100/200 stay
302
+ // apart as !1H51/!1H101/!1H201). Only when the single-letter chain is
303
+ // exhausted does it fall back to the coarser !αAaβ form.
311
304
  let expo = plain.length - 1
312
305
  if (expo >= 1 && expo <= 22) {
313
306
  return { sym: "!", letter: letterName(expo), arg: expo, mant: 1, useBeta: true }
@@ -343,22 +336,15 @@ function symbolName(layer) {
343
336
  }
344
337
 
345
338
  // ─── Ordinal Letter: Extract letter name from ordinal rows ───────
346
- // Two formats for ordinal rows:
347
- // Format A (3+ element): [count, v1, v2, ..., vN, diag]
348
- // diag = 1-26 (A-Z), the uppercase letter
349
- // v1..vN = 0-25 (a-z), the lowercase letters
350
- // count = repetition count
351
- // Format B (2-element): [count, value]
352
- // count = repetition count at ω-level
353
- // value = ordinal level number (e.g., 901 means ω*901)
354
- // Letter is always "Aa" (base ω-level)
355
-
356
- // Snap CNF coefficients (little-endian [a1, a2, …, ak], ordinal
357
- // ω^(k-1)·ak + … + ω·a2 + a1) to the ones the letter grid can name:
358
- // every ai must satisfy 0 ≤ ai ≤ 25 and the top ak ≤ 26. Larger
359
- // coefficients carry UP — ω*2+96 → ω*3 (Ca), ω*99 → ω² (Aaa),
360
- // ω²*100 → ω³ (Aaaa) — so an unnamed ordinal displays as the next
361
- // nameable one above it, never as a lower/wrong letter.
339
+ // Format A (3+ elements): [count, v1..vN, diag] — diag 1-26 (A-Z, uppercase),
340
+ // v1..vN 0-25 (a-z, lowercase), count = repetition count.
341
+ // Format B (2 elements): [count, value] — count at ω-level, value the
342
+ // ordinal level number (901 means ω*901); letter is always "Aa".
343
+
344
+ // Snap CNF coefficients (little-endian [a1..ak], ordinal ω^(k-1)·ak+…+a1) to
345
+ // the ones the letter grid can name: 0 ≤ ai ≤ 25, top ak ≤ 26. Larger
346
+ // coefficients carry UP (ω*2+96 → ω*3 (Ca), ω*99 → ω² (Aaa), ω²*100 → ω³
347
+ // (Aaaa)), so an unnamed ordinal displays as the next nameable one above it.
362
348
  function snapCoeffsToGrid(coeffs) {
363
349
  let r = (coeffs || []).slice()
364
350
  // little-endian ORDINAL carry: index 0 is the constant term. A term above
@@ -420,11 +406,10 @@ function getOrdinalLetter(ordRows) {
420
406
 
421
407
  // ─── Reference Polarize (verbatim port of PowiainaNum's myPolarize) ──
422
408
  // Recovers the display α ("bottom") and the diagonal level ("arrows") of an
423
- // r0 chain STRUCTURALLY, by climbing log10 level by level — the only method
424
- // that stays exact at high levels where the engine's own fractional-arg
425
- // bisect quantizes (level ≥ 20). Ground truth: format-powiainanum.js.
426
- // their array = [base, [level, count, 1, 1], ...] sorted top-level-first
427
- // ours: r0 = [base, cnt1, cnt2, ...] where cnt_i = count at level i
409
+ // r0 chain STRUCTURALLY, climbing log10 level by level — the only method that
410
+ // stays exact at high levels where the engine's fractional-arg bisect
411
+ // quantizes (level ≥ 20). Ground truth: format-powiainanum.js. Their array
412
+ // [base, [level, count, 1, 1], …] maps to our r0 = [base, cnt1, cnt2, …].
428
413
  function refPolarizeArraySort(array) {
429
414
  array.sort(function (a, b) {
430
415
  if (typeof a == 'number') return
@@ -544,21 +529,16 @@ function refPolarize(array, hasOperationRepeat = true) {
544
529
  }
545
530
  }
546
531
  // Convert a finite-chain polarize triple (bottom, repeation, arrows) into the
547
- // dlsdl diagonal-letter display pair (mantissa, β) for a diagonal letter
548
- // (Aa, Ba, Ca, Aaa, … — multi-letter tokens ending in 'a').
549
- // The value behaves as 10{arrows+1}(t) with t = log10(bottom)+repeation.
550
- // Pure dlsdl convention for diagonal letters (α = 2·5^f ∈ [2,10)):
551
- // t ∈ [2,10) -> mantissa t, β = arrows
552
- // (3{100}3 -> 2.376Aa99; 10{100}(10{93}10)-> 2.002Aa100)
553
- // t ≥ 10 -> the count climbs one level per geometric smooth-log step
554
- // f → 1+log10(f); the exact anchor t=10 is mantissa 2 at
555
- // β=arrows+1 (10{100}10 -> 2.000Aa100, Aa100 itself)
556
- // t ∈ [1,2) -> below the β anchor: mantissa 2·5^(t-1), β=arrows-1
557
- // Truncation-marker normalization for finite rows: when the engine truncated
558
- // an overflow cascade it reset r0 to [10] and bumped the LOWEST kept row's
559
- // count by 1 over the uniform per-level fill (xCnt). That +1 marks the
560
- // dropped tail, not a real operation — read the row as carrying xCnt so
561
- // truncated displays match the untruncated anchor (cross-config stability).
532
+ // diagonal-letter display pair (mantissa, β). The value behaves as
533
+ // 10{arrows+1}(t), t = log10(bottom)+repeation; the diagonal convention is
534
+ // α = 2·5^f ∈ [2,10):
535
+ // t ∈ [2,10) -> mantissa t, β = arrows (3{100}3 -> 2.376Aa99)
536
+ // t ≥ 10 -> one level per smooth-log step f → 1+log10(f); the anchor
537
+ // t=10 is mantissa 2 at β=arrows+1 (10{100}10 -> 2.000Aa100)
538
+ // t ∈ [1,2) -> mantissa 2·5^(t-1), β = arrows-1
539
+ // Truncation marker: a truncated cascade resets r0 to [10] and bumps the
540
+ // lowest kept row's count by 1 over the uniform fill (xCnt) — read it as xCnt
541
+ // so truncated displays match the untruncated anchor.
562
542
  function normalizedFiniteRows(r0, finiteRows) {
563
543
  if (!finiteRows || finiteRows.length === 0 || r0[0] !== 10) return finiteRows
564
544
  let sorted = finiteRows.slice().sort((a, b) => a[1] - b[1])
@@ -630,6 +610,57 @@ function formatR0ArgStr(argStr) {
630
610
  return null
631
611
  }
632
612
 
613
+ // De-layered !Aa x instance (decision 1B): the shape [[x],[1,0×k,1]] with
614
+ // k = max(1, floor(x)), x >= 1. Per the README the display is the APPROXIMATE
615
+ // binary form !α'Aa'β' (β' = floor(x), α' = 2·5^(x−β') ∈ [2,10)) — the
616
+ // mantissa carries only FORMAT_OPTIONS digits, so the round-trip recovers x
617
+ // to that precision (issue #20: tolerance-based assertion). An INTEGER x has
618
+ // α' = 2 exactly, and the binary spelling now denotes the ω^n diagonal
619
+ // cascade (issue #19), so it shows the exact letter token A(a×k)x instead.
620
+ // Returns null for every other shape (x < 2 included — the no-"!" binary form
621
+ // comes from formatOrdinal).
622
+ function formatBangAaValue(num, precision) {
623
+ if (num.sign !== 1 || num.layer !== 0) return null
624
+ let a = num.array
625
+ if (a.length !== 2 || a[0].length !== 1) return null
626
+ let x = a[0][0]
627
+ if (typeof x !== 'number' || !isFinite(x) || x < 2) return null
628
+ let row = a[1]
629
+ if (row.length < 3 || row[0] !== 1 || row[row.length - 1] !== 1) return null
630
+ for (let i = 1; i < row.length - 1; i++) if (row[i] !== 0) return null
631
+ let k = row.length - 2
632
+ if (k !== Math.max(1, Math.floor(x))) return null
633
+ if (x === Math.floor(x)) {
634
+ // integer anchor: the letter token A(a×k)x is the exact spelling
635
+ return "A" + "a".repeat(k) + formatR0Arg(x, precision)
636
+ }
637
+ let beta = Math.floor(x)
638
+ let mant = 2 * Math.pow(5, x - beta)
639
+ return "!" + regularFormat(mant, precision) + "Aa" + formatR0Arg(beta, precision)
640
+ }
641
+
642
+ // The full rule-3 cascade of 10{ω^n}10 (n ≥ 2) — the stored form of !Aa<n>
643
+ // (issue #19: "!" adds one ω^, so !Aa3 = 10{ω^3}10). A cheap shape pre-filter
644
+ // (base [10], full row budget, top row [8, 9×n]) plus an exact comparison
645
+ // against the engine's own cascade, so nothing else matches. Displayed as the
646
+ // binary diagonal !2.000Aa<n>, which parses back to the same cascade.
647
+ function formatAaCascade(num, precision) {
648
+ if (num.sign !== 1 || num.layer !== 0) return null
649
+ let a = num.array
650
+ if (a.length !== MetaNum.maxRows || a.length < 3) return null
651
+ if (a[0].length !== 1 || a[0][0] !== 10) return null
652
+ let last = a[a.length - 1]
653
+ let n = last.length - 1
654
+ if (n < 2 || last[0] !== 8) return null
655
+ for (let i = 1; i < last.length; i++) if (last[i] !== 9) return null
656
+ let coeffs = []
657
+ for (let i = 0; i < n; i++) coeffs.push(0)
658
+ coeffs.push(1)
659
+ let ref = MetaNum._hyperopFromOrdinalRaw(MetaNum.TEN, MetaNum.TEN, coeffs)
660
+ if (!ref.eq(num)) return null
661
+ return "!" + regularFormat(2, precision) + "Aa" + formatR0Arg(n, precision)
662
+ }
663
+
633
664
  function format(num, precision=3, small=false) {
634
665
  if (MetaNum.isNaN(num)) return "NaN"
635
666
  let sciSigDigits = FORMAT_OPTIONS.sciSignificantDigits // for E notation
@@ -645,14 +676,11 @@ function format(num, precision=3, small=false) {
645
676
  if (num.sign < 0) return "-" + format(num.neg(), precision, small)
646
677
  if (num.isInfinite()) return "Infinity"
647
678
 
648
- // Sub-unit values (sign 2 / -2, the whole 0 < |v| < 1 range): a plain
649
- // finite r0 stores the RECIPROCAL, so the real value is 1/r0 — format that
650
- // number and let the normal machinery below render it (decimal places at or
651
- // above 10^-smallNotationThreshold, αE-β below it). Reading the raw r0 as
652
- // the value printed the reciprocal instead: MetaNum(1).div(2000)
653
- // (r0 = [2000], value 5e-4) came out as "2.000E-3" instead of "5.000E-4".
654
- // Structured sign-2 forms (layer > 0, ordinal rows, power-tower r0) keep the
655
- // letter-law path further down.
679
+ // Sub-unit values (sign 2 / -2, 0 < |v| < 1): a plain finite r0 stores the
680
+ // RECIPROCAL, so the real value is 1/r0 — format that number (decimals at
681
+ // or above 10^-smallNotationThreshold, αE-β below). Reading the raw r0
682
+ // printed the reciprocal (MetaNum(1).div(2000) → "2.000E-3"). Structured
683
+ // sign-2 forms keep the letter-law path below.
656
684
  if ((num.sign === 2 || num.sign === -2) && num.layer === 0 &&
657
685
  num.array.length === 1 && num.array[0].length === 1 &&
658
686
  isFinite(num.array[0][0]) && num.array[0][0] !== 0) {
@@ -688,16 +716,12 @@ function format(num, precision=3, small=false) {
688
716
  return prefix + regularFormat(mant, sciMantissaPrecision(expPart)) + "E-" + commaFormat(expPart)
689
717
  }
690
718
  }
691
- // Tier 2: mag is large → "E-" + letter-law form of mag =
692
- // log10 of the reciprocal base. Letter law (README): a chain
693
- // of n same-level ops collapses to ONE next-level op with
694
- // arg+n, so log10 shifts only the OUTERMOST letter:
695
- // E-chain (EE1000): log10 = E1000 (one E stripped)
696
- // F-chain (F500): log10 = F499 (arg −1, same letter)
697
- // Γ ≥ G (G200): log10 = G200 (Γβ−1+1 = Γβ: the leading
698
- // F(G(β−1)) collapses back to G(β))
699
- // Roundtrip exact: "E-1.000G200" parses back to 1/G200,
700
- // "E-1.000F499" to 1/F500 (test_mixed format block)
719
+ // Tier 2: mag is large → "E-" + the letter-law form of mag
720
+ // (= log10 of the reciprocal base). A chain of n same-level
721
+ // ops collapses to ONE next-level op with arg+n, so log10
722
+ // shifts only the OUTERMOST letter (EE1000 → E1000, F500 →
723
+ // F499, G200 → G200). Roundtrip exact: "E-1.000G200" parses
724
+ // back to 1/G200.
701
725
  var recipStr = format(recipFmt, precision, small)
702
726
  var magStr = recipStr
703
727
  // leading run of E letters (e.g. "EE1.000E1,000"): strip one E
@@ -795,6 +819,12 @@ function format(num, precision=3, small=false) {
795
819
  // ── Handle ordinal rows (Aa, Ab, ..., Aaa, ... range) ──
796
820
  let hasOrdinalRows = array.length > 1
797
821
  if (hasOrdinalRows) {
822
+ let bang = formatBangAaValue(num, multiPrecision)
823
+ if (bang !== null) return bang
824
+ // issue #19: the full 10{ω^n}10 cascade (stored !Aa<n>) — display the
825
+ // binary diagonal so the parser round-trips it
826
+ let cascade = formatAaCascade(num, multiPrecision)
827
+ if (cascade !== null) return cascade
798
828
  return formatOrdinal(num, precision, multiPrecision)
799
829
  }
800
830
 
@@ -887,39 +917,47 @@ function formatLayer(num, precision, precision2, precision3, precision4) {
887
917
  return innerStr + "ε" + commaFormat(num.layer)
888
918
  }
889
919
 
920
+ // issue #19: the 10{ω^n}10 cascade at a symbol layer — the stored form of
921
+ // @Aa<n> (layer 1), #Aa<n> (layer 2), … ONE symbol per value: the symbol
922
+ // index is the total ω^-layer count (layer 0 → "!", 1 → "@", 2 → "#"),
923
+ // never a stack ("@!…" is invalid). The body is the same binary diagonal
924
+ // the layer-0 form uses, so the display stays isomorphic to !Aa<n>.
925
+ let cascadeStr = formatAaCascade(inner, precision4)
926
+ if (cascadeStr !== null) {
927
+ let body = cascadeStr.slice(1) // strip the layer-0 "!"
928
+ if (num.layer < SYMBOLS.length) return SYMBOLS[num.layer] + body
929
+ return body + "ε" + commaFormat(num.layer + 1)
930
+ }
931
+
890
932
  // Compact diagonal: the symbol row [1,0…0,1] with m=compactBase zeros is
891
- // 10{ω^m}10 — a DIAGONAL letter (A + m a's) at argument 10. Diagonal
892
- // letter combinations use the pure dlsdl α=2·5^f ∈ [2,10) convention:
893
- // integer m anchors mantissa 2. For layers above ! the symbol of the
894
- // layer below prefixes the same inner form:
895
- // !Aa3 = 2.000Aaaa10, @Aa3 = !2.000Aaaa10.
896
- // multiLetterLimit symbol carry: once the diagonal letter would be longer
897
- // than the limit (m+1 letters) it switches to one more symbol and the Aa
898
- // argument takes over the exponent — 10{ω^10}10 → !2.000Aa10, and layer 1
899
- // on top stacks the symbols (no-symbol → ! → @ → # …):
900
- // apix(3,10) = 10{ω^(ω^10)}10 → @2.000Aa10.
933
+ // 10{ω^m}10 — a DIAGONAL letter (A + m a's) at argument 10, with the pure
934
+ // dlsdl α=2·5^f ∈ [2,10) convention (integer m anchors mantissa 2). For
935
+ // layers above ! the symbol of the layer below prefixes the same inner
936
+ // form (!Aa3 = 2.000Aaaa10, @Aa3 = !2.000Aaaa10); past multiLetterLimit the
937
+ // Aa argument takes over the exponent (10{ω^10}10 → !2.000Aa10).
901
938
  if (canCompact && compactBase >= 2) {
902
939
  let mInt = Math.floor(compactBase)
903
940
  let frac = compactBase - mInt
904
941
  let mant = 2 * Math.pow(5, frac)
905
- let limit = Math.max(2, FORMAT_OPTIONS.multiLetterLimit | 0)
906
- if (mInt + 1 > limit) {
907
- // Carry to the SINGLE-letter form sym αΓβ — same rule as "!":
908
- // Γ is the finite level the diagonal names (10{ω^m}10 → letter of
909
- // m) and β is the diagonal's argument, so the symbols stack
910
- // identically: 10{ω^10}10 → !1.000N10, 10{ω^(ω^10)}10 → @1.000N10
911
- let total = num.layer + 1
912
- let body = regularFormat(1, precision4) + letterName(mInt) +
913
- formatR0Arg(10, precision4)
914
- if (total <= SYMBOLS.length) return SYMBOLS[total - 1] + body
915
- return body + "ε" + commaFormat(total)
942
+ // #28: past multiLetterLimit the old single-letter carry (!1.000N10)
943
+ // did not round-trip — it added a layer and could not name the exact
944
+ // diagonal. Keep the exact A(a×m)10 token while the string stays
945
+ // reasonable; only fall back to the carry past a hard safety cap.
946
+ if (mInt <= MULTI_TOKEN_CAP) {
947
+ // Stored layer L: exactly ONE symbol per ω^ layer (symbolName(L));
948
+ // stacking symbols ("@!…") is invalid (issue #19). The body is
949
+ // the diagonal letter A(a×m)10 at the smooth mantissa. (Layers
950
+ // past the symbol list already returned through the ε branch.)
951
+ return symbolName(num.layer) + regularFormat(mant, precision4) +
952
+ "A" + "a".repeat(mInt) + "10"
916
953
  }
917
- // Stored layer L (the parser de-layers small !Aa rows):
918
- // L=0 (!Aa3) -> no prefix; L=1 (@Aa3) -> "!"; L=2 (#Aa3) -> "@!"
919
- let prefixSyms = ""
920
- for (let sl = num.layer; sl >= 1; sl--) prefixSyms += symbolName(sl)
921
- return prefixSyms + regularFormat(mant, precision4) +
922
- "A" + "a".repeat(mInt) + "10"
954
+ // Hard-cap fallback: keep the argument (10 for a compact diagonal) but
955
+ // the level is the historical single-letter carry approximation.
956
+ let total = num.layer + 1
957
+ let body = regularFormat(1, precision4) + letterName(mInt) +
958
+ formatR0Arg(10, precision4)
959
+ if (total <= SYMBOLS.length) return SYMBOLS[total - 1] + body
960
+ return body + "ε" + commaFormat(total)
923
961
  }
924
962
 
925
963
  // For !, @, #, $, %, &, ~, <, >, ? symbols: prefix the symbol. When the
@@ -972,10 +1010,9 @@ function formatR0Arg(value, precision) {
972
1010
 
973
1011
  // ─── Gamma-Arg Recovery ─────────────────────────────────────────
974
1012
  // Recover the real argument x with 10{level}x = v (the Γ-canonical argument of
975
- // v at letter `level`), by bisection on the engine's own smooth arrow curve.
976
- // Returns null when x exceeds the double-precision bisect range. The doubling
977
- // guard runs to 200: E-notation arguments (hardy(1120)'s F-arg ≈ 4.4e13) need
978
- // ~45 doublings before the bracket closes.
1013
+ // v at letter `level`) by bisection on the engine's smooth arrow curve. null
1014
+ // when x exceeds the double-precision bisect range; the doubling guard runs to
1015
+ // 200 (hardy(1120)'s F-arg ≈ 4.4e13 needs ~45 doublings).
979
1016
  function gammaArgOf(v, level, iters) {
980
1017
  if (iters === undefined) iters = 60
981
1018
  let G = function (x) { return MetaNum(10).arrow(level)(x) }
@@ -1034,14 +1071,11 @@ function distinctLetterTypes(str) {
1034
1071
  }
1035
1072
 
1036
1073
  // ─── Format r0 as Chain ────────────────────────────────────────
1037
- // Formats r0 = [base, e, f, g, ...] as a dlsdl letter-notation chain.
1038
- // Format pattern: outerLetters + α + lastLetter + β
1039
- // where α = 10^(base - floor(base)), β = floor(base) formatted as number or αEβ
1040
- // e.g. EE200 → "E1.000E200", F300 → "1.000F300", FE400 → "F1.000E400"
1041
- // Mantissa of a binary form αΓβ. A letter whose name ends in "a" is a
1042
- // diagonal letter (Aa = ω, Ba = ω·2, Aaa = ω², …) and its binary form has
1043
- // α = 2·5^f ∈ [2,10) — the diagonal 10{23}10 reads "2.000Aa23", never
1044
- // "1.000Aa23". Every other letter keeps the ordinary [1,10) mantissa.
1074
+ // Formats r0 = [base, e, f, g, ...] as a dlsdl chain: outerLetters + α +
1075
+ // lastLetter + β, with α = 10^(base-floor(base)) and β = floor(base)
1076
+ // (EE200 → "E1.000E200", FE400 → "F1.000E400"). A letter ending in "a" is a
1077
+ // diagonal letter (Aa = ω, Ba = ω·2, Aaa = ω², …) and its binary form uses
1078
+ // α = 2·5^f ∈ [2,10) — 10{23}10 reads "2.000Aa23", never "1.000Aa23".
1045
1079
  function diagonalMantissa(letter, mantissa) {
1046
1080
  if (letter && letter.charAt(letter.length - 1) === 'a') {
1047
1081
  let f = mantissa - Math.floor(mantissa)
@@ -1084,17 +1118,14 @@ function formatR0AsChain(r0, precision, used) {
1084
1118
  }
1085
1119
 
1086
1120
  // ── ≤2-letter-types rule ──
1087
- // When the full structural spelling would use MORE than two letter
1088
- // types, spell only the top two levels' letters and compress everything
1089
- // below into the second letter's Binary-Canonical form
1090
- // "bottom Γ₂ repeation" — the polarize triple of the chain below the
1091
- // top letter (bisect fails there: the arg exceeds double range):
1092
- // 3{9}3 → "L2.376K2" (user: "L…K…")
1093
- // 3{10}3 → "M2.376L2" (user: "M(10^0.376)L2")
1094
- // 4{9}4 → "LLK3.550K3" (user: "LLK…K…")
1095
- // 5{9}5 → "LLLKK4.669K4" (user: "LLLKK…K…")
1096
- // A top count ≥ repeatLetterThreshold+1 carries to the next single
1097
- // letter instead (6{9}6 → "5.760M5" via the collapse paths below).
1121
+ // When the full spelling would use MORE than two letter types, spell only
1122
+ // the top two levels' letters and compress everything below into the
1123
+ // second letter's binary-canonical form "bottom Γ₂ repeation" (the
1124
+ // polarize triple; bisect fails there — the arg exceeds double range):
1125
+ // 3{9}3 → "L2.376K2", 3{10}3 → "M2.376L2",
1126
+ // 4{9}4 → "LLK3.550K3", 5{9}5 → "LLLKK4.669K4".
1127
+ // A top count ≥ repeatLetterThreshold+1 carries to the next single letter
1128
+ // (6{9}6 → "5.760M5").
1098
1129
  if (maxLevel >= 2 && maxLevel <= 22 && count <= effThreshold) {
1099
1130
  let spellTypes = 0
1100
1131
  for (let s = 2; s <= maxLevel; s++) {
@@ -1209,17 +1240,14 @@ function formatR0AsChain(r0, precision, used) {
1209
1240
  }
1210
1241
 
1211
1242
  // ── Γ-canonical recovery (2-letter rule) ──
1212
- // Two rules, both driven by the engine's own smooth arrow curve:
1213
1243
  // (a) structural top count < threshold AND inner top < maxLevel: keep
1214
- // the structural top letter Γᵀ bare and format the inner chain
1215
- // recursively — at most 2 letter types appear (2-letter rule:
1216
- // VαEβ → VαFβ → … → VαVβ, never a mixed "VFαEβ").
1244
+ // the top letter Γᵀ bare and format the inner chain recursively (at
1245
+ // most 2 letter types: VαEβ → VαFβ → … → VαVβ, never "VFαEβ").
1217
1246
  // (b) otherwise: collapse to the single letter Γ = the HIGHEST level L
1218
- // whose recovered argument x (bisect 10{L}x = v) is ≥ 2, in binary
1219
- // form αΓβ (2-letter rule: hardy(4166) → "2.397G5", arrow(3,4.3,3) →
1220
- // "1.285H8", arrow(3,4.1,3) → "G1.161G897").
1221
- // Verified laws: n ops of level L on a Γ^{L+1}-structured value collapse
1222
- // to ONE Γ^{L+1} with arg+n (F^4(G(1.3796)) = G(5.3796) — bisect-exact).
1247
+ // whose recovered argument (bisect 10{L}x = v) is ≥ 2, in binary form
1248
+ // αΓβ (hardy(4166) → "2.397G5", arrow(3,4.3,3) → "1.285H8").
1249
+ // Verified law: n ops of level L on a Γ^{L+1}-structured value collapse to
1250
+ // ONE Γ^{L+1} with arg+n (F^4(G(1.3796)) = G(5.3796)).
1223
1251
  let r0Num = new MetaNum({ sign: 1, layer: 0, array: [r0.slice(0)] })
1224
1252
 
1225
1253
  if (count < effThreshold && maxLevel >= 2 && runLen <= effThreshold) {
@@ -1248,12 +1276,11 @@ function formatR0AsChain(r0, precision, used) {
1248
1276
  if (innerR0[it] > 0) { innerTop = it; break }
1249
1277
  }
1250
1278
  if (innerTop < maxLevel) {
1251
- // ≤2 letter types rule: count the distinct letter
1252
- // types the peeled prefix + innerStr would use. If the inner chain
1253
- // spells a THIRD type, collapse the inner to the binary form at
1254
- // maxLevel-1 instead — VαEβ → VαFβ → … → VαVβ, never a mixed
1255
- // "VFαEβ" (e.g. arrow(3,9,3) → "L2.376K2": the MLKJIHG F-chain
1256
- // spells only L and K, the M..J part collapses into K's arg).
1279
+ // ≤2 letter types rule: count the distinct letter types the
1280
+ // peeled prefix + innerStr would use. If the inner chain spells a
1281
+ // THIRD type, collapse it to the binary form at maxLevel-1 —
1282
+ // VαEβ → VαFβ → … → VαVβ, never a mixed "VFαEβ" (arrow(3,9,3) →
1283
+ // "L2.376K2": the M..J part collapses into K's arg).
1257
1284
  let topLetter = letterName(maxLevel)
1258
1285
  let innerTypes = distinctLetterTypes(innerStr)
1259
1286
  if (innerTypes.length >= 2
@@ -1290,12 +1317,11 @@ function formatR0AsChain(r0, precision, used) {
1290
1317
  }
1291
1318
  }
1292
1319
 
1293
- // (b) collapse: the structural chain (n repeats of the top letter Γᵀ on
1294
- // an inner value whose own canonical letter is Γᵀ⁺¹, e.g. F^895(G(2.06)))
1295
- // compresses to ONE Γᵀ⁺¹ with arg+n — bisect at the STRUCTURAL next
1296
- // letter first (maxLevel+1); if its arg falls below 2, search upward for
1297
- // the highest level whose arg ≥ 2 (2-letter rule: hardy(4166) → "2.397G5",
1298
- // arrow(3,4.3,3) → "1.285H8")
1320
+ // (b) collapse: the structural chain (n repeats of the top letter Γᵀ on an
1321
+ // inner value whose canonical letter is Γᵀ⁺¹, e.g. F^895(G(2.06)))
1322
+ // compresses to ONE Γᵀ⁺¹ with arg+n — bisect at the structural next letter
1323
+ // first (maxLevel+1); if its arg falls below 2, search upward for the
1324
+ // highest level whose arg ≥ 2 (hardy(4166) → "2.397G5").
1299
1325
  let structuralNext = Math.min(maxLevel + 1, 22)
1300
1326
  let sx = gammaArgOf(r0Num, structuralNext, 40)
1301
1327
  if (sx !== null && isFinite(sx) && sx >= 2) {
@@ -1334,12 +1360,11 @@ function formatR0AsChain(r0, precision, used) {
1334
1360
  // bare letter + canonical argument (single α at the innermost value)
1335
1361
  return newLetter + formatR0Arg(newTop + Math.log10(bottom < 1 ? Math.pow(10, bottom) : bottom), precision)
1336
1362
  }
1337
- // Format: α + letter + β. Canonical all-8s arrays (built from the
1338
- // 10^10 base: G600, ZZ10, ...) carry no recoverable mantissa below
1339
- // 10^10, so α = 1.000 by construction. Every other value recovers
1340
- // its real α by stripping newTop slog's — the residue lands in
1341
- // [1,10) and is exactly the α of the binary form αΓβ = Γ(β+log α)
1342
- // (issues.md: hardy(4150) → "2.300G5", not "1.000G5")
1363
+ // Format: α + letter + β. Canonical all-8s arrays (from the 10^10
1364
+ // base: G600, ZZ10, …) carry no recoverable mantissa below 10^10, so
1365
+ // α = 1.000 by construction; every other value recovers its real α by
1366
+ // stripping newTop slog's — the residue in [1,10) is exactly the α of
1367
+ // αΓβ = Γ(β+log α) (hardy(4150) → "2.300G5").
1343
1368
  let alpha = 1.0
1344
1369
  let all8s = (r0[0] === 10000000000)
1345
1370
  for (let ai = 2; ai < pol.height && all8s; ai++) {
@@ -1406,15 +1431,14 @@ function formatR0AsChain(r0, precision, used) {
1406
1431
 
1407
1432
  // ─── Format with Ordinal Rows ────────────────────────────────────
1408
1433
 
1409
- // finite-row chain display (≤2-letter-types rule):
1410
- // r0 cols + [count, level] rows form one hyperoperation cascade. Display:
1411
- // top level ≥ 23 (Aa region): the αAaβ diagonal — (bottom, repeation,
1412
- // arrows) from polarizeFiniteChain, mantissa normalized into [1,10)
1413
- // carrying decades into β: 3{24}3 → "2.376Aa23", 3{100}3 → "2.376Aa99",
1414
- // 10{10000}10 → "1.000Aa10,000" (bottom 1, repeation 10 carries).
1434
+ // finite-row chain display (≤2-letter-types rule): r0 cols + [count, level]
1435
+ // rows form one hyperoperation cascade.
1436
+ // top level ≥ 23 (Aa region): the αAaβ diagonal from polarizeFiniteChain,
1437
+ // mantissa normalized into [1,10) carrying decades into β:
1438
+ // 3{24}3 → "2.376Aa23", 3{100}3 → "2.376Aa99", 10{10000}10 → "1.000Aa10,000".
1415
1439
  // top level ≤ 22 (single letters): the structural two-letter form
1416
1440
  // Γ_top×c₁ + Γ_second×(c₂−1) + bottom + Γ_second + repeation:
1417
- // 3{22}3 → "Y2.376X2", 3{23}3 → "Z2.376Y2" (user: "Y…X…", "Z…Y…").
1441
+ // 3{22}3 → "Y2.376X2", 3{23}3 → "Z2.376Y2".
1418
1442
  // c₁ ≥ repeatLetterThreshold+1 carries to the next single letter.
1419
1443
  function formatFiniteRowChain(r0, finiteRows, precision) {
1420
1444
  function levelCount(lv) {
@@ -1495,6 +1519,25 @@ function formatFiniteRowChain(r0, finiteRows, precision) {
1495
1519
  }
1496
1520
  }
1497
1521
 
1522
+ // ── Repeated top letter over a dominant lower level ──
1523
+ // When the top row is only a prefix (count ≤ threshold) but a lower row
1524
+ // holds the real argument, polarizeFiniteChain collapses the chain and the
1525
+ // top argument degenerates (ZZ152416 read "1.000Z3"). Spell the top row as
1526
+ // prefix letters and format the remaining chain on its own, keeping the
1527
+ // real argument (ZZ152416 → "Z1.000Z152,416").
1528
+ let topRow = finiteRows[finiteRows.length - 1]
1529
+ if (finiteRows.length >= 2 && topRow && topRow[1] === topLv &&
1530
+ Number.isInteger(topRow[0]) && topRow[0] >= 1 && topRow[0] <= effThreshold) {
1531
+ let restR0 = r0.slice(0)
1532
+ for (let i = 0; i < finiteRows.length - 1; i++) {
1533
+ let r = finiteRows[i]
1534
+ while (restR0.length <= r[1]) restR0.push(0)
1535
+ restR0[r[1]] = r[0]
1536
+ }
1537
+ let innerStr = formatR0AsChain(restR0, precision)
1538
+ if (innerStr) return letterName(topLv).repeat(topRow[0]) + innerStr
1539
+ }
1540
+
1498
1541
  // fallback: single binary at the top letter
1499
1542
  let polF = polarizeFiniteChain(r0, finiteRows)
1500
1543
  if (polF && isFinite(polF.bottom) && polF.bottom >= 1) {
@@ -1546,13 +1589,9 @@ function formatOrdinal(num, precision, precision4) {
1546
1589
  totalAaCount += row[0] || 1
1547
1590
  }
1548
1591
  if (allAaOnly && totalAaCount > 0) {
1549
- // Build the correct letter: "A" + "a".repeat(aaZeroCount) for Aa, Aaa, Aaaa, etc.
1550
- // For ordinal rows: diag=1->"A", vals=[0]->"a", so letter = A + a*n = Aa(n+1 letters)
1551
- // aaZeroCount = number of lowercase a's (vals length)
1552
- // 2-letter (Aa): aaZeroCount=1, height=23
1553
- // 3-letter (Aaa): aaZeroCount=2, height=699
1554
- // 4-letter (Aaaa): aaZeroCount=3, height=...
1555
- // Calculate the correct baseHeight for this letter:
1592
+ // Build the letter "A" + "a".repeat(aaZeroCount) (Aa, Aaa, Aaaa, …):
1593
+ // for ordinal rows diag=1 → "A" and each val 0 → "a", so
1594
+ // aaZeroCount = vals length (Aa → 1, height 23; Aaa → 2, height 699).
1556
1595
  let baseHeight
1557
1596
  if (aaZeroCount === 1) {
1558
1597
  baseHeight = 23 // Aa
@@ -1650,20 +1689,26 @@ function formatOrdinal(num, precision, precision4) {
1650
1689
  } else {
1651
1690
  // Single DIAGONAL letter (the row ends in 'a': Aa, Aaa,
1652
1691
  // Ba, …): pure dlsdl convention α=2·5^f ∈ [2,10).
1653
- // - symbol-de-layered row (base == #zeros, e.g. !Aa3): the
1654
- // base is the LETTER INDEX, argument is fixed 10
1655
- // (!Aa3 -> 2.000Aaaa10)
1656
- // - ordinary letter application: base is the argument β
1657
- // (Aa100 -> 2.000Aa100)
1692
+ // Decision 1B: the base is the ARGUMENT β (the row zero count
1693
+ // encodes the letter level). !Aa3 = Aaaa3 therefore reads
1694
+ // "2.000Aaaa3" — not the historical anchor-A "2.000Aaaa10".
1658
1695
  let diagMant = 2 * Math.pow(5, base - Math.floor(base))
1659
- let diagBeta = (base === aaZeroCount) ? 10 : top
1660
- // multiLetterLimit symbol carry: A + aaZeroCount a's is
1661
- // aaZeroCount+1 letters — past the limit the diagonal reads as
1662
- // one more symbol with the SINGLE letter of the finite level
1663
- // aaZeroCount (10{ω^10}10 → !1.000N10)
1664
- if (aaZeroCount + 1 > Math.max(2, FORMAT_OPTIONS.multiLetterLimit | 0)) {
1696
+ let diagBeta = top
1697
+ // #26/#28: a single multi-a diagonal marker round-trips exactly
1698
+ // as the letter token itself (Aaa1.5, Aaaaa3.5, Aaaaa10). The
1699
+ // αAaaβ spelling is only used for integer arguments that stay
1700
+ // inside multiLetterLimit (α=2); past the limit the old
1701
+ // single-letter carry hard-coded 10 and did not round-trip.
1702
+ let multiLimit = Math.max(2, FORMAT_OPTIONS.multiLetterLimit | 0)
1703
+ if (aaZeroCount >= 2 && (base !== top || aaZeroCount + 1 > multiLimit)) {
1704
+ let argStr = String(base)
1705
+ if (/e-/i.test(argStr)) argStr = base.toFixed(20)
1706
+ argStr = argStr.replace(/e\+?/i, "E")
1707
+ if (aaZeroCount <= MULTI_TOKEN_CAP) return letter + argStr
1708
+ // Hard-cap fallback: preserve the argument but accept the
1709
+ // historical single-letter level approximation.
1665
1710
  return "!" + regularFormat(1, precision4) +
1666
- letterName(aaZeroCount) + formatR0Arg(10, precision4)
1711
+ letterName(aaZeroCount) + argStr
1667
1712
  }
1668
1713
  return regularFormat(diagMant, precision4) + letter + commaFormat(diagBeta)
1669
1714
  }
@@ -1672,11 +1717,10 @@ function formatOrdinal(num, precision, precision4) {
1672
1717
 
1673
1718
  // ── Unnamed-level display (v2.0.2) ──
1674
1719
  // The letter grid only names ordinals with coefficients ≤ 25 (top ≤ 26).
1675
- // When the TOP row's ordinal is above that (ω*2+96, ω*99, ω²*100, …), every
1676
- // row of the cascade snaps up to the SAME next nameable level, and summing
1677
- // their counts would collapse the letter further (243 Ca's → Cb(244)) —
1678
- // overstating the level and hiding the argument. Instead the value reads
1679
- // as level-arg: the snapped level with the operation's own argument —
1720
+ // When the TOP row's ordinal is above that (ω*2+96, ω*99, ω²*100, …) every
1721
+ // row snaps up to the SAME next nameable level, and summing their counts
1722
+ // would collapse the letter further (243 Ca's → Cb(244)) — overstating the
1723
+ // level and hiding the argument. Instead the value reads as level-arg:
1680
1724
  // 10{ω*2+100}100 → "1.000Ca100", 10{ω*100}100 → "1.000Aaa100",
1681
1725
  // 10{ω²*100+ω*100+100}1000 → "1.000Aaaa1,000".
1682
1726
  let gapStr = null
@@ -1703,15 +1747,15 @@ function formatOrdinal(num, precision, precision4) {
1703
1747
  for (let gj = 0; gj < gvals.length; gj++) gn += gvals[gj] * Math.pow(26, gj)
1704
1748
  gh = 23 + goffset + gn
1705
1749
  }
1706
- // β = the fundamental-sequence index that recovers the ordinal
1707
- // from the snapped level. When the carry is in the TOP
1708
- // coefficient (ω^p·c → ω^(p+1)) that index is c+1:
1750
+ // β = the fundamental-sequence index recovering the ordinal
1751
+ // from the snapped level. Carry in the TOP coefficient
1752
+ // (ω^p·c → ω^(p+1)): the index is c+1 —
1709
1753
  // 10{ω²·50+ω·100+100}100 → "2.000Aaaa51"
1710
1754
  // 10{ω²·100+ω·100+100}100 → "2.000Aaaa101"
1711
1755
  // 10{ω²·200+…}100 → "2.000Aaaa201"
1712
- // (taking the largest coefficient instead collapsed all three
1713
- // of 50/60/100 into one string). When the carry is BELOW the
1714
- // top (ω·2+100 → ω·3) the index is the operation's argument.
1756
+ // (the largest coefficient collapsed all three into one
1757
+ // string). Carry BELOW the top (ω·2+100 → ω·3): the index is
1758
+ // the operation's argument.
1715
1759
  let gbeta = (topRow[0] || 1) + 2
1716
1760
  for (let g2 = oc.length - 1; g2 >= 0; g2--) {
1717
1761
  let gc2 = Math.round(oc[g2])
@@ -1946,15 +1990,14 @@ function formatOrdinal(num, precision, precision4) {
1946
1990
 
1947
1991
  if (heights.length >= 1) {
1948
1992
  // ── Display rules (README "format-metanum rules (v2.0)") ──
1949
- // * the rows are a COMPOSITION: the top row is the outermost
1950
- // operation, so it leads the display and the row below it
1951
- // supplies the single α and β (at most TWO letter types —
1952
- // every lower row compresses into that inner argument);
1993
+ // * the rows are a COMPOSITION: the top row leads and the row
1994
+ // below supplies the single α and β (at most TWO letter
1995
+ // types — lower rows compress into that inner argument);
1953
1996
  // * "+count collapse": count >= multiLetterRepeatThreshold+1
1954
- // repeats of a letter collapse to ONE next letter with
1955
- // arg count+1 (F⁴(G(1.3796)) = G(5.3796));
1956
- // * stacking the same function therefore nests the letter:
1957
- // poea(10,poea(10,100)) = Ad(Ad(99)) → "Ad1.000Ad99";
1997
+ // repeats collapse to ONE next letter with arg count+1
1998
+ // (F⁴(G(1.3796)) = G(5.3796));
1999
+ // * stacking the same function nests the letter:
2000
+ // poea(10,poea(10,100)) → "Ad1.000Ad99";
1958
2001
  // * a combination longer than multiLetterLimit carries to the
1959
2002
  // symbol notation !αAaβ.
1960
2003
  let effTh = Math.max(2, FORMAT_OPTIONS.multiLetterRepeatThreshold)