g_math 0.4.2

Multi-domain fixed-point arithmetic with geometric extension: Lie groups, manifolds, ODE solvers, tensors, fiber bundles — zero-float, 0 ULP transcendentals
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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
1044
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
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
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
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
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
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
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
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
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
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
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
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
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
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
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
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
//! # RationalNumber: Tiered Rational Arithmetic
//!
//! Exact rational arithmetic (a/b) with a 7-tier storage hierarchy that automatically
//! selects the smallest integer type for each value's magnitude:
//! Tiny(i8) → Small(i16) → Medium(i32) → Large(i64) → Huge(i128) → Massive(I256) → Ultra(I512).
//!
//! Overflow-safe: all arithmetic returns `Result<T, OverflowDetected>` for UGOD tier promotion.
//! Thread-safe: immutable operations, deterministic across platforms.
//! Profile-aware: deployment profiles constrain maximum tier for precision-performance tradeoffs.

use crate::deployment_profiles::DeploymentProfile;
#[cfg(feature = "infinite-precision")]
use num_bigint::BigInt;
#[cfg(feature = "infinite-precision")]
use num_traits::{Zero, One, ToPrimitive};
use crate::fixed_point::universal::ugod::{UniversalTieredArithmetic, DomainType as UGODDomainType};

use std::fmt;
use std::cmp::Ordering;

// Domain metadata types (DomainType, ExactnessType, ShadowMetadata)
use crate::fixed_point::core_types::domain_metadata::{
    ShadowMetadata,
    DomainType,
    ExactnessType,
};


// Canonical binary_fixed integer types
// Eliminates duplicate I256/I512 definitions that caused 198 E0034 name collision errors
use crate::fixed_point::domains::binary_fixed::{I256, I512, I1024};

// Symbolic arithmetic delegation imports
use crate::fixed_point::symbolic::symbolic_arithmetic::{
    // Basic rational operations for all tiers
    mul_i8_rational, mul_i16_rational, mul_i32_rational, mul_i64_rational, mul_i128_rational,
    add_i8_rational, add_i16_rational, add_i32_rational, add_i64_rational, add_i128_rational,
    sub_i8_rational, sub_i16_rational, sub_i32_rational, sub_i64_rational, sub_i128_rational,
    div_i8_rational, div_i16_rational, div_i32_rational, div_i64_rational, div_i128_rational,
    // GCD for UGOD tier promotion fallback (pure integer, no heap allocation)
    gcd_unsigned,
};

/// Ultra-Compact RationalNumber with integrated shadow intelligence
/// 
/// MEMORY PROFILE: 6-60 bytes (10x improvement over legacy 48+ bytes)
/// PERFORMANCE: Sub-100ns operation latency with AVX2 SIMD acceleration  
/// PRECISION: Automatic tier promotion with deployment profile constraints
#[derive(Debug, Clone)]
pub struct RationalNumber {
    /// Tiered storage: 2-48 bytes depending on precision requirements
    storage: RationalStorage,
    /// Ultra-compact shadow metadata: 4 bytes (bitmap-optimized)
    metadata: ShadowMetadata,
}

/// 7-Tier Storage Hierarchy with AVX2 SIMD Coordination
/// 
/// Each tier optimized for specific value ranges with SIMD batch processing:
/// - Memory efficiency: Common fractions use minimal storage
/// - SIMD batching: AVX2 registers process multiple values simultaneously
/// - Overflow detection: Automatic promotion to higher tiers
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RationalStorage {
    /// Tier 1: 99.9% of simple fractions (1/2, 1/3, 3/4, etc.)
    /// Memory: 2 bytes | SIMD: 16× batching in single AVX2 register
    Tiny { num: i8, den: u8 },
    
    /// Tier 2: Mathematical constants (355/113 for π, 1597/987 for φ, etc.)
    /// Memory: 4 bytes | SIMD: 8× batching in single AVX2 register
    Small { num: i16, den: u16 },
    
    /// Tier 3: Common calculations (currency, percentages, etc.)
    /// Memory: 8 bytes | SIMD: 4× batching in single AVX2 register
    Medium { num: i32, den: u32 },
    
    /// Tier 4: Extended precision (scientific computing)
    /// Memory: 16 bytes | SIMD: 2× batching in single AVX2 register
    Large { num: i64, den: u64 },
    
    /// Tier 5: Maximum standard precision (SIMD computational limit)
    /// Memory: 32 bytes | SIMD: 1× value per AVX2 register
    Huge { num: i128, den: u128 },
    
    /// Tier 6: Extreme precision (scientific applications)
    /// Memory: 64 bytes | SIMD: Custom 2× AVX2 registers per value
    #[cfg(not(feature = "embedded"))]
    Massive { num: I256, den: I256 },
    
    /// Tier 7: I512 numerator/denominator
    /// Memory: 128 bytes | Processing: Scalar operations (exceeds AVX2 capacity)
    Ultra { num: I512, den: I512 },
    
    /// Tier 8: BigInt numerator/denominator (requires `infinite-precision` feature)
    /// Memory: Variable | Processing: Scalar operations only
    #[cfg(feature = "infinite-precision")]
    Infinite { num: BigInt, den: BigInt },
}


// ============================================================================
// CANONICAL ERROR TYPE - Now unified in core_types/errors.rs
// ============================================================================

/// Re-export canonical OverflowDetected from core_types
///
/// **UNIFIED**: Single error type shared across all arithmetic domains
/// **LOCATION**: Defined in crate::fixed_point::core_types::errors
pub use crate::fixed_point::core_types::errors::OverflowDetected;

/// Tier-preserving rational parts extraction
///
/// Preserves native tier types without over-promotion.
///
/// This enum provides access to numerator/denominator in their native storage types,
/// enabling tier-specific arithmetic without unnecessary type widening.
///
/// **MOTIVATION**: The legacy `extract_as_i128()` method promotes all tiers to i128,
/// which violates tier preservation and doesn't work for I256/I512 tiers. This enum
/// preserves the exact tier information.
///
/// **USAGE PATTERNS**:
/// - Same-tier arithmetic: Extract native types, use tier-specific operations
/// - Mixed-tier arithmetic: Promote to common tier intelligently
/// - Cross-domain conversion: Handle each tier with appropriate conversion logic
///
/// **PERFORMANCE**: Zero-copy extraction for tier-native operations
/// **CORRECTNESS**: Prevents accidental precision loss from over-promotion
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RationalParts {
    /// Tier 1: i8 numerator, u8 denominator
    Tiny(i8, u8),
    /// Tier 2: i16 numerator, u16 denominator
    Small(i16, u16),
    /// Tier 3: i32 numerator, u32 denominator
    Medium(i32, u32),
    /// Tier 4: i64 numerator, u64 denominator
    Large(i64, u64),
    /// Tier 5: i128 numerator, u128 denominator
    Huge(i128, u128),
    /// Tier 6: I256 numerator and denominator
    #[cfg(not(feature = "embedded"))]
    Massive(I256, I256),
    /// Tier 7: I512 numerator and denominator
    Ultra(I512, I512),
    /// Tier 8: BigInt numerator and denominator
    #[cfg(feature = "infinite-precision")]
    Infinite(BigInt, BigInt),
}

impl RationalParts {
    /// Get the tier level of these rational parts (1-8)
    pub fn tier_level(&self) -> u8 {
        match self {
            RationalParts::Tiny(..) => 1,
            RationalParts::Small(..) => 2,
            RationalParts::Medium(..) => 3,
            RationalParts::Large(..) => 4,
            RationalParts::Huge(..) => 5,
            #[cfg(not(feature = "embedded"))]
            RationalParts::Massive(..) => 6,
            RationalParts::Ultra(..) => 7,
            #[cfg(feature = "infinite-precision")]
            RationalParts::Infinite(..) => 8,
        }
    }

    /// Promote to i128 if possible, returning None for tiers that don't fit
    ///
    /// **COMPATIBILITY**: Provides the same functionality as the old extract_as_i128()
    /// but makes the tier limitation explicit in the return type.
    pub fn try_as_i128(&self) -> Option<(i128, u128)> {
        match self {
            RationalParts::Tiny(n, d) => Some((*n as i128, *d as u128)),
            RationalParts::Small(n, d) => Some((*n as i128, *d as u128)),
            RationalParts::Medium(n, d) => Some((*n as i128, *d as u128)),
            RationalParts::Large(n, d) => Some((*n as i128, *d as u128)),
            RationalParts::Huge(n, d) => Some((*n, *d)),
            #[cfg(not(feature = "embedded"))]
            RationalParts::Massive(n, d) => {
                // Try to downcast I256 to i128/u128
                if n.fits_in_i128() && d.fits_in_i128() {
                    Some((n.as_i128(), d.as_i128() as u128))
                } else {
                    None
                }
            },
            RationalParts::Ultra(..) => None, // Too large for i128
            #[cfg(feature = "infinite-precision")]
            RationalParts::Infinite(n, d) => {
                // Try to convert BigInt to i128/u128
                match (n.to_i128(), d.to_u128()) {
                    (Some(n_i128), Some(d_u128)) => Some((n_i128, d_u128)),
                    _ => None,
                }
            },
        }
    }

    /// Promote to I256 pair if possible (tiers 1-6)
    ///
    /// Widens smaller tiers to I256. Returns None for tiers 7+ (too large for I256).
    #[cfg(not(feature = "embedded"))]
    pub fn try_as_i256_pair(&self) -> Option<(I256, I256)> {
        match self {
            RationalParts::Tiny(n, d) => Some((I256::from_i128(*n as i128), I256::from_i128(*d as i128))),
            RationalParts::Small(n, d) => Some((I256::from_i128(*n as i128), I256::from_i128(*d as i128))),
            RationalParts::Medium(n, d) => Some((I256::from_i128(*n as i128), I256::from_i128(*d as i128))),
            RationalParts::Large(n, d) => Some((I256::from_i128(*n as i128), I256::from_i128(*d as i128))),
            RationalParts::Huge(n, d) => Some((I256::from_i128(*n), I256::from_i128(*d as i128))),
            #[cfg(not(feature = "embedded"))]
            RationalParts::Massive(n, d) => Some((*n, *d)),
            RationalParts::Ultra(..) => None, // Too large for I256
            #[cfg(feature = "infinite-precision")]
            RationalParts::Infinite(..) => None,
        }
    }

    /// Promote to I512 pair if possible (tiers 1-7)
    ///
    /// Widens smaller tiers to I512. Returns None for tier 8 (BigInt).
    pub fn try_as_i512_pair(&self) -> Option<(I512, I512)> {
        match self {
            RationalParts::Tiny(n, d) => Some((I512::from_i128(*n as i128), I512::from_i128(*d as i128))),
            RationalParts::Small(n, d) => Some((I512::from_i128(*n as i128), I512::from_i128(*d as i128))),
            RationalParts::Medium(n, d) => Some((I512::from_i128(*n as i128), I512::from_i128(*d as i128))),
            RationalParts::Large(n, d) => Some((I512::from_i128(*n as i128), I512::from_i128(*d as i128))),
            RationalParts::Huge(n, d) => Some((I512::from_i128(*n), I512::from_i128(*d as i128))),
            #[cfg(not(feature = "embedded"))]
            RationalParts::Massive(n, d) => Some((I512::from_i256(*n), I512::from_i256(*d))),
            RationalParts::Ultra(n, d) => Some((*n, *d)),
            #[cfg(feature = "infinite-precision")]
            RationalParts::Infinite(..) => None, // BigInt cannot fit in I512
        }
    }
}

// ================================================================================================
// IMPLEMENTATION: RationalStorage Operations
// ================================================================================================

impl RationalStorage {
    /// Get maximum tier level available in current compilation profile
    #[allow(unreachable_code)]
    pub fn max_tier_for_profile() -> u8 {
        #[cfg(feature = "infinite-precision")] { return 8; }
        #[cfg(not(feature = "infinite-precision"))] { return 7; }
        7 // Default to Ultra tier (I512)
    }
    
    /// Get memory footprint in bytes
    pub fn memory_size(&self) -> usize {
        match self {
            RationalStorage::Tiny { .. }     => 2,   // i8 + u8
            RationalStorage::Small { .. }    => 4,   // i16 + u16
            RationalStorage::Medium { .. }   => 8,   // i32 + u32
            RationalStorage::Large { .. }    => 16,  // i64 + u64
            RationalStorage::Huge { .. }     => 32,  // i128 + u128
            #[cfg(not(feature = "embedded"))]
            RationalStorage::Massive { .. }  => 64,  // I256 + I256
            RationalStorage::Ultra { .. }    => 128, // I512 + I512
            #[cfg(feature = "infinite-precision")]
            RationalStorage::Infinite { .. } => 48,  // Approximate BigInt minimum
        }
    }
    
