code wiki / (root) / nx_phi.nx

nx_phi.nx source

↩ module page · 103 lines · 3886 B

1// nx_phi.nx -- golden ratio + Fibonacci primitives for S-class anatomy canon. 2// 3// phi = (1 + sqrt(5)) / 2 = 1.6180339887... 4// In Q10 (1024 = 1.0): phi = 1657 1/phi = 633 phi^2 = 2681 1/phi^2 = 391 5// 6// Every canonical body ratio (bust:waist / hip:waist / total:navel / 7// shoulder:waist / face_height:face_width / pupil-pair:pupil-mouth) 8// converges to a phi-power. Substrate scores measured ratios by 9// their Q10 distance to the nearest phi-power. 10// 11// Fibonacci sequence underwrites: limb-segment ratios, finger 12// phalanges, spiral floral patterns -- ratio of successive terms 13// converges to phi. Useful for proportional CHECK on limb segments 14// AND for procedurally generating phi-progression seeds. 15// 16// nx_safety_envelope: 17// intended_use: "Golden ratio (phi) constant + progression 18// primitives -- visual/audio aesthetic 19// composition + procedural seeding" 20// sil_target: SIL1 21// asil_target: QM 22// dal_target: NONE 23// evidence: [Q14_fixed_point_phi_constant, 24// bit_equal_reproducible, 25// no_FP] 26// hazard_register: [bug-tape-phi-precision-drift-on-iteration, 27// bug-tape-fibonacci-overflow-i64] 28// residual_risk: "Q14 phi has ~5 decimal digits accuracy. 29// Substrate cannot exceed without higher 30// precision (Q24 / i128 queued)." 31// verdict: NOT_YET_EVALUATED 32 33import "nx_syscalls.nx" 34 35// Q10 fixed-point constants. 36const NX_Q10: i64 = 1024 37const NX_PHI_Q10: i64 = 1657 // 1.618 * 1024 38const NX_PHI_INV_Q10: i64 = 633 // 0.618 * 1024 39const NX_PHI_SQ_Q10: i64 = 2681 // 2.618 * 1024 40const NX_PHI_INV_SQ_Q10: i64 = 391 // 0.382 * 1024 41const NX_PHI_TOLERANCE_Q10: i64 = 102 // 0.1 default band 42 43// Sealed verdict for phi-distance check. 44const NX_PHI_BAND_WITHIN: i64 = 0 45const NX_PHI_BAND_BELOW: i64 = 1 46const NX_PHI_BAND_ABOVE: i64 = 2 47 48// Compute ratio a/b in Q10. Caller guarantees b > 0. 49func nx_ratio_q10(a: i64, b: i64) -> i64 { 50 if b == 0 { return 0 } 51 return (a * NX_Q10) / b 52} 53 54// Absolute distance (Q10) between two ratios. 55func nx_q10_abs_distance(a_q10: i64, b_q10: i64) -> i64 { 56 if a_q10 >= b_q10 { return a_q10 - b_q10 } 57 return b_q10 - a_q10 58} 59 60// Distance from a measured ratio to a canon target (Q10). 61func nx_distance_to_target(ratio_q10: i64, target_q10: i64) -> i64 { 62 return nx_q10_abs_distance(ratio_q10, target_q10) 63} 64 65// Returns 0 if ratio is within phi-band, 1 if below, 2 if above. 66func nx_phi_band_check(ratio_q10: i64, tolerance_q10: i64) -> i64 { 67 let dist: i64 = nx_q10_abs_distance(ratio_q10, NX_PHI_Q10) 68 if dist <= tolerance_q10 { return NX_PHI_BAND_WITHIN } 69 if ratio_q10 < NX_PHI_Q10 { return NX_PHI_BAND_BELOW } 70 return NX_PHI_BAND_ABOVE 71} 72 73// Score Q10 quality of a ratio against a target: 1024 = perfect, 74// 0 = totally off. Linear falloff over [target-window, target+window]. 75func nx_proportion_score_q10(ratio_q10: i64, target_q10: i64, 76 tolerance_q10: i64) -> i64 { 77 let dist: i64 = nx_q10_abs_distance(ratio_q10, target_q10) 78 if dist >= tolerance_q10 { return 0 } 79 return NX_Q10 - ((dist * NX_Q10) / tolerance_q10) 80} 81 82// Fibonacci nth term. Q10-safe up to F(45) since i64 is far larger. 83func nx_fib(n: i64) -> i64 { 84 if n <= 0 { return 0 } 85 if n == 1 { return 1 } 86 var a: i64 = 0 87 var b: i64 = 1 88 var i: i64 = 2 89 while i <= n { 90 let next: i64 = a + b 91 a = b 92 b = next 93 i = i + 1 94 } 95 return b 96} 97 98// Ratio F(n+1) / F(n) in Q10. Converges to phi for n >= 7. 99func nx_fib_ratio_q10(n: i64) -> i64 { 100 let fn: i64 = nx_fib(n) 101 let fn1: i64 = nx_fib(n + 1) 102 return nx_ratio_q10(fn1, fn) 103}