code wiki / (root) / nx_itrig.nx

nx_itrig.nx source

↩ module page · 28 lines · 1597 B

1// nx_itrig.nx -- Provides integer trigonometric functions using 5th-order Taylor series approximations for angles in rad*4096 units. 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 6// THE FIXED-POINT UNIT THIS WHOLE LIBRARY IS DEFINED IN -- the fx4096 of the header line above, the same 7// 4096 as in the function names. It arrived here as `IT_MAGIC_4096`, auto-hoisted, wedged BETWEEN the two 8// halves of this file's own header comment, and named after its own value: a name that restates the number 9// is less greppable than the bare literal was, and it told the next reader nothing about what the quantity 10// IS. It is the angle-and-value scale, so it is named for that. 11const IT_FX: i64 = 4096 12const IT_PI: i64 = 12868 13const IT_PI2: i64 = 6434 14 15func it_sin4096(a0: i64) -> i64 { 16 var a: i64 = a0 % (2 * IT_PI) 17 if a < 0 { a = a + 2 * IT_PI } 18 var sign: i64 = 1 19 if a > IT_PI { a = a - IT_PI; sign = 0 - 1 } 20 if a > IT_PI2 { a = IT_PI - a } 21 let a2: i64 = a * a / IT_FX 22 let a3: i64 = a2 * a / IT_FX 23 let a5: i64 = a3 * a2 / IT_FX 24 var s: i64 = a - a3 / 6 + a5 / 120 25 if s > IT_FX { s = IT_FX } 26 return s * sign 27} 28func it_cos4096(a: i64) -> i64 { return it_sin4096(a + IT_PI2) }