    /// Get storage tier level (1-8, depending on compilation profile)
    pub fn tier_level(&self) -> u8 {
        match self {
            RationalStorage::Tiny { .. }     => 1,
            RationalStorage::Small { .. }    => 2,
            RationalStorage::Medium { .. }   => 3,
            RationalStorage::Large { .. }    => 4,
            RationalStorage::Huge { .. }     => 5,
            #[cfg(not(feature = "embedded"))]
            RationalStorage::Massive { .. }  => 6,
            RationalStorage::Ultra { .. }    => 7,
            #[cfg(feature = "infinite-precision")]
            RationalStorage::Infinite { .. } => 8,
        }
    }
    
}

// ================================================================================================  
// IMPLEMENTATION: RationalNumber Core Operations
// ================================================================================================

impl RationalNumber {
    /// Create new ultra-compact rational number with automatic tier selection
    pub fn new(numerator: i128, denominator: u128) -> Self {
        assert_ne!(denominator, 0, "RationalNumber: Denominator cannot be zero");

        let storage = Self::optimal_storage_tier(numerator, denominator);
        let metadata = Self::analyze_metadata(numerator, denominator);

        Self { storage, metadata }
    }

    /// Create from I256 numerator/denominator pair (Tier 6: Massive)
    ///
    /// **USE CASE**: Balanced/Scientific profiles (38 decimal precision)
    /// **PRECISION**: 10^38 denominators, ~126 bits of precision
    /// **FEATURE GATE**: Only available for non-embedded profiles
    #[cfg(not(feature = "embedded"))]
    pub fn from_i256_pair(numerator: I256, denominator: I256) -> Self {
        // Verify denominator is not zero
        assert!(!denominator.is_zero(), "RationalNumber: Denominator cannot be zero");

        let storage = RationalStorage::Massive {
            num: numerator,
            den: denominator,
        };

        // Set metadata for high-precision constants
        use crate::fixed_point::core_types::domain_metadata::{ShadowMetadata, DomainType, ExactnessType};
        let metadata = ShadowMetadata::new(DomainType::Symbolic, ExactnessType::Exact);

        Self { storage, metadata }
    }

    /// Create from I512 numerator/denominator pair (Tier 7: Ultra)
    ///
    /// **USE CASE**: Scientific profile (77 decimal precision)
    /// **PRECISION**: 10^77 denominators, ~256 bits of precision
    /// **AVAILABILITY**: Always available (unconditional)
    pub fn from_i512_pair(numerator: I512, denominator: I512) -> Self {
        // Verify denominator is not zero
        assert!(!denominator.is_zero(), "RationalNumber: Denominator cannot be zero");

        let storage = RationalStorage::Ultra {
            num: numerator,
            den: denominator,
        };

        // Set metadata for wide-integer constants
        use crate::fixed_point::core_types::domain_metadata::{ShadowMetadata, DomainType, ExactnessType};
        let metadata = ShadowMetadata::new(DomainType::Symbolic, ExactnessType::Exact);

        Self { storage, metadata }
    }

    /// Select optimal storage tier based on value magnitude
    fn optimal_storage_tier(numerator: i128, denominator: u128) -> RationalStorage {
        let max_val = numerator.abs().max(denominator as i128);
        
        // Apply deployment profile constraints
        let max_tier = RationalStorage::max_tier_for_profile();
        
        let optimal_tier = match max_val {
            0..=127 if max_tier >= 1 => {
                RationalStorage::Tiny {
                    num: numerator as i8,
                    den: denominator as u8
                }
            },
            128..=32767 if max_tier >= 2 => {
                RationalStorage::Small {
                    num: numerator as i16,
                    den: denominator as u16
                }
            },
            32768..=2147483647 if max_tier >= 3 => {
                RationalStorage::Medium {
                    num: numerator as i32,
                    den: denominator as u32
                }
            },
            2147483648..=9223372036854775807 if max_tier >= 4 => {
                RationalStorage::Large {
                    num: numerator as i64,
                    den: denominator as u64
                }
            },
            _ if max_tier >= 5 => {
                RationalStorage::Huge {
                    num: numerator,
                    den: denominator
                }
            },
            #[cfg(feature = "infinite-precision")]
            _ => {
                // Fallback to BigInt for values exceeding I512 range
                RationalStorage::Infinite {
                    num: BigInt::from(numerator),
                    den: BigInt::from(denominator),
                }
            },
            #[cfg(not(feature = "infinite-precision"))]
            _ => {
                // Input is (i128, u128) — always fits in Huge tier regardless of profile
                RationalStorage::Huge {
                    num: numerator,
                    den: denominator,
                }
            }
        };

        // Denominator is always positive (u128), no sign normalization needed
        optimal_tier
    }


    /// Analyze precision metadata for new value
    fn analyze_metadata(_numerator: i128, _denominator: u128) -> ShadowMetadata {
        // For new exact values, create minimal metadata
        // Note: ShadowMetadata::new() simplified to (domain, exactness) signature
        ShadowMetadata::new(
            DomainType::Symbolic,
            ExactnessType::Exact,
        )
    }
    


    /// Get storage tier level
    pub fn tier_level(&self) -> u8 {
        self.storage.tier_level()
    }

    /// Map symbolic internal tier (1-8) to universal tier (1-6+)
    ///
    /// Universal tier mapping:
    ///   Symbolic 1-2 (Tiny/Small, i8-i16)  → Universal 1 (fits in i32)
    ///   Symbolic 3   (Medium, i32)          → Universal 2 (fits in i64)
    ///   Symbolic 4   (Large, i64)           → Universal 3 (fits in i128)
    ///   Symbolic 5   (Huge, i128)           → Universal 4 (fits in I256 conceptually)
    ///   Symbolic 6   (Massive, I256)        → Universal 5
    ///   Symbolic 7   (Ultra, I512)          → Universal 6
    ///   Symbolic 8   (BigInt)               → Universal 6 (capped)
    pub fn universal_tier(&self) -> u8 {
        symbolic_to_universal_tier(self.tier_level())
    }

    /// Create RationalNumber from i128 ratio
    pub fn from_ratio(numerator: i128, denominator: u128) -> Self {
        Self::new(numerator, denominator)
    }

    /// Create RationalNumber from BigInt ratio (helper for reconstruction)
    #[cfg(feature = "infinite-precision")]
    pub fn from_bigint_ratio(numerator: BigInt, denominator: BigInt) -> Self {
        Self {
            storage: RationalStorage::Infinite {
                num: numerator,
                den: denominator
            },
            metadata: ShadowMetadata::new(DomainType::Symbolic, ExactnessType::Exact),
        }
    }

    // from_bigint_ratio() only exists with infinite-precision feature
    // For tiers 1-7, use new(i128, i128) directly

    /// Get numerator as i128 (RECOMMENDED for tiers 1-7)
    ///
    /// Returns None if value exceeds i128 range (tier 6-8 with large values).
    /// For tier 8 (BigInt), use `numerator_bigint()` instead.
    pub fn numerator_i128(&self) -> Option<i128> {
        match &self.storage {
            RationalStorage::Tiny { num, .. } => Some(*num as i128),
            RationalStorage::Small { num, .. } => Some(*num as i128),
            RationalStorage::Medium { num, .. } => Some(*num as i128),
            RationalStorage::Large { num, .. } => Some(*num as i128),
            RationalStorage::Huge { num, .. } => Some(*num),
            #[cfg(not(feature = "embedded"))]
            RationalStorage::Massive { num, .. } => {
                if num.fits_in_i128() { Some(num.as_i128()) } else { None }
            },
            RationalStorage::Ultra { num, .. } => {
                if num.fits_in_i128() { Some(num.as_i128()) } else { None }
            },
            #[cfg(feature = "infinite-precision")]
            RationalStorage::Infinite { num, .. } => num.to_i128(),
        }
    }

    /// Get denominator as i128 (RECOMMENDED for tiers 1-7)
    ///
    /// Returns None if value exceeds i128 range (tier 6-8 with large values).
    /// For tier 8 (BigInt), use `denominator_bigint()` instead.
    pub fn denominator_i128(&self) -> Option<i128> {
        match &self.storage {
            RationalStorage::Tiny { den, .. } => Some(*den as i128),
            RationalStorage::Small { den, .. } => Some(*den as i128),
            RationalStorage::Medium { den, .. } => Some(*den as i128),
            RationalStorage::Large { den, .. } => Some(*den as i128),
            RationalStorage::Huge { den, .. } => Some(*den as i128),
            #[cfg(not(feature = "embedded"))]
            RationalStorage::Massive { den, .. } => {
                if den.fits_in_i128() { Some(den.as_i128()) } else { None }
            },
            RationalStorage::Ultra { den, .. } => {
                if den.fits_in_i128() { Some(den.as_i128()) } else { None }
            },
            #[cfg(feature = "infinite-precision")]
            RationalStorage::Infinite { den, .. } => den.to_i128(),
        }
    }

    /// Get numerator as BigInt. Prefer `numerator_i128()` for tiers 1-7.
    #[cfg(feature = "infinite-precision")]
    pub fn numerator(&self) -> BigInt {
        match &self.storage {
            RationalStorage::Tiny { num, .. } => BigInt::from(*num),
            RationalStorage::Small { num, .. } => BigInt::from(*num),
            RationalStorage::Medium { num, .. } => BigInt::from(*num),
            RationalStorage::Large { num, .. } => BigInt::from(*num),
            RationalStorage::Huge { num, .. } => BigInt::from(*num),
            #[cfg(not(feature = "embedded"))]
            RationalStorage::Massive { num, .. } => BigInt::from(num.as_i128()),
            RationalStorage::Ultra { num, .. } => BigInt::from(num.as_i128()),
            RationalStorage::Infinite { num, .. } => num.clone(),
        }
    }

    /// Get denominator as BigInt. Prefer `denominator_i128()` for tiers 1-7.
    #[cfg(feature = "infinite-precision")]
    pub fn denominator(&self) -> BigInt {
        match &self.storage {
            RationalStorage::Tiny { den, .. } => BigInt::from(*den),
            RationalStorage::Small { den, .. } => BigInt::from(*den),
            RationalStorage::Medium { den, .. } => BigInt::from(*den),
            RationalStorage::Large { den, .. } => BigInt::from(*den),
            RationalStorage::Huge { den, .. } => BigInt::from(*den),
            #[cfg(not(feature = "embedded"))]
            RationalStorage::Massive { den, .. } => BigInt::from(den.as_i128()),
            RationalStorage::Ultra { den, .. } => BigInt::from(den.as_i128()),
            RationalStorage::Infinite { den, .. } => den.clone(),
        }
    }

    // numerator() and denominator() only exist with infinite-precision feature
    // For tiers 1-7, use numerator_i128() and denominator_i128() instead

    /// Extract numerator and denominator in their native tier types (TIER-PRESERVING)
    ///
    /// Tier-preserving: avoids unnecessary type widening.
    ///
    /// This method returns a `RationalParts` enum that preserves the exact storage tier,
    /// enabling tier-specific arithmetic without over-promotion to i128.
    ///
    /// **BENEFITS**:
    /// - Zero overhead for tier-native operations
    /// - Supports all tiers including I256/I512 (which don't fit in i128)
    /// - Explicit tier information in return type
    /// - Prevents accidental precision loss from over-promotion
    ///
    /// **USAGE**:
    /// ```ignore
    /// let parts = rational.extract_native();
    /// match parts {
    ///     RationalParts::Tiny(n, d) => { /* i8/u8 arithmetic */ }
    ///     RationalParts::Huge(n, d) => { /* i128/u128 arithmetic */ }
    ///     RationalParts::Massive(n, d) => { /* I256 arithmetic */ }
    ///     // ... handle other tiers
    /// }
    /// ```
    ///
    /// **MIGRATION**: Replaces `extract_as_i128()` for tier-preserving code paths.
    pub fn extract_native(&self) -> RationalParts {
        match &self.storage {
            RationalStorage::Tiny { num, den } => RationalParts::Tiny(*num, *den),
            RationalStorage::Small { num, den } => RationalParts::Small(*num, *den),
            RationalStorage::Medium { num, den } => RationalParts::Medium(*num, *den),
            RationalStorage::Large { num, den } => RationalParts::Large(*num, *den),
            RationalStorage::Huge { num, den } => RationalParts::Huge(*num, *den),
            #[cfg(not(feature = "embedded"))]
            RationalStorage::Massive { num, den } => RationalParts::Massive(num.clone(), den.clone()),
            RationalStorage::Ultra { num, den } => RationalParts::Ultra(num.clone(), den.clone()),
            #[cfg(feature = "infinite-precision")]
            RationalStorage::Infinite { num, den } => RationalParts::Infinite(num.clone(), den.clone()),
        }
    }

