code wiki / (root) / nx_itrig.nx

nx_itrig.nx source

↩ module page · 22 lines · 1017 B

1const IT_MAGIC_4096: i64 = 4096 2// nx_itrig.nx -- SHARED integer trig (extracted from the proven nx_wasm_mineworld implementation per the 3// reuse law: skeleton/animation/robotics/regions all need rotations; nobody re-derives Taylor coefficients). 4// Angle unit = rad*4096 (PI4096=12868, PI2_4096=6434); values fx4096. 5th-order Taylor folded to [-PI/2,PI/2], 5// max error ~0.6% (gate-proven in the mineworld tick gate T0). ALL INTEGER. license_tier: ORIGINAL 6const IT_PI: i64 = 12868 7const IT_PI2: i64 = 6434 8 9func it_sin4096(a0: i64) -> i64 { 10 var a: i64 = a0 % (2 * IT_PI) 11 if a < 0 { a = a + 2 * IT_PI } 12 var sign: i64 = 1 13 if a > IT_PI { a = a - IT_PI; sign = 0 - 1 } 14 if a > IT_PI2 { a = IT_PI - a } 15 let a2: i64 = a * a / IT_MAGIC_4096 16 let a3: i64 = a2 * a / IT_MAGIC_4096 17 let a5: i64 = a3 * a2 / IT_MAGIC_4096 18 var s: i64 = a - a3 / 6 + a5 / 120 19 if s > IT_MAGIC_4096 { s = IT_MAGIC_4096 } 20 return s * sign 21} 22func it_cos4096(a: i64) -> i64 { return it_sin4096(a + IT_PI2) }