code wiki / _hdl_build / nx_research_trig_gate.nx

nx_research_trig_gate.nx source

↩ module page · 68 lines · 4769 B

1// nx_research_trig_gate.nx -- sovereign fixed-point trigonometry (sin/cos), the trig counterpart to isqrt and the 2// missing piece for NONLINEAR dynamics (pendulum a=-sin(theta)). Method: range-reduce the angle to [0, pi/2] via 3// the standard symmetries, then a Taylor series sin(x)=x - x^3/3! + x^5/5! - ... ; cos(x)=sin(x+pi/2). Verified 4// against exact known values AND the Pythagorean identity sin^2+cos^2=1 (the self-check). Fixed-point scale 5// SC=1e6 (1.0 == 1000000); angles in scaled radians. No float, deterministic. GREEN iff 6/6. license_tier: ORIGINAL 6import "nx_syscalls.nx" 7 8func g_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 9func g_n(v: i64) -> i64 { var m: i64=v; if m<0{g_w("-");m=0-m} let t:*u8=sys_mmap(24); var k:i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i:i64=0; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1}; sys_write(1,o,k); return 0 } 10func g_row(id: *u8, ok: i64, pass: *i64) -> i64 { g_w(" "); g_w(id); g_w(": "); if ok==1 { g_w("OK\n"); pass[0]=pass[0]+1 } else { g_w("FAIL\n") } return 0 } 11func iabs(x: i64) -> i64 { if x<0 { return 0-x } return x } 12 13// constants for SC=1e6 14func sin_fixed(Xin: i64, SC: i64) -> i64 { 15 let PI: i64=3141593; let TWO_PI: i64=6283185; let HALF: i64=1570796 16 var sign: i64=1; var X: i64=Xin 17 if X<0 { X=0-X; sign=0-1 } 18 X = X % TWO_PI 19 if X > PI { X = TWO_PI - X; sign = 0-sign } 20 if X > HALF { X = PI - X } 21 let x2: i64 = X*X/SC 22 var term: i64=X; var sum: i64=X; var k: i64=1 23 while k<=8 { term = (0-term)*x2/SC/((2*k)*(2*k+1)); sum=sum+term; k=k+1 } 24 return sum*sign 25} 26func cos_fixed(X: i64, SC: i64) -> i64 { return sin_fixed(X+1570796, SC) } 27func near(a: i64, b: i64, tol: i64) -> i64 { if iabs(a-b)<=tol { return 1 } return 0 } 28 29func main() -> i64 { 30 let pass: *i64 = sys_mmap(8) as *i64; pass[0]=0 31 g_w("=== NX-RESEARCH-TRIG GATE (sovereign fixed-point sin/cos: Taylor + range reduction, Pythagorean-verified) ===\n") 32 let SC: i64=1000000 33 let PI: i64=3141593; let HALF: i64=1570796 34 let P6: i64=523599; let P4: i64=785398; let P3: i64=1047198; let T3: i64=2094395 // pi/6, pi/4, pi/3, 2pi/3 35 36 let s0: i64=sin_fixed(0,SC); let s6: i64=sin_fixed(P6,SC); let s4: i64=sin_fixed(P4,SC); let s3: i64=sin_fixed(P3,SC); let s2: i64=sin_fixed(HALF,SC) 37 let sPI: i64=sin_fixed(PI,SC); let sT3: i64=sin_fixed(T3,SC); let sNeg6: i64=sin_fixed(0-P6,SC) 38 let c0: i64=cos_fixed(0,SC); let c2: i64=cos_fixed(HALF,SC); let cPI: i64=cos_fixed(PI,SC) 39 40 g_w(" sin: 0="); g_n(s0); g_w(" pi/6="); g_n(s6); g_w(" pi/4="); g_n(s4); g_w(" pi/3="); g_n(s3); g_w(" pi/2="); g_n(s2); g_w("\n") 41 g_w(" sin: pi="); g_n(sPI); g_w(" 2pi/3="); g_n(sT3); g_w(" -pi/6="); g_n(sNeg6); g_w("\n") 42 g_w(" cos: 0="); g_n(c0); g_w(" pi/2="); g_n(c2); g_w(" pi="); g_n(cPI); g_w("\n") 43 44 // Pythagorean identity over several angles 45 var pyth_ok: i64=1; let angs: *i64=sys_mmap(8*8) as *i64; angs[0]=0; angs[1]=P6; angs[2]=P4; angs[3]=P3; angs[4]=1000000; angs[5]=2000000 46 var ai: i64=0; var worst: i64=0 47 while ai<6 { let s: i64=sin_fixed(angs[ai],SC); let c: i64=cos_fixed(angs[ai],SC); let sumsq: i64=(s*s+c*c)/SC; let e: i64=iabs(sumsq-SC); if e>worst { worst=e } if e>2000 { pyth_ok=0 } ai=ai+1 } 48 g_w(" pythagorean sin^2+cos^2: worst |dev| from SC="); g_n(worst); g_w(" (tol 2000 = 0.2%)\n") 49 50 // small-angle: sin(x)~x for small x 51 let sm: i64=sin_fixed(10000,SC) // 0.01 rad 52 let small_ok: i64=near(sm,10000,20) 53 54 // liar-kill: a wrong sin (claim sin(pi/6)=0.6) breaks the Pythagorean identity with the true cos 55 let cf6: i64=cos_fixed(P6,SC); let fake: i64=600000; let badsum: i64=(fake*fake+cf6*cf6)/SC 56 let liar: i64=(iabs(badsum-SC) > 2000) as i64 57 58 g_row("KNOWN sin: sin(0)=0, sin(pi/6)=0.5, sin(pi/2)=1 (within tol)" as *u8, near(s0,0,50)*near(s6,500000,1500)*near(s2,1000000,1500), pass) 59 g_row("KNOWN cos: cos(0)=1, cos(pi/2)=0, cos(pi)=-1 (within tol)" as *u8, near(c0,1000000,1500)*near(c2,0,1500)*near(cPI,0-1000000,1500), pass) 60 g_row("RANGE REDUCTION: sin(pi)=0, sin(2pi/3)=sin(pi/3)=0.866, sin(-pi/6)=-0.5 (symmetry+fold)" as *u8, near(sPI,0,1500)*near(sT3,866025,1500)*near(sNeg6,0-500000,1500), pass) 61 g_row("PYTHAGOREAN VERIFIER: sin^2+cos^2 == 1 across 6 angles (worst dev < 0.2%)" as *u8, pyth_ok, pass) 62 g_row("SMALL-ANGLE: sin(x) ~ x for small x (the harmonic limit; sin(0.01)~0.01)" as *u8, small_ok, pass) 63 g_row("LIAR-KILL: a wrong sin value breaks the Pythagorean identity with the true cos" as *u8, liar, pass) 64 65 g_w("RESEARCH-TRIG-GATE rows=6 pass="); g_n(pass[0]) 66 if pass[0]==6 { g_w(" verdict=GREEN\n"); sys_exit(0); return 0 } 67 g_w(" verdict=RED\n"); sys_exit(1); return 1 68}