    /// Convert to f64 (lossy conversion)
    ///
    /// **WARNING**: This conversion is lossy and should only be used for:
    /// - Display/debugging purposes
    /// - Interop with floating-point APIs
    /// - Approximate comparisons
    ///
    /// **PRECISION**: Loses exact rational representation, subject to f64 limitations
    pub fn to_f64(&self) -> f64 {
        // Extract as i128 for tier-preserving conversion
        match self.extract_native().try_as_i128() {
            Some((num, den)) => {
                // Convert to f64 with standard division
                (num as f64) / (den as f64)
            }
            None => {
                // For tiers beyond i128, use BigInt (feature-gated)
                #[cfg(feature = "infinite-precision")]
                {
                    use num_bigint::ToBigInt;
                    let num_big = self.numerator();
                    let den_big = self.denominator();
                    // Convert BigInt to f64 (very lossy for large values)
                    num_big.to_f64().unwrap_or(f64::NAN) / den_big.to_f64().unwrap_or(1.0)
                }
                #[cfg(not(feature = "infinite-precision"))]
                {
                    // Without BigInt tier, this shouldn't happen
                    f64::NAN
                }
            }
        }
    }

}


// ================================================================================================
// IMPLEMENTATION: Overflow Detection for Universal Wrapper
// ================================================================================================

impl RationalNumber {
    /// Attempt multiplication with overflow detection for universal wrapper coordination
    /// INTEGRATION V2: Delegates to symbolic_arithmetic mathematical engine
    pub fn try_multiply(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let result_tier = Self::predict_multiplication_tier(&self.storage, &other.storage);
        
        // Check deployment profile constraints  
        let max_allowed_tier = RationalStorage::max_tier_for_profile();
        
        if result_tier > max_allowed_tier {
            return Err(OverflowDetected::PrecisionLimit);
        }
        
        // DELEGATION: Route to appropriate symbolic_arithmetic mathematical functions
        let math_result = match (&self.storage, &other.storage) {
            // Tiny × Tiny operations → delegate to symbolic_arithmetic::mul_i8_rational
            (RationalStorage::Tiny { num: a_num, den: a_den },
             RationalStorage::Tiny { num: b_num, den: b_den }) => {
                mul_i8_rational(*a_num, *a_den, *b_num, *b_den)
                    .map(|(num, den)| (num as i128, den as u128))
            },
            
            // Small × Small operations → delegate to symbolic_arithmetic::mul_i16_rational
            (RationalStorage::Small { num: a_num, den: a_den },
             RationalStorage::Small { num: b_num, den: b_den }) => {
                mul_i16_rational(*a_num, *a_den, *b_num, *b_den)
                    .map(|(num, den)| (num as i128, den as u128))
            },
            
            // Medium × Medium operations → delegate to symbolic_arithmetic::mul_i32_rational
            (RationalStorage::Medium { num: a_num, den: a_den },
             RationalStorage::Medium { num: b_num, den: b_den }) => {
                mul_i32_rational(*a_num, *a_den, *b_num, *b_den)
                    .map(|(num, den)| (num as i128, den as u128))
            },
            
            // Large × Large operations → delegate to symbolic_arithmetic::mul_i64_rational  
            (RationalStorage::Large { num: a_num, den: a_den },
             RationalStorage::Large { num: b_num, den: b_den }) => {
                mul_i64_rational(*a_num, *a_den, *b_num, *b_den)
                    .map(|(num, den)| (num as i128, den as u128))
            },
            
            // Huge × Huge operations → delegate to symbolic_arithmetic::mul_i128_rational
            (RationalStorage::Huge { num: a_num, den: a_den },
             RationalStorage::Huge { num: b_num, den: b_den }) => {
                mul_i128_rational(*a_num, *a_den, *b_num, *b_den)
            },
            
            // For mixed tiers, promote both to higher tier and delegate without precision loss
            _ => {
                return self.multiply_mixed_tiers(other);
            }
        };
        
        // COORDINATION: Handle mathematical result with UGOD tier promotion
        match math_result {
            Ok((result_num, result_den)) => {
                Ok(Self::new(result_num, result_den))
            },
            Err(OverflowDetected::TierOverflow) => {
                // UGOD promotion: recompute at i128 using native checked arithmetic (FASC-compliant)
                let (a_num, a_den) = self.extract_i128_pair()?;
                let (b_num, b_den) = other.extract_i128_pair()?;
                let num = a_num.checked_mul(b_num).ok_or(OverflowDetected::TierOverflow)?;
                let den = a_den.checked_mul(b_den).ok_or(OverflowDetected::TierOverflow)?;
                let g = gcd_unsigned(num.unsigned_abs(), den);
                Ok(Self::new(num / (g as i128), den / g))
            },
            Err(e) => Err(e),
        }
    }

    /// Multiplication fallback using highest available tier
    /// INTEGRATION V2: Uses BigInt delegation through symbolic_arithmetic for mathematical correctness
    pub fn multiply_at_max_tier(&self, other: &Self) -> Self {
        // Convert both to highest available tier
        let self_promoted = self.promote_to_max_tier();
        let other_promoted = other.promote_to_max_tier();

        #[cfg(feature = "infinite-precision")]
        {
            if let (RationalStorage::Infinite { num: a_num, den: a_den },
                    RationalStorage::Infinite { num: b_num, den: b_den }) =
                   (&self_promoted.storage, &other_promoted.storage) {

                // Delegate through i128 tier with BigInt intermediate fallback
                let a_num_i128 = a_num.to_i128().unwrap_or(0);
                let a_den_i128 = a_den.to_u128().unwrap_or(1);
                let b_num_i128 = b_num.to_i128().unwrap_or(0);
                let b_den_i128 = b_den.to_u128().unwrap_or(1);

                // Use symbolic_arithmetic with BigInt fallback for intermediate calculations
                match mul_i128_rational(a_num_i128, a_den_i128, b_num_i128, b_den_i128) {
                    Ok((result_num, result_den)) => {
                        // Result fits in i128 - create from result
                        Self::new(result_num, result_den)
                    },
                    Err(_) => {
                        // Fallback to direct BigInt multiplication for truly large values
                        let result_num = a_num * b_num;
                        let result_den = a_den * b_den;

                        Self {
                            storage: RationalStorage::Infinite {
                                num: result_num,
                                den: result_den
                            },
                            metadata: ShadowMetadata::new(DomainType::Symbolic, ExactnessType::Exact),
                                }
                    }
                }
            } else {
                unreachable!("promote_to_max_tier should always return BigInt storage")
            }
        }

        #[cfg(not(feature = "infinite-precision"))]
        {
            // Without BigInt, use i128 multiplication
            // Extract operands as i128 with saturation for values that don't fit
            let (a_num_i128, a_den_i128) = self_promoted.extract_native().try_as_i128()
                .unwrap_or((i128::MAX, 1)); // Saturate on overflow
            let (b_num_i128, b_den_i128) = other_promoted.extract_native().try_as_i128()
                .unwrap_or((i128::MAX, 1)); // Saturate on overflow

            // Use i128 multiplication (best available without BigInt)
            match mul_i128_rational(a_num_i128, a_den_i128, b_num_i128, b_den_i128) {
                Ok((result_num, result_den)) => Self::new(result_num, result_den),  // mul_i128_rational returns (i128, u128)
                Err(_) => {
                    // Overflow - return saturated value
                    Self::new(i128::MAX, 1)
                }
            }
        }
    }
    
    /// Precision-preserving mixed-tier multiplication
    /// Promotes both operands to the higher tier and delegates without precision loss
    fn multiply_mixed_tiers(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let self_tier = self.storage.tier_level();
        let other_tier = other.storage.tier_level();
        let target_tier = self_tier.max(other_tier);
        
        // Check profile limits
        if target_tier > RationalStorage::max_tier_for_profile() {
            return Err(OverflowDetected::PrecisionLimit);
        }
        
        // Promote both operands to target tier and delegate
        match target_tier {
            1 => self.promote_both_to_tiny(other).and_then(|(a, b)| a.multiply_tiny_tier(&b)),
            2 => self.promote_both_to_small(other).and_then(|(a, b)| a.multiply_small_tier(&b)),
            3 => self.promote_both_to_medium(other).and_then(|(a, b)| a.multiply_medium_tier(&b)),
            4 => self.promote_both_to_large(other).and_then(|(a, b)| a.multiply_large_tier(&b)),
            5 => self.promote_both_to_huge(other).and_then(|(a, b)| a.multiply_huge_tier(&b)),
            #[cfg(not(feature = "embedded"))]
            6 => self.promote_both_to_massive(other).and_then(|(a, b)| a.multiply_massive_tier(&b)),
            7 => self.promote_both_to_ultra(other).and_then(|(a, b)| a.multiply_ultra_tier(&b)),
            #[cfg(feature = "infinite-precision")]
            8 => Ok(self.promote_to_max_tier().multiply_at_max_tier(&other.promote_to_max_tier())),
            _ => Err(OverflowDetected::PrecisionLimit)
        }
    }

    /// Precision-preserving mixed-tier addition
    /// Promotes both operands to the higher tier and delegates without precision loss
    fn add_mixed_tiers(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let self_tier = self.storage.tier_level();
        let other_tier = other.storage.tier_level();
        let target_tier = self_tier.max(other_tier);
        
        // Check profile limits
        if target_tier > RationalStorage::max_tier_for_profile() {
            return Err(OverflowDetected::PrecisionLimit);
        }
        
        // Promote both operands to target tier and delegate
        match target_tier {
            1 => self.promote_both_to_tiny(other).and_then(|(a, b)| a.add_tiny_tier(&b)),
            2 => self.promote_both_to_small(other).and_then(|(a, b)| a.add_small_tier(&b)),
            3 => self.promote_both_to_medium(other).and_then(|(a, b)| a.add_medium_tier(&b)),
            4 => self.promote_both_to_large(other).and_then(|(a, b)| a.add_large_tier(&b)),
            5 => self.promote_both_to_huge(other).and_then(|(a, b)| a.add_huge_tier(&b)),
            #[cfg(not(feature = "embedded"))]
            6 => self.promote_both_to_massive(other).and_then(|(a, b)| a.add_massive_tier(&b)),
            7 => self.promote_both_to_ultra(other).and_then(|(a, b)| a.add_ultra_tier(&b)),
            _ => Err(OverflowDetected::PrecisionLimit)
        }
    }

    /// Precision-preserving mixed-tier subtraction
    fn subtract_mixed_tiers(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let self_tier = self.storage.tier_level();
        let other_tier = other.storage.tier_level();
        let target_tier = self_tier.max(other_tier);

        if target_tier > RationalStorage::max_tier_for_profile() {
            return Err(OverflowDetected::PrecisionLimit);
        }

        match target_tier {
            1 => self.promote_both_to_tiny(other).and_then(|(a, b)| a.subtract_tiny_tier(&b)),
            2 => self.promote_both_to_small(other).and_then(|(a, b)| a.subtract_small_tier(&b)),
            3 => self.promote_both_to_medium(other).and_then(|(a, b)| a.subtract_medium_tier(&b)),
            4 => self.promote_both_to_large(other).and_then(|(a, b)| a.subtract_large_tier(&b)),
            5 => self.promote_both_to_huge(other).and_then(|(a, b)| a.subtract_huge_tier(&b)),
            #[cfg(not(feature = "embedded"))]
            6 => self.promote_both_to_massive(other).and_then(|(a, b)| a.subtract_massive_tier(&b)),
            7 => self.promote_both_to_ultra(other).and_then(|(a, b)| a.subtract_ultra_tier(&b)),
            _ => Err(OverflowDetected::PrecisionLimit)
        }
    }

    /// Precision-preserving mixed-tier division
    fn divide_mixed_tiers(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let self_tier = self.storage.tier_level();
        let other_tier = other.storage.tier_level();
        let target_tier = self_tier.max(other_tier);

        if target_tier > RationalStorage::max_tier_for_profile() {
            return Err(OverflowDetected::PrecisionLimit);
        }

        match target_tier {
            1 => self.promote_both_to_tiny(other).and_then(|(a, b)| a.divide_tiny_tier(&b)),
            2 => self.promote_both_to_small(other).and_then(|(a, b)| a.divide_small_tier(&b)),
            3 => self.promote_both_to_medium(other).and_then(|(a, b)| a.divide_medium_tier(&b)),
            4 => self.promote_both_to_large(other).and_then(|(a, b)| a.divide_large_tier(&b)),
            5 => self.promote_both_to_huge(other).and_then(|(a, b)| a.divide_huge_tier(&b)),
            #[cfg(not(feature = "embedded"))]
            6 => self.promote_both_to_massive(other).and_then(|(a, b)| a.divide_massive_tier(&b)),
            7 => self.promote_both_to_ultra(other).and_then(|(a, b)| a.divide_ultra_tier(&b)),
            _ => Err(OverflowDetected::PrecisionLimit)
        }
    }
    
    /// Predict result tier for multiplication
    fn predict_multiplication_tier(a: &RationalStorage, b: &RationalStorage) -> u8 {
        // Result tier = max of operand tiers. BigInt intermediates handle overflow.
        a.tier_level().max(b.tier_level())
    }

    /// Predict result tier for addition
    fn predict_addition_tier(a: &RationalStorage, b: &RationalStorage) -> u8 {
        // Result tier = max of operand tiers. The i128 rational functions use BigInt
        // intermediates for cross-denominator calculations, so no +1 headroom needed.
        a.tier_level().max(b.tier_level())
    }

    /// Predict result tier for subtraction
    fn predict_subtraction_tier(a: &RationalStorage, b: &RationalStorage) -> u8 {
        a.tier_level().max(b.tier_level())
    }

    /// Predict result tier for division
    fn predict_division_tier(a: &RationalStorage, b: &RationalStorage) -> u8 {
        a.tier_level().max(b.tier_level())
    }
    
    /// Promote current value to BigInt tier
    fn promote_to_max_tier(&self) -> Self {
        #[cfg(feature = "infinite-precision")]
        {
            // Extract as BigInt
            let num = self.numerator();
            let den = self.denominator();
            Self {
                storage: RationalStorage::Infinite { num, den },
                metadata: self.metadata,
            }
        }

        #[cfg(not(feature = "infinite-precision"))]
        {
            // Fallback: Use highest available tier (Huge with i128) - PURE INTEGER ARITHMETIC
            // Try to convert to Huge tier (i128/u128) or saturate if too large
            match self.extract_native().try_as_i128() {
                Some((num, den)) => Self::new(num, den),  // try_as_i128() already returns (i128, u128)
                None => {
                    // Value exceeds i128 range (Massive/Ultra tiers), saturate
                    Self::new(i128::MAX, 1)
                }
            }
        }
    }
    
    // ================================================================================================
    // PROMOTION METHODS: Tier promotion and conversion infrastructure
    // ================================================================================================
    
    /// Promote both operands to tiny tier
    fn promote_both_to_tiny(&self, other: &Self) -> Result<(Self, Self), OverflowDetected> {
        let a = self.promote_to_tier(1)?;
        let b = other.promote_to_tier(1)?;
        Ok((a, b))
    }
    
    /// Promote both operands to small tier
    fn promote_both_to_small(&self, other: &Self) -> Result<(Self, Self), OverflowDetected> {
        let a = self.promote_to_tier(2)?;
        let b = other.promote_to_tier(2)?;
        Ok((a, b))
    }
    
    /// Promote both operands to medium tier
    fn promote_both_to_medium(&self, other: &Self) -> Result<(Self, Self), OverflowDetected> {
        let a = self.promote_to_tier(3)?;
        let b = other.promote_to_tier(3)?;
        Ok((a, b))
    }
    
    /// Promote both operands to large tier
    fn promote_both_to_large(&self, other: &Self) -> Result<(Self, Self), OverflowDetected> {
        let a = self.promote_to_tier(4)?;
        let b = other.promote_to_tier(4)?;
        Ok((a, b))
    }
    
    /// Promote both operands to huge tier
    fn promote_both_to_huge(&self, other: &Self) -> Result<(Self, Self), OverflowDetected> {
        let a = self.promote_to_tier(5)?;
        let b = other.promote_to_tier(5)?;
        Ok((a, b))
    }
    
    /// Promote both operands to massive tier
    #[cfg(not(feature = "embedded"))]
    fn promote_both_to_massive(&self, other: &Self) -> Result<(Self, Self), OverflowDetected> {
        let a = self.promote_to_tier(6)?;
        let b = other.promote_to_tier(6)?;
        Ok((a, b))
    }
    
    /// Promote both operands to ultra tier
    fn promote_both_to_ultra(&self, other: &Self) -> Result<(Self, Self), OverflowDetected> {
        let a = self.promote_to_tier(7)?;
        let b = other.promote_to_tier(7)?;
        Ok((a, b))
    }
    
    /// Generic promotion to target tier
    fn promote_to_tier(&self, target_tier: u8) -> Result<Self, OverflowDetected> {
        let current_tier = self.storage.tier_level();

        if current_tier >= target_tier {
            return Ok(self.clone()); // Already at or above target tier
        }

        // Check if target tier is allowed by deployment profile
        let max_allowed_tier = RationalStorage::max_tier_for_profile();
        if target_tier > max_allowed_tier {
            return Err(OverflowDetected::PrecisionLimit);
        }

        let parts = self.extract_native();

        // Tier 6 (Massive, I256): widen to I256 pair
        #[cfg(not(feature = "embedded"))]
        if target_tier == 6 {
            let (num, den) = parts.try_as_i256_pair()
                .ok_or(OverflowDetected::PrecisionLimit)?;
            return Ok(Self::from_i256_pair(num, den));
        }

        // Tier 7 (Ultra, I512): widen to I512 pair
        if target_tier == 7 {
            let (num, den) = parts.try_as_i512_pair()
                .ok_or(OverflowDetected::PrecisionLimit)?;
            return Ok(Self::from_i512_pair(num, den));
        }

        // Tiers 1-5: extract as i128/u128 and reconstruct
        let (num_i128, den_u128) = parts.try_as_i128()
            .ok_or(OverflowDetected::PrecisionLimit)?;
        Ok(Self::new(num_i128, den_u128))
    }
    
    // ================================================================================================
    // TIER-SPECIFIC OPERATION METHODS: Mathematical operations at specific tiers
    // ================================================================================================
    
    /// Addition at tiny tier: a/a_den + b/b_den = (a*b_den + b*a_den) / (a_den*b_den)
    fn add_tiny_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        match (&self.storage, &other.storage) {
            (RationalStorage::Tiny { num: a_num, den: a_den },
             RationalStorage::Tiny { num: b_num, den: b_den }) => {
                let num = *a_num as i128 * *b_den as i128 + *b_num as i128 * *a_den as i128;
                let den = *a_den as u128 * *b_den as u128;
                Ok(Self::new(num, den))
            },
            _ => Err(OverflowDetected::TierOverflow),
        }
    }

    /// Subtraction at tiny tier: a/a_den - b/b_den = (a*b_den - b*a_den) / (a_den*b_den)
    fn subtract_tiny_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        match (&self.storage, &other.storage) {
            (RationalStorage::Tiny { num: a_num, den: a_den },
             RationalStorage::Tiny { num: b_num, den: b_den }) => {
                let num = *a_num as i128 * *b_den as i128 - *b_num as i128 * *a_den as i128;
                let den = *a_den as u128 * *b_den as u128;
                Ok(Self::new(num, den))
            },
            _ => Err(OverflowDetected::TierOverflow),
        }
    }
    
    /// Multiplication at tiny tier  
    fn multiply_tiny_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        match (&self.storage, &other.storage) {
            (RationalStorage::Tiny { num: a_num, den: a_den },
             RationalStorage::Tiny { num: b_num, den: b_den }) => {
                Ok(Self::new(*a_num as i128 * *b_num as i128, *a_den as u128 * *b_den as u128))
            },
            _ => Err(OverflowDetected::TierOverflow),
        }
    }
    
    /// Division at tiny tier: (a/a_den) / (b/b_den) = (a*b_den) / (a_den*|b|), with sign
    fn divide_tiny_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        match (&self.storage, &other.storage) {
            (RationalStorage::Tiny { num: a_num, den: a_den },
             RationalStorage::Tiny { num: b_num, den: b_den }) => {
                if *b_num == 0 {
                    return Err(OverflowDetected::PrecisionLimit);
                }
                // (a/a_den) / (b/b_den) = (a*b_den) / (a_den*|b|)
                // When b is negative, negate the numerator to preserve sign
                let mut num = *a_num as i128 * *b_den as i128;
                if *b_num < 0 {
                    num = -num;
                }
                let den = *a_den as u128 * b_num.unsigned_abs() as u128;
                Ok(Self::new(num, den))
            },
            _ => Err(OverflowDetected::TierOverflow),
        }
    }
    
    // Tier-specific arithmetic methods — called by mixed-tier promotion paths.
    // Extract (i128, u128) from both operands and delegate to symbolic_arithmetic.
    // After promote_both_to_xxx, both operands are guaranteed to be at that tier,
    // so all values fit in i128/u128.

    fn extract_i128_pair(&self) -> Result<(i128, u128), OverflowDetected> {
        match &self.storage {
            RationalStorage::Tiny { num, den } => Ok((*num as i128, *den as u128)),
            RationalStorage::Small { num, den } => Ok((*num as i128, *den as u128)),
            RationalStorage::Medium { num, den } => Ok((*num as i128, *den as u128)),
            RationalStorage::Large { num, den } => Ok((*num as i128, *den as u128)),
            RationalStorage::Huge { num, den } => Ok((*num, *den)),
            _ => Err(OverflowDetected::PrecisionLimit),
        }
    }

    fn from_i128_result(result: Result<(i128, u128), OverflowDetected>) -> Result<Self, OverflowDetected> {
        result.map(|(num, den)| Self::new(num, den))
    }

    fn add_small_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i128_pair()?;
        let (bn, bd) = other.extract_i128_pair()?;
        Self::from_i128_result(add_i128_rational(an, ad, bn, bd))
    }
    fn subtract_small_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i128_pair()?;
        let (bn, bd) = other.extract_i128_pair()?;
        Self::from_i128_result(sub_i128_rational(an, ad, bn, bd))
    }
    fn multiply_small_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i128_pair()?;
        let (bn, bd) = other.extract_i128_pair()?;
        Self::from_i128_result(mul_i128_rational(an, ad, bn, bd))
    }
    fn divide_small_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i128_pair()?;
        let (bn, bd) = other.extract_i128_pair()?;
        Self::from_i128_result(div_i128_rational(an, ad, bn, bd))
    }

    fn add_medium_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i128_pair()?;
        let (bn, bd) = other.extract_i128_pair()?;
        Self::from_i128_result(add_i128_rational(an, ad, bn, bd))
    }
    fn subtract_medium_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i128_pair()?;
        let (bn, bd) = other.extract_i128_pair()?;
        Self::from_i128_result(sub_i128_rational(an, ad, bn, bd))
    }
    fn multiply_medium_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i128_pair()?;
        let (bn, bd) = other.extract_i128_pair()?;
        Self::from_i128_result(mul_i128_rational(an, ad, bn, bd))
    }
    fn divide_medium_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i128_pair()?;
        let (bn, bd) = other.extract_i128_pair()?;
        Self::from_i128_result(div_i128_rational(an, ad, bn, bd))
    }

    fn add_large_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i128_pair()?;
        let (bn, bd) = other.extract_i128_pair()?;
        Self::from_i128_result(add_i128_rational(an, ad, bn, bd))
    }
    fn subtract_large_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i128_pair()?;
        let (bn, bd) = other.extract_i128_pair()?;
        Self::from_i128_result(sub_i128_rational(an, ad, bn, bd))
    }
    fn multiply_large_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i128_pair()?;
        let (bn, bd) = other.extract_i128_pair()?;
        Self::from_i128_result(mul_i128_rational(an, ad, bn, bd))
    }
    fn divide_large_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i128_pair()?;
        let (bn, bd) = other.extract_i128_pair()?;
        Self::from_i128_result(div_i128_rational(an, ad, bn, bd))
    }

    fn add_huge_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i128_pair()?;
        let (bn, bd) = other.extract_i128_pair()?;
        Self::from_i128_result(add_i128_rational(an, ad, bn, bd))
    }
    fn subtract_huge_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i128_pair()?;
        let (bn, bd) = other.extract_i128_pair()?;
        Self::from_i128_result(sub_i128_rational(an, ad, bn, bd))
    }
    fn multiply_huge_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i128_pair()?;
        let (bn, bd) = other.extract_i128_pair()?;
        Self::from_i128_result(mul_i128_rational(an, ad, bn, bd))
    }
    fn divide_huge_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i128_pair()?;
        let (bn, bd) = other.extract_i128_pair()?;
        Self::from_i128_result(div_i128_rational(an, ad, bn, bd))
    }
    
    // ═══ Massive tier (I256) arithmetic — uses I512 intermediates ═══

    #[cfg(not(feature = "embedded"))]
    fn extract_i256_pair(&self) -> Result<(I256, I256), OverflowDetected> {
        match &self.storage {
            RationalStorage::Massive { num, den } => Ok((*num, *den)),
            _ => {
                // Promote from smaller tier
                let parts = self.extract_native();
                if let Some((n, d)) = parts.try_as_i128() {
                    Ok((I256::from_i128(n), I256::from_i128(d as i128)))
                } else {
                    Err(OverflowDetected::TierOverflow)
                }
            }
        }
    }

    #[cfg(not(feature = "embedded"))]
    fn add_massive_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i256_pair()?;
        let (bn, bd) = other.extract_i256_pair()?;
        // a/b + c/d = (a*d + c*b) / (b*d) using I512 intermediates
        let an_w = I512::from_i256(an);
        let ad_w = I512::from_i256(ad);
        let bn_w = I512::from_i256(bn);
        let bd_w = I512::from_i256(bd);
        let num = an_w * bd_w + bn_w * ad_w;
        let den = ad_w * bd_w;
        // GCD reduce then downscale
        let g = gcd_i512(num, den);
        let rn = num / g;
        let rd = den / g;
        // Check fits in I256
        if rn.fits_in_i256() && rd.fits_in_i256() {
            Ok(Self::from_i256_pair(rn.as_i256(), rd.as_i256()))
        } else {
            Err(OverflowDetected::TierOverflow)
        }
    }

    #[cfg(not(feature = "embedded"))]
    fn subtract_massive_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i256_pair()?;
        let (bn, bd) = other.extract_i256_pair()?;
        let an_w = I512::from_i256(an);
        let ad_w = I512::from_i256(ad);
        let bn_w = I512::from_i256(bn);
        let bd_w = I512::from_i256(bd);
        let num = an_w * bd_w - bn_w * ad_w;
        let den = ad_w * bd_w;
        let g = gcd_i512(num, den);
        let rn = num / g;
        let rd = den / g;
        if rn.fits_in_i256() && rd.fits_in_i256() {
            Ok(Self::from_i256_pair(rn.as_i256(), rd.as_i256()))
        } else {
            Err(OverflowDetected::TierOverflow)
        }
    }

    #[cfg(not(feature = "embedded"))]
    fn multiply_massive_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i256_pair()?;
        let (bn, bd) = other.extract_i256_pair()?;
        // Cross-GCD reduction before multiplication: (a/ad) * (b/bd)
        // g1 = gcd(a, bd), g2 = gcd(b, ad) → reduces intermediate sizes
        let g1 = gcd_i256(an, bd);
        let g2 = gcd_i256(bn, ad);
        let an_r = an / g1;
        let bd_r = bd / g1;
        let bn_r = bn / g2;
        let ad_r = ad / g2;
        let an_w = I512::from_i256(an_r);
        let ad_w = I512::from_i256(ad_r);
        let bn_w = I512::from_i256(bn_r);
        let bd_w = I512::from_i256(bd_r);
        let num = an_w * bn_w;
        let den = ad_w * bd_w;
        // Post-multiply GCD (usually 1 after cross-reduction, but be safe)
        let g = gcd_i512(num, den);
        let rn = num / g;
        let rd = den / g;
        if rn.fits_in_i256() && rd.fits_in_i256() {
            Ok(Self::from_i256_pair(rn.as_i256(), rd.as_i256()))
        } else {
            Err(OverflowDetected::TierOverflow)
        }
    }

    #[cfg(not(feature = "embedded"))]
    fn divide_massive_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i256_pair()?;
        let (bn, bd) = other.extract_i256_pair()?;
        if bn.is_zero() { return Err(OverflowDetected::TierOverflow); }
        // (a/b) / (c/d) = (a*d) / (b*c)
        let an_w = I512::from_i256(an);
        let ad_w = I512::from_i256(ad);
        let bn_w = I512::from_i256(bn);
        let bd_w = I512::from_i256(bd);
        let num = an_w * bd_w;
        let den = ad_w * bn_w;
        let g = gcd_i512(num, den);
        let rn = num / g;
        let rd = den / g;
        // Normalize sign: denominator should be positive
        let (rn, rd) = if rd.is_negative() { (-rn, -rd) } else { (rn, rd) };
        if rn.fits_in_i256() && rd.fits_in_i256() {
            Ok(Self::from_i256_pair(rn.as_i256(), rd.as_i256()))
        } else {
            Err(OverflowDetected::TierOverflow)
        }
    }

    // ═══ Ultra tier (I512) arithmetic — uses I1024 intermediates ═══

    fn extract_i512_pair(&self) -> Result<(I512, I512), OverflowDetected> {
        match &self.storage {
            RationalStorage::Ultra { num, den } => Ok((*num, *den)),
            #[cfg(not(feature = "embedded"))]
            RationalStorage::Massive { num, den } => {
                Ok((I512::from_i256(*num), I512::from_i256(*den)))
            }
            _ => {
                let parts = self.extract_native();
                if let Some((n, d)) = parts.try_as_i128() {
                    Ok((I512::from_i128(n), I512::from_i128(d as i128)))
                } else {
                    Err(OverflowDetected::TierOverflow)
                }
            }
        }
    }

    fn add_ultra_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i512_pair()?;
        let (bn, bd) = other.extract_i512_pair()?;
        let an_w = I1024::from_i512(an);
        let ad_w = I1024::from_i512(ad);
        let bn_w = I1024::from_i512(bn);
        let bd_w = I1024::from_i512(bd);
        let num = an_w * bd_w + bn_w * ad_w;
        let den = ad_w * bd_w;
        let g = gcd_i1024(num, den);
        let rn = num / g;
        let rd = den / g;
        if rn.fits_in_i512() && rd.fits_in_i512() {
            Ok(Self::from_i512_pair(rn.as_i512(), rd.as_i512()))
        } else {
            Err(OverflowDetected::TierOverflow)
        }
    }

    fn subtract_ultra_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i512_pair()?;
        let (bn, bd) = other.extract_i512_pair()?;
        let an_w = I1024::from_i512(an);
        let ad_w = I1024::from_i512(ad);
        let bn_w = I1024::from_i512(bn);
        let bd_w = I1024::from_i512(bd);
        let num = an_w * bd_w - bn_w * ad_w;
        let den = ad_w * bd_w;
        let g = gcd_i1024(num, den);
        let rn = num / g;
        let rd = den / g;
        if rn.fits_in_i512() && rd.fits_in_i512() {
            Ok(Self::from_i512_pair(rn.as_i512(), rd.as_i512()))
        } else {
            Err(OverflowDetected::TierOverflow)
        }
    }

    fn multiply_ultra_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i512_pair()?;
        let (bn, bd) = other.extract_i512_pair()?;
        // Cross-GCD reduction before multiplication
        let g1 = gcd_i512(an, bd);
        let g2 = gcd_i512(bn, ad);
        let an_r = an / g1;
        let bd_r = bd / g1;
        let bn_r = bn / g2;
        let ad_r = ad / g2;
        let an_w = I1024::from_i512(an_r);
        let ad_w = I1024::from_i512(ad_r);
        let bn_w = I1024::from_i512(bn_r);
        let bd_w = I1024::from_i512(bd_r);
        let num = an_w * bn_w;
        let den = ad_w * bd_w;
        let g = gcd_i1024(num, den);
        let rn = num / g;
        let rd = den / g;
        if rn.fits_in_i512() && rd.fits_in_i512() {
            Ok(Self::from_i512_pair(rn.as_i512(), rd.as_i512()))
        } else {
            Err(OverflowDetected::TierOverflow)
        }
    }

    fn divide_ultra_tier(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let (an, ad) = self.extract_i512_pair()?;
        let (bn, bd) = other.extract_i512_pair()?;
        if bn.is_zero() { return Err(OverflowDetected::TierOverflow); }
        let an_w = I1024::from_i512(an);
        let ad_w = I1024::from_i512(ad);
        let bn_w = I1024::from_i512(bn);
        let bd_w = I1024::from_i512(bd);
        let num = an_w * bd_w;
        let den = ad_w * bn_w;
        let g = gcd_i1024(num, den);
        let rn = num / g;
        let rd = den / g;
        let (rn, rd) = if (rd.words[15] as i64) < 0 { (-rn, -rd) } else { (rn, rd) };
        if rn.fits_in_i512() && rd.fits_in_i512() {
            Ok(Self::from_i512_pair(rn.as_i512(), rd.as_i512()))
        } else {
            Err(OverflowDetected::TierOverflow)
        }
    }

    /// Extract exact rational as BigInt pair. Prefer `extract_native()` for tiers 1-7.
    /// - Tier 8 (BigInt) with `#[cfg(feature = "infinite-precision")]` - ONLY valid use
    /// - Fallback in mixed-tier comparisons when tier-preserving extraction fails (extremely rare)
    ///
    /// **MIGRATION**: If you're using this for tiers 1-7, switch to `extract_native()` or `try_as_i128()`.
    #[cfg(feature = "infinite-precision")]
    pub(crate) fn extract_exact_rational(&self) -> (BigInt, BigInt) {
        match &self.storage {
            // Tiers 1-7: Convert to BigInt (PERFORMANCE WARNING - avoid if possible)
            RationalStorage::Tiny { num, den } => (BigInt::from(*num), BigInt::from(*den)),
            RationalStorage::Small { num, den } => (BigInt::from(*num), BigInt::from(*den)),
            RationalStorage::Medium { num, den } => (BigInt::from(*num), BigInt::from(*den)),
            RationalStorage::Large { num, den } => (BigInt::from(*num), BigInt::from(*den)),
            RationalStorage::Huge { num, den } => (BigInt::from(*num), BigInt::from(*den)),
            #[cfg(not(feature = "embedded"))]
            RationalStorage::Massive { num, den } => {
                // I256::as_i128() returns i128 directly (not Option)
                (BigInt::from(num.as_i128()), BigInt::from(den.as_i128()))
            },
            RationalStorage::Ultra { num, den } => {
                (BigInt::from(num.as_i128()), BigInt::from(den.as_i128()))
            },
            // Tier 8: Proper use case
            RationalStorage::Infinite { num, den } => (num.clone(), den.clone()),
        }
    }

    // extract_exact_rational() only exists with infinite-precision feature
    // For tiers 1-7, use extract_as_i128() instead
    
    /// Addition with overflow detection for universal wrapper coordination
    /// INTEGRATION V2: Delegates to symbolic_arithmetic mathematical engine
    pub fn try_add(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let result_tier = Self::predict_addition_tier(&self.storage, &other.storage);

        // Check compile-time tier constraints (GLOBAL_FLAGS may not be initialized)
        let max_allowed_tier = RationalStorage::max_tier_for_profile();

        if result_tier > max_allowed_tier {
            return Err(OverflowDetected::PrecisionLimit);
        }
        
        // DELEGATION: Route to appropriate symbolic_arithmetic mathematical functions
        let math_result = match (&self.storage, &other.storage) {
            // Tiny + Tiny operations → delegate to symbolic_arithmetic::add_i8_rational
            (RationalStorage::Tiny { num: a_num, den: a_den },
             RationalStorage::Tiny { num: b_num, den: b_den }) => {
                add_i8_rational(*a_num, *a_den, *b_num, *b_den)
                    .map(|(num, den)| (num as i128, den as u128))
            },
            
            // Small + Small operations → delegate to symbolic_arithmetic::add_i16_rational
            (RationalStorage::Small { num: a_num, den: a_den },
             RationalStorage::Small { num: b_num, den: b_den }) => {
                add_i16_rational(*a_num, *a_den, *b_num, *b_den)
                    .map(|(num, den)| (num as i128, den as u128))
            },
            
            // Medium + Medium operations → delegate to symbolic_arithmetic::add_i32_rational
            (RationalStorage::Medium { num: a_num, den: a_den },
             RationalStorage::Medium { num: b_num, den: b_den }) => {
                add_i32_rational(*a_num, *a_den, *b_num, *b_den)
                    .map(|(num, den)| (num as i128, den as u128))
            },
            
            // Large + Large operations → delegate to symbolic_arithmetic::add_i64_rational  
            (RationalStorage::Large { num: a_num, den: a_den },
             RationalStorage::Large { num: b_num, den: b_den }) => {
                add_i64_rational(*a_num, *a_den, *b_num, *b_den)
                    .map(|(num, den)| (num as i128, den as u128))
            },
            
            // Huge + Huge operations → delegate to symbolic_arithmetic::add_i128_rational
            (RationalStorage::Huge { num: a_num, den: a_den },
             RationalStorage::Huge { num: b_num, den: b_den }) => {
                add_i128_rational(*a_num, *a_den, *b_num, *b_den)
            },
            
            // For mixed tiers, promote both to higher tier and delegate without precision loss
            _ => {
                return self.add_mixed_tiers(other);
            }
        };
        
        // COORDINATION: Handle mathematical result with UGOD tier promotion
        match math_result {
            Ok((result_num, result_den)) => {
                Ok(Self::new(result_num, result_den))
            },
            Err(OverflowDetected::TierOverflow) => {
                // UGOD promotion: recompute at i128 using native checked arithmetic (FASC-compliant)
                let (a_num, a_den) = self.extract_i128_pair()?;
                let (b_num, b_den) = other.extract_i128_pair()?;
                let b_den_s = i128::try_from(b_den).map_err(|_| OverflowDetected::TierOverflow)?;
                let a_den_s = i128::try_from(a_den).map_err(|_| OverflowDetected::TierOverflow)?;
                let a_exp = a_num.checked_mul(b_den_s).ok_or(OverflowDetected::TierOverflow)?;
                let b_exp = b_num.checked_mul(a_den_s).ok_or(OverflowDetected::TierOverflow)?;
                let num = a_exp.checked_add(b_exp).ok_or(OverflowDetected::TierOverflow)?;
                let den = a_den.checked_mul(b_den).ok_or(OverflowDetected::TierOverflow)?;
                let g = gcd_unsigned(num.unsigned_abs(), den);
                Ok(Self::new(num / (g as i128), den / g))
            },
            Err(e) => Err(e),
        }
    }

    /// Subtraction with overflow detection for universal wrapper coordination
    /// INTEGRATION V2: Delegates to symbolic_arithmetic mathematical engine
    pub fn try_subtract(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let result_tier = Self::predict_subtraction_tier(&self.storage, &other.storage);
        
        // Check deployment profile constraints  
        let max_allowed_tier = RationalStorage::max_tier_for_profile();
        
        if result_tier > max_allowed_tier {
            return Err(OverflowDetected::PrecisionLimit);
        }
        
        // DELEGATION: Route to appropriate symbolic_arithmetic mathematical functions
        let math_result = match (&self.storage, &other.storage) {
            // Tiny - Tiny operations → delegate to symbolic_arithmetic::sub_i8_rational
            (RationalStorage::Tiny { num: a_num, den: a_den },
             RationalStorage::Tiny { num: b_num, den: b_den }) => {
                sub_i8_rational(*a_num, *a_den, *b_num, *b_den)
                    .map(|(num, den)| (num as i128, den as u128))
            },
            
            // Small - Small operations → delegate to symbolic_arithmetic::sub_i16_rational
            (RationalStorage::Small { num: a_num, den: a_den },
             RationalStorage::Small { num: b_num, den: b_den }) => {
                sub_i16_rational(*a_num, *a_den, *b_num, *b_den)
                    .map(|(num, den)| (num as i128, den as u128))
            },
            
            // Medium - Medium operations → delegate to symbolic_arithmetic::sub_i32_rational
            (RationalStorage::Medium { num: a_num, den: a_den },
             RationalStorage::Medium { num: b_num, den: b_den }) => {
                sub_i32_rational(*a_num, *a_den, *b_num, *b_den)
                    .map(|(num, den)| (num as i128, den as u128))
            },
            
            // Large - Large operations → delegate to symbolic_arithmetic::sub_i64_rational  
            (RationalStorage::Large { num: a_num, den: a_den },
             RationalStorage::Large { num: b_num, den: b_den }) => {
                sub_i64_rational(*a_num, *a_den, *b_num, *b_den)
                    .map(|(num, den)| (num as i128, den as u128))
            },
            
            // Huge - Huge operations → delegate to symbolic_arithmetic::sub_i128_rational
            (RationalStorage::Huge { num: a_num, den: a_den },
             RationalStorage::Huge { num: b_num, den: b_den }) => {
                sub_i128_rational(*a_num, *a_den, *b_num, *b_den)
            },
            
            // For mixed tiers, promote both to higher tier and delegate without precision loss
            _ => {
                return self.subtract_mixed_tiers(other);
            }
        };

        // COORDINATION: Handle mathematical result with UGOD tier promotion
        match math_result {
            Ok((result_num, result_den)) => {
                Ok(Self::new(result_num, result_den))
            },
            Err(OverflowDetected::TierOverflow) => {
                // UGOD promotion: recompute at i128 using native checked arithmetic (FASC-compliant)
                let (a_num, a_den) = self.extract_i128_pair()?;
                let (b_num, b_den) = other.extract_i128_pair()?;
                let b_den_s = i128::try_from(b_den).map_err(|_| OverflowDetected::TierOverflow)?;
                let a_den_s = i128::try_from(a_den).map_err(|_| OverflowDetected::TierOverflow)?;
                let a_exp = a_num.checked_mul(b_den_s).ok_or(OverflowDetected::TierOverflow)?;
                let b_exp = b_num.checked_mul(a_den_s).ok_or(OverflowDetected::TierOverflow)?;
                let num = a_exp.checked_sub(b_exp).ok_or(OverflowDetected::TierOverflow)?;
                let den = a_den.checked_mul(b_den).ok_or(OverflowDetected::TierOverflow)?;
                let g = gcd_unsigned(num.unsigned_abs(), den);
                Ok(Self::new(num / (g as i128), den / g))
            },
            Err(e) => Err(e),
        }
    }
    
    /// Division with overflow detection for universal wrapper coordination
    /// INTEGRATION V2: Delegates to symbolic_arithmetic mathematical engine
    pub fn try_divide(&self, other: &Self) -> Result<Self, OverflowDetected> {
        let result_tier = Self::predict_division_tier(&self.storage, &other.storage);
        
        // Check deployment profile constraints  
        let max_allowed_tier = RationalStorage::max_tier_for_profile();
        
        if result_tier > max_allowed_tier {
            return Err(OverflowDetected::PrecisionLimit);
        }
        
        // DELEGATION: Route to appropriate symbolic_arithmetic mathematical functions
        let math_result = match (&self.storage, &other.storage) {
            // Tiny ÷ Tiny operations → delegate to symbolic_arithmetic::div_i8_rational
            (RationalStorage::Tiny { num: a_num, den: a_den },
             RationalStorage::Tiny { num: b_num, den: b_den }) => {
                div_i8_rational(*a_num, *a_den, *b_num, *b_den)
                    .map(|(num, den)| (num as i128, den as u128))
            },
            
            // Small ÷ Small operations → delegate to symbolic_arithmetic::div_i16_rational
            (RationalStorage::Small { num: a_num, den: a_den },
             RationalStorage::Small { num: b_num, den: b_den }) => {
                div_i16_rational(*a_num, *a_den, *b_num, *b_den)
                    .map(|(num, den)| (num as i128, den as u128))
            },
            
            // Medium ÷ Medium operations → delegate to symbolic_arithmetic::div_i32_rational
            (RationalStorage::Medium { num: a_num, den: a_den },
             RationalStorage::Medium { num: b_num, den: b_den }) => {
                div_i32_rational(*a_num, *a_den, *b_num, *b_den)
                    .map(|(num, den)| (num as i128, den as u128))
            },
            
            // Large ÷ Large operations → delegate to symbolic_arithmetic::div_i64_rational  
            (RationalStorage::Large { num: a_num, den: a_den },
             RationalStorage::Large { num: b_num, den: b_den }) => {
                div_i64_rational(*a_num, *a_den, *b_num, *b_den)
                    .map(|(num, den)| (num as i128, den as u128))
            },
            
            // Huge ÷ Huge operations → delegate to symbolic_arithmetic::div_i128_rational
            (RationalStorage::Huge { num: a_num, den: a_den },
             RationalStorage::Huge { num: b_num, den: b_den }) => {
                div_i128_rational(*a_num, *a_den, *b_num, *b_den)
            },

            // Massive ÷ Massive operations (I256 rational division)
            #[cfg(not(feature = "embedded"))]
            (RationalStorage::Massive { .. }, RationalStorage::Massive { .. }) => {
                // Delegate to mixed_tiers for I256 handling
                return self.divide_mixed_tiers(other);
            },

            // Ultra ÷ Ultra operations (I512 rational division)
            (RationalStorage::Ultra { .. }, RationalStorage::Ultra { .. }) => {
                // Delegate to mixed_tiers for I512 handling
                return self.divide_mixed_tiers(other);
            },

            // For mixed tiers, promote both to higher tier and delegate without precision loss
            _ => {
                return self.divide_mixed_tiers(other);
            }
        };

        // COORDINATION: Handle mathematical result with UGOD tier promotion
        match math_result {
            Ok((result_num, result_den)) => {
                Ok(Self::new(result_num, result_den))
            },
            Err(OverflowDetected::TierOverflow) => {
                // UGOD promotion: recompute at i128 using native checked arithmetic (FASC-compliant)
                let (a_num, a_den) = self.extract_i128_pair()?;
                let (b_num, b_den) = other.extract_i128_pair()?;
                if b_num == 0 { return Err(OverflowDetected::PrecisionLoss); }
                // (a/a_den) ÷ (b/b_den) = (a * b_den) / (a_den * |b|)
                let b_den_s = i128::try_from(b_den).map_err(|_| OverflowDetected::TierOverflow)?;
                let mut num = a_num.checked_mul(b_den_s).ok_or(OverflowDetected::TierOverflow)?;
                if b_num < 0 { num = num.checked_neg().ok_or(OverflowDetected::TierOverflow)?; }
                let den = a_den.checked_mul(b_num.unsigned_abs()).ok_or(OverflowDetected::TierOverflow)?;
                let g = gcd_unsigned(num.unsigned_abs(), den);
                Ok(Self::new(num / (g as i128), den / g))
            },
            Err(e) => Err(e),
        }
    }
    
    /// Negation with overflow detection for universal wrapper coordination
    pub fn try_negate(&self) -> Result<Self, OverflowDetected> {
        match &self.storage {
            RationalStorage::Tiny { num, den } => {
                if *num == i8::MIN {
                    Err(OverflowDetected::TierOverflow) // -128 → +128 overflows i8
                } else {
                    Ok(Self { 
                        storage: RationalStorage::Tiny { num: -num, den: *den }, 
                        metadata: self.metadata,
                    })
                }
            },
            RationalStorage::Small { num, den } => {
                if *num == i16::MIN {
                    Err(OverflowDetected::TierOverflow) // i16::MIN negation overflow
                } else {
                    Ok(Self { 
                        storage: RationalStorage::Small { num: -num, den: *den }, 
                        metadata: self.metadata,
                    })
                }
            },
            RationalStorage::Medium { num, den } => {
                if *num == i32::MIN {
                    Err(OverflowDetected::TierOverflow) // i32::MIN negation overflow
                } else {
                    Ok(Self { 
                        storage: RationalStorage::Medium { num: -num, den: *den }, 
                        metadata: self.metadata,
                    })
                }
            },
            RationalStorage::Large { num, den } => {
                if *num == i64::MIN {
                    Err(OverflowDetected::TierOverflow) // i64::MIN negation overflow
                } else {
                    Ok(Self { 
                        storage: RationalStorage::Large { num: -num, den: *den }, 
                        metadata: self.metadata,
                    })
                }
            },
            RationalStorage::Huge { num, den } => {
                if *num == i128::MIN {
                    Err(OverflowDetected::TierOverflow) // i128::MIN negation overflow
                } else {
                    Ok(Self { 
                        storage: RationalStorage::Huge { num: -num, den: *den }, 
                        metadata: self.metadata,
                    })
                }
            },
            #[cfg(not(feature = "embedded"))]
            RationalStorage::Massive { num, den } => {
                // For I256, use Neg trait (unary minus operator)
                Ok(Self {
                    storage: RationalStorage::Massive { num: -*num, den: *den },
                    metadata: self.metadata,
                })
            },
            RationalStorage::Ultra { num, den } => {
                // For I512, use Neg trait (unary minus operator)
                Ok(Self {
                    storage: RationalStorage::Ultra { num: -*num, den: *den },
                    metadata: self.metadata,
                })
            },
            #[cfg(feature = "infinite-precision")]
            RationalStorage::Infinite { num, den } => {
                Ok(Self { 
                    storage: RationalStorage::Infinite { num: -num, den: den.clone() }, 
                    metadata: self.metadata,
                })
            },
        }
    }
}

// ================================================================================================
// IMPLEMENTATION: Display and Comparison Traits
// ================================================================================================

impl fmt::Display for RationalNumber {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // PRODUCTION: Use native types for display (NO BigInt for tiers 1-7)
        match &self.storage {
            RationalStorage::Tiny { num, den } => {
                if *den == 1 { write!(f, "{}", num) } else { write!(f, "{}/{}", num, den) }
            },
            RationalStorage::Small { num, den } => {
                if *den == 1 { write!(f, "{}", num) } else { write!(f, "{}/{}", num, den) }
            },
            RationalStorage::Medium { num, den } => {
                if *den == 1 { write!(f, "{}", num) } else { write!(f, "{}/{}", num, den) }
            },
            RationalStorage::Large { num, den } => {
                if *den == 1 { write!(f, "{}", num) } else { write!(f, "{}/{}", num, den) }
            },
            RationalStorage::Huge { num, den } => {
                if *den == 1 { write!(f, "{}", num) } else { write!(f, "{}/{}", num, den) }
            },
            #[cfg(not(feature = "embedded"))]
            RationalStorage::Massive { num, den } => {
                let num_i128 = num.as_i128();
                let den_i128 = den.as_i128();
                if den_i128 == 1 { write!(f, "{}", num_i128) } else { write!(f, "{}/{}", num_i128, den_i128) }
            },
            RationalStorage::Ultra { num, den } => {
                let num_i128 = num.as_i128();
                let den_i128 = den.as_i128();
                if den_i128 == 1 { write!(f, "{}", num_i128) } else { write!(f, "{}/{}", num_i128, den_i128) }
            },
            #[cfg(feature = "infinite-precision")]
            RationalStorage::Infinite { num, den } => {
                if den.is_one() { write!(f, "{}", num) } else { write!(f, "{}/{}", num, den) }
            },
        }
    }
}

impl PartialEq for RationalNumber {
    fn eq(&self, other: &Self) -> bool {
        // PRODUCTION: Compare rationals using native types (NO BigInt for tiers 1-7)
        // Compare a/b == c/d by checking a*d == b*c
        match (&self.storage, &other.storage) {
            // Same-tier comparisons with appropriate intermediates to prevent overflow
            (RationalStorage::Tiny { num: a, den: ad }, RationalStorage::Tiny { num: b, den: bd }) => {
                // i8 * u8 fits in i16
                ((*a as i16) * (*bd as i16)) == ((*b as i16) * (*ad as i16))
            },
            (RationalStorage::Small { num: a, den: ad }, RationalStorage::Small { num: b, den: bd }) => {
                // i16 * u16 fits in i32
                ((*a as i32) * (*bd as i32)) == ((*b as i32) * (*ad as i32))
            },
            (RationalStorage::Medium { num: a, den: ad }, RationalStorage::Medium { num: b, den: bd }) => {
                // i32 * u32 fits in i64
                ((*a as i64) * (*bd as i64)) == ((*b as i64) * (*ad as i64))
            },
            (RationalStorage::Large { num: a, den: ad }, RationalStorage::Large { num: b, den: bd }) => {
                // i64 * u64 fits in i128
                ((*a as i128) * (*bd as i128)) == ((*b as i128) * (*ad as i128))
            },
            (RationalStorage::Huge { num: a, den: ad }, RationalStorage::Huge { num: b, den: bd }) => {
                // i128 * u128 may overflow - use checked arithmetic
                match (a.checked_mul(*bd as i128), b.checked_mul(*ad as i128)) {
                    (Some(lhs), Some(rhs)) => lhs == rhs,
                    _ => {
                        // Overflow: compare using cross-multiplication with I256
                        let lhs_i256 = I256::from_i128(*a) * I256::from_i128(*bd as i128);
                        let rhs_i256 = I256::from_i128(*b) * I256::from_i128(*ad as i128);
                        lhs_i256 == rhs_i256
                    }
                }
            },
            #[cfg(not(feature = "embedded"))]
            (RationalStorage::Massive { num: a, den: ad }, RationalStorage::Massive { num: b, den: bd }) => {
                // Cross-multiply: a/ad == b/bd iff a*bd == b*ad
                // Use I512 intermediates to prevent overflow
                let lhs = I512::from_i256(*a) * I512::from_i256(*bd);
                let rhs = I512::from_i256(*b) * I512::from_i256(*ad);
                lhs == rhs
            },
            (RationalStorage::Ultra { num: a, den: ad }, RationalStorage::Ultra { num: b, den: bd }) => {
                // Cross-multiply: a/ad == b/bd iff a*bd == b*ad
                // Use I1024 intermediates to prevent overflow
                let lhs = I1024::from_i512(*a) * I1024::from_i512(*bd);
                let rhs = I1024::from_i512(*b) * I1024::from_i512(*ad);
                lhs == rhs
            },
            #[cfg(feature = "infinite-precision")]
            (RationalStorage::Infinite { num: a, den: ad }, RationalStorage::Infinite { num: b, den: bd }) => {
                // Tier 8: BigInt is appropriate here
                (a * bd) == (b * ad)
            },
            // Mixed-tier comparisons: use tier-preserving extraction
            _ => {
                // Try to extract both as i128 for efficient comparison
                if let (Some((a_num, a_den)), Some((b_num, b_den))) =
                    (self.extract_native().try_as_i128(), other.extract_native().try_as_i128())
                {
                    // Both fit in i128: use i128 comparison with checked arithmetic
                    match (a_num.checked_mul(b_den as i128), b_num.checked_mul(a_den as i128)) {
                        (Some(lhs), Some(rhs)) => lhs == rhs,
                        _ => {
                            // Overflow to I256
                            let lhs = I256::from_i128(a_num) * I256::from_u128(b_den);
                            let rhs = I256::from_i128(b_num) * I256::from_u128(a_den);
                            lhs == rhs
                        }
                    }
                } else {
                    // At least one doesn't fit in i128: fallback to BigInt comparison
                    #[cfg(feature = "infinite-precision")]
                    {
                        let (a_num, a_den) = self.extract_exact_rational();
                        let (b_num, b_den) = other.extract_exact_rational();
                        (&a_num * &b_den) == (&b_num * &a_den)
                    }
                    #[cfg(not(feature = "infinite-precision"))]
                    {
                        // Without BigInt tier, values that don't fit are unequal
                        false
                    }
                }
            }
        }
    }
}

impl Eq for RationalNumber {}

impl PartialOrd for RationalNumber {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for RationalNumber {
    fn cmp(&self, other: &Self) -> Ordering {
        // PRODUCTION: Compare rationals using native types (NO BigInt for tiers 1-7)
        // Compare a/b with c/d by comparing a*d with b*c
        match (&self.storage, &other.storage) {
            // Same-tier comparisons with appropriate intermediates
            (RationalStorage::Tiny { num: a, den: ad }, RationalStorage::Tiny { num: b, den: bd }) => {
                let lhs = (*a as i16) * (*bd as i16);
                let rhs = (*b as i16) * (*ad as i16);
                lhs.cmp(&rhs)
            },
            (RationalStorage::Small { num: a, den: ad }, RationalStorage::Small { num: b, den: bd }) => {
                let lhs = (*a as i32) * (*bd as i32);
                let rhs = (*b as i32) * (*ad as i32);
                lhs.cmp(&rhs)
            },
            (RationalStorage::Medium { num: a, den: ad }, RationalStorage::Medium { num: b, den: bd }) => {
                let lhs = (*a as i64) * (*bd as i64);
                let rhs = (*b as i64) * (*ad as i64);
                lhs.cmp(&rhs)
            },
            (RationalStorage::Large { num: a, den: ad }, RationalStorage::Large { num: b, den: bd }) => {
                let lhs = (*a as i128) * (*bd as i128);
                let rhs = (*b as i128) * (*ad as i128);
                lhs.cmp(&rhs)
            },
            (RationalStorage::Huge { num: a, den: ad }, RationalStorage::Huge { num: b, den: bd }) => {
                match (a.checked_mul(*bd as i128), b.checked_mul(*ad as i128)) {
                    (Some(lhs), Some(rhs)) => lhs.cmp(&rhs),
                    _ => {
                        let lhs_i256 = I256::from_i128(*a) * I256::from_i128(*bd as i128);
                        let rhs_i256 = I256::from_i128(*b) * I256::from_i128(*ad as i128);
                        lhs_i256.cmp(&rhs_i256)
                    }
                }
            },
            #[cfg(not(feature = "embedded"))]
            (RationalStorage::Massive { num: a, den: ad }, RationalStorage::Massive { num: b, den: bd }) => {
                let lhs = *a * *ad;
                let rhs = *b * *bd;
                lhs.cmp(&rhs)
            },
            (RationalStorage::Ultra { num: a, den: ad }, RationalStorage::Ultra { num: b, den: bd }) => {
                let lhs = *a * *ad;
                let rhs = *b * *bd;
                lhs.cmp(&rhs)
            },
            #[cfg(feature = "infinite-precision")]
            (RationalStorage::Infinite { num: a, den: ad }, RationalStorage::Infinite { num: b, den: bd }) => {
                let lhs = a * bd;
                let rhs = b * ad;
                lhs.cmp(&rhs)
            },
            // Mixed-tier comparisons: use tier-preserving extraction
            _ => {
                // Try to extract both as i128 for efficient comparison
                if let (Some((a_num, a_den)), Some((b_num, b_den))) =
                    (self.extract_native().try_as_i128(), other.extract_native().try_as_i128())
                {
                    match (a_num.checked_mul(b_den as i128), b_num.checked_mul(a_den as i128)) {
                        (Some(lhs), Some(rhs)) => lhs.cmp(&rhs),
                        _ => {
                            let lhs = I256::from_i128(a_num) * I256::from_u128(b_den);
                            let rhs = I256::from_i128(b_num) * I256::from_u128(a_den);
                            lhs.cmp(&rhs)
                        }
                    }
                } else {
                    #[cfg(feature = "infinite-precision")]
                    {
                        let (a_num, a_den) = self.extract_exact_rational();
                        let (b_num, b_den) = other.extract_exact_rational();
                        let lhs = &a_num * &b_den;
                        let rhs = &b_num * &a_den;
                        lhs.cmp(&rhs)
                    }
                    #[cfg(not(feature = "infinite-precision"))]
                    {
                        // Without BigInt tier, compare what we can
                        Ordering::Equal // Fallback
                    }
                }
            }
        }
    }
}

// ================================================================================================
// IMPLEMENTATION: Arithmetic Operator Traits
// ================================================================================================

impl std::ops::Add for RationalNumber {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        self.try_add(&other).expect("RationalNumber addition overflow - consider using try_add for fallible operations")
    }
}

impl std::ops::Sub for RationalNumber {
    type Output = Self;

    fn sub(self, other: Self) -> Self {
        self.try_subtract(&other).expect("RationalNumber subtraction overflow - consider using try_subtract for fallible operations")
    }
}

impl std::ops::Mul for RationalNumber {
    type Output = Self;

    fn mul(self, other: Self) -> Self {
        self.try_multiply(&other).expect("RationalNumber multiplication overflow - consider using try_multiply for fallible operations")
    }
}

impl std::ops::Div for RationalNumber {
    type Output = Self;

    fn div(self, other: Self) -> Self {
        self.try_divide(&other).expect("RationalNumber division error - consider using try_divide for fallible operations")
    }
}

impl std::ops::Neg for RationalNumber {
    type Output = Self;

    fn neg(self) -> Self {
        self.try_negate().expect("RationalNumber negation error - tier overflow")
    }
}

// ================================================================================================
// IMPLEMENTATION: Numeric Traits (available when infinite-precision feature is enabled)
// ================================================================================================

#[cfg(feature = "infinite-precision")]
impl Zero for RationalNumber {
    fn zero() -> Self {
        Self::zero()
    }

    fn is_zero(&self) -> bool {
        self.is_zero()
    }
}

#[cfg(feature = "infinite-precision")]
impl One for RationalNumber {
    fn one() -> Self {
        Self::one()
    }

    fn is_one(&self) -> bool {
        self.is_one()
    }
}

impl RationalNumber {
    /// Create the rational number 0/1
    pub fn zero() -> Self {
        Self::new(0, 1)
    }

    /// Check if this rational number is zero (numerator == 0)
    pub fn is_zero(&self) -> bool {
        match &self.storage {
            RationalStorage::Tiny { num, .. } => *num == 0,
            RationalStorage::Small { num, .. } => *num == 0,
            RationalStorage::Medium { num, .. } => *num == 0,
            RationalStorage::Large { num, .. } => *num == 0,
            RationalStorage::Huge { num, .. } => *num == 0,
            #[cfg(not(feature = "embedded"))]
            RationalStorage::Massive { num, .. } => num.is_zero(),
            RationalStorage::Ultra { num, .. } => num.is_zero(),
            #[cfg(feature = "infinite-precision")]
            RationalStorage::Infinite { num, .. } => num == &BigInt::from(0),
        }
    }

    /// Check if this rational number represents an integer (denominator == 1)
    pub fn is_integer(&self) -> bool {
        match &self.storage {
            RationalStorage::Tiny { den, .. } => *den == 1,
            RationalStorage::Small { den, .. } => *den == 1,
            RationalStorage::Medium { den, .. } => *den == 1,
            RationalStorage::Large { den, .. } => *den == 1,
            RationalStorage::Huge { den, .. } => *den == 1,
            #[cfg(not(feature = "embedded"))]
            RationalStorage::Massive { den, .. } => den.as_i128() == 1,
            RationalStorage::Ultra { den, .. } => den.as_i128() == 1,
            #[cfg(feature = "infinite-precision")]
            RationalStorage::Infinite { den, .. } => den == &BigInt::from(1),
        }
    }

    /// Create the rational number 1/1
    pub fn one() -> Self {
        Self::new(1, 1)
    }

    /// Check if this rational number equals 1
    pub fn is_one(&self) -> bool {
        match &self.storage {
            RationalStorage::Tiny { num, den } => *num == 1 && *den == 1,
            RationalStorage::Small { num, den } => *num == 1 && *den == 1,
            RationalStorage::Medium { num, den } => *num == 1 && *den == 1,
            RationalStorage::Large { num, den } => *num == 1 && *den == 1,
            RationalStorage::Huge { num, den } => *num == 1 && *den == 1,
            #[cfg(not(feature = "embedded"))]
            RationalStorage::Massive { num, den } => num.as_i128() == 1 && den.as_i128() == 1,
            RationalStorage::Ultra { num, den } => num.as_i128() == 1 && den.as_i128() == 1,
            #[cfg(feature = "infinite-precision")]
            RationalStorage::Infinite { num, den } => num == &BigInt::from(1) && den == &BigInt::from(1),
        }
    }

    /// Integer exponentiation: (a/b)^n
    ///
    /// Supports negative exponents: (a/b)^(-n) = (b/a)^n
    /// **EXACT**: No precision loss — pure integer arithmetic
    pub fn pow(self, exponent: i32) -> Self {
        if exponent == 0 {
            return Self::one();
        }
        if exponent == 1 {
            return self;
        }

        // For negative exponents, reciprocal first
        let (base, exp) = if exponent < 0 {
            let recip = Self::new(
                if self.numerator_i128().unwrap_or(0) < 0 {
                    -(self.denominator_i128().unwrap_or(1) as i128)
                } else {
                    self.denominator_i128().unwrap_or(1) as i128
                },
                self.numerator_i128().unwrap_or(1).unsigned_abs() as u128,
            );
            (recip, (-exponent) as u32)
        } else {
            (self, exponent as u32)
        };

        // Exponentiation by squaring
        let mut result = Self::one();
        let mut b = base;
        let mut e = exp;
        while e > 0 {
            if e & 1 == 1 {
                result = result.try_multiply(&b).unwrap_or(result);
            }
            e >>= 1;
            if e > 0 {
                b = b.clone().try_multiply(&b).unwrap_or(b);
            }
        }
        result
    }
}

// ================================================================================================
// IMPLEMENTATION: Universal Tiered Arithmetic Integration (UGOD)
// ================================================================================================

impl UniversalTieredArithmetic for RationalNumber {
    type Error = OverflowDetected;

    /// Addition with UGOD overflow detection
    fn try_add(&self, other: &Self) -> Result<Self, Self::Error> {
        // Use existing rational arithmetic implementation with proper UGOD integration
        RationalNumber::try_add(self, other)
    }

    /// Subtraction with UGOD overflow detection
    fn try_subtract(&self, other: &Self) -> Result<Self, Self::Error> {
        RationalNumber::try_subtract(self, other)
    }

    /// Multiplication with UGOD overflow detection
    fn try_multiply(&self, other: &Self) -> Result<Self, Self::Error> {
        RationalNumber::try_multiply(self, other)
    }

    /// Division with UGOD overflow detection
    fn try_divide(&self, other: &Self) -> Result<Self, Self::Error> {
        RationalNumber::try_divide(self, other)
    }

    /// Negation with UGOD overflow detection
    fn try_negate(&self) -> Result<Self, Self::Error> {
        RationalNumber::try_negate(self)
    }

    /// Get current rational tier level
    fn current_tier(&self) -> u8 {
        self.tier_level()
    }

    /// Check if value can be promoted to target tier
    fn can_promote_to_tier(&self, tier: u8) -> bool {
        let current_tier = self.current_tier();
        let max_tier = Self::max_tier();
        tier <= max_tier && tier > current_tier
    }

    /// Promote to target tier within rational arithmetic hierarchy
    fn promote_to_tier(&self, tier: u8) -> Option<Self> {
        if !self.can_promote_to_tier(tier) {
            return None;
        }

        let parts = self.extract_native();

        // Tier 6 (Massive, I256): widen to I256 pair
        #[cfg(not(feature = "embedded"))]
        if tier == 6 {
            let (num, den) = parts.try_as_i256_pair()?;
            return Some(Self::from_i256_pair(num, den));
        }

        // Tier 7 (Ultra, I512): widen to I512 pair
        if tier == 7 {
            let (num, den) = parts.try_as_i512_pair()?;
            return Some(Self::from_i512_pair(num, den));
        }

        // Tiers 1-5: extract as i128/u128 and reconstruct (auto-selects optimal tier)
        let (num_i128, den_i128) = parts.try_as_i128()?;
        Some(Self::new(num_i128, den_i128))
    }

    /// Maximum tier supported by RationalNumber (all 7-8 tiers)
    fn max_tier() -> u8 {
        8 // Full rational arithmetic tier hierarchy including BigInt
    }

    /// RationalNumber domain type for UGOD coordination
    fn domain_type() -> UGODDomainType {
        UGODDomainType::Symbolic
    }

    /// Rational arithmetic symbolic tier mapping (1:1 mapping)
    fn symbolic_tier_mapping(symbolic_tier: u8) -> u8 {
        symbolic_tier // RationalNumber uses symbolic tiers directly
    }

    /// Check if RationalNumber can accommodate symbolic tier operations
    fn can_accommodate_symbolic_tier(&self, symbolic_tier: u8) -> bool {
        self.current_tier() >= symbolic_tier
    }

    /// Get deployment profile maximum tier limit for rational arithmetic
    fn max_tier_for_profile(profile: DeploymentProfile) -> u8 {
        match profile {
            DeploymentProfile::Realtime => 3,        // Minimal tiers for Q16.16
            DeploymentProfile::Compact => 4,         // Compact tiers for Q32.32
            DeploymentProfile::Embedded => 5,        // Conservative for embedded
            DeploymentProfile::Balanced => 7,        // Full tiers except BigInt
            DeploymentProfile::Scientific => 7,      // Full tiers for research
            DeploymentProfile::Custom => 8,          // Full tier hierarchy for custom
        }
    }
}

// ============================================================================
// UNIVERSAL TIER MAPPING: Symbolic (8 tiers) ↔ Universal (6 tiers)
// ============================================================================

/// Map symbolic internal tier (1-8) to universal tier (1-6)
///
/// Symbolic tiers use finer granularity (i8, i16 are separate tiers).
/// The universal system groups them into 6 tiers aligned with backing type sizes.
pub fn symbolic_to_universal_tier(symbolic_tier: u8) -> u8 {
    match symbolic_tier {
        1 | 2 => 1,  // Tiny(i8) + Small(i16) → Universal Tier 1 (small integers)
        3 => 2,       // Medium(i32) → Universal Tier 2
        4 => 3,       // Large(i64) → Universal Tier 3
        5 => 4,       // Huge(i128) → Universal Tier 4
        6 => 5,       // Massive(I256) → Universal Tier 5
        7 | 8 => 6,   // I512 + BigInt → Universal Tier 6
        _ => 6,       // Default to max
    }
}

/// Map universal tier (1-6) to the minimum symbolic tier needed
///
/// Used when creating a RationalNumber that must live at a specific universal tier.
pub fn universal_to_symbolic_tier(universal_tier: u8) -> u8 {
    match universal_tier {
        1 => 2,   // Universal 1 → Small(i16) minimum
        2 => 3,   // Universal 2 → Medium(i32)
        3 => 4,   // Universal 3 → Large(i64)
        4 => 5,   // Universal 4 → Huge(i128)
        5 => 6,   // Universal 5 → Massive(I256)
        6 => 7,   // Universal 6 → Ultra(I512)
        _ => 7,   // Default to Ultra
    }
}

// ═══ GCD helpers for extended-precision rational arithmetic ═══

/// GCD for I256 values using Euclidean algorithm (operates on absolute values)
#[cfg(not(feature = "embedded"))]
fn gcd_i256(a: I256, b: I256) -> I256 {
    let mut a = if a.is_negative() { -a } else { a };
    let mut b = if b.is_negative() { -b } else { b };
    let zero = I256::zero();
    while b != zero {
        let t = b;
        b = a % b;
        a = t;
    }
    if a == zero { I256::from_i128(1) } else { a }
}

/// GCD for I512 values using Euclidean algorithm (operates on absolute values)
#[cfg(not(feature = "embedded"))]
fn gcd_i512(a: I512, b: I512) -> I512 {
    let mut a = if a.is_negative() { -a } else { a };
    let mut b = if b.is_negative() { -b } else { b };
    let zero = I512::zero();
    while b != zero {
        let t = b;
        b = a % b;
        a = t;
    }
    if a == zero { I512::from_i128(1) } else { a }
}

/// GCD for I1024 values using Euclidean algorithm (operates on absolute values)
fn gcd_i1024(a: I1024, b: I1024) -> I1024 {
    let mut a = if (a.words[15] as i64) < 0 { -a } else { a };
    let mut b = if (b.words[15] as i64) < 0 { -b } else { b };
    let zero = I1024::zero();
    while b != zero {
        let t = b;
        b = a % b;
        a = t;
    }
    if a == zero { I1024::from_i128(1) } else { a }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_tier_assignment() {
        let tiny = RationalNumber::new(1, 3);
        let small = RationalNumber::new(355, 113);
        let large = RationalNumber::new(1_000_000, 3_000_000);

        assert_eq!(tiny.tier_level(), 1);
        assert_eq!(small.tier_level(), 2);
        assert_eq!(large.tier_level(), 3);
    }

    #[test]
    fn test_overflow_detection() {
        let large_a = RationalNumber::new(i8::MAX as i128, 1);
        let large_b = RationalNumber::new(i8::MAX as i128, 1);

        // UGOD promotion: i8*i8 overflows Tiny tier but promotes to Small (i16)
        // 127 * 127 = 16129, fits in i16
        let result = large_a.try_multiply(&large_b);
        assert!(result.is_ok(), "UGOD should promote i8 overflow to higher tier");
        let product = result.unwrap();
        assert_eq!(product.extract_native().try_as_i128().unwrap().0, 16129);

        // multiply_at_max_tier also works, at highest available tier
        let max_tier_result = large_a.multiply_at_max_tier(&large_b);
        assert!(max_tier_result.tier_level() >= 2);  // At least Small tier
    }
    
    #[test]
    fn test_exact_arithmetic() {
        let a = RationalNumber::new(1, 3);
        let b = RationalNumber::new(2, 6);
        let c = RationalNumber::new(1, 2);

        assert_eq!(a, b);  // 1/3 == 2/6
        assert!(a < c);    // 1/3 < 1/2
        assert!(c > a);    // 1/2 > 1/3

        assert!(!a.is_zero());
        assert!(!a.is_one());

        let one = RationalNumber::one();
        assert!(one.is_one());

        let zero = RationalNumber::zero();
        assert!(zero.is_zero());
    }

    #[test]
    fn test_pow() {
        let base = RationalNumber::new(2, 3);
        assert_eq!(base.clone().pow(0), RationalNumber::one());
        assert_eq!(base.clone().pow(1), base);
        assert_eq!(base.clone().pow(2), RationalNumber::new(4, 9));
        assert_eq!(base.clone().pow(3), RationalNumber::new(8, 27));

        // Negative powers
        assert_eq!(base.clone().pow(-1), RationalNumber::new(3, 2));
        assert_eq!(base.pow(-2), RationalNumber::new(9, 4));
    }
}