code wiki / (root) / nx_acceleration_test.nx

nx_acceleration_test.nx source

↩ module page · 86 lines · 3555 B

1// nx_acceleration_test.nx -- acceleration vector ops + Newton's 2// second-law roundtrip composition with nx_force. 3// 4// Closed-form invariants (Q14 m/s² unless noted): 5// (a) Zero magnitude == 0. 6// (b) Earth gravity = (0, 0, -9.80665 m/s²) → fz_q14 ≈ -160665. 7// (c) Gravity magnitude ≈ 160665 Q14 (= 9.80665 m/s² × Q14_ONE). 8// (d) 3-4-5 triangle in acceleration space. 9// (e) Add: (1, 2, 3) + (4, 5, 6) = (5, 7, 9). 10// (f) Scale by 2.0 (32768 Q14) doubles each component. 11// (g) Newton's second law roundtrip: gravity × 1 kg ≈ matches 12// nx_force_gravity_on_kg(1 kg). Allows ±2 Q14 tolerance for 13// the two-step Q14 chain (mass×accel/Q14_ONE). 14// (h) F = ma for 5 kg at acceleration (0, 0, -10 m/s²) → F ≈ (0, 0, -50 N). 15// (i) Verdict name lookup non-NULL. 16// 17// expect_exit: 0 18// license_tier: ORIGINAL 19 20import "nx_syscalls.nx" 21import "nx_force.nx" 22import "nx_acceleration.nx" 23 24const Q14: i64 = 16384 25 26func near(actual: i64, expected: i64, tol: i64) -> i64 { 27 let diff: i64 = actual - expected 28 if diff < 0 { if -diff <= tol { return 1 } } 29 if diff >= 0 { if diff <= tol { return 1 } } 30 return 0 31} 32 33func main() -> i64 { 34 // --- (a) Zero --- 35 let a0: *NxAcceleration = nx_acceleration_zero() 36 if nx_acceleration_magnitude_q14(a0) != 0 { return 10 } 37 38 // --- (b) Gravity --- 39 let g: *NxAcceleration = nx_acceleration_g_earth() 40 if g.ax_q14 != 0 { return 20 } 41 if g.ay_q14 != 0 { return 21 } 42 if near(g.az_q14, -160665, 2) != 1 { return 22 } 43 44 // --- (c) Gravity magnitude --- 45 if near(nx_acceleration_magnitude_q14(g), 160665, 2) != 1 { return 30 } 46 47 // --- (d) 3-4-5 --- 48 let a345: *NxAcceleration = nx_acceleration_new(3 * Q14, 4 * Q14, 0) 49 if near(nx_acceleration_magnitude_q14(a345), 5 * Q14, 1) != 1 { return 40 } 50 51 // --- (e) Add --- 52 let pa: *NxAcceleration = nx_acceleration_new(1, 2, 3) 53 let pb: *NxAcceleration = nx_acceleration_new(4, 5, 6) 54 let psum: *NxAcceleration = nx_acceleration_add(pa, pb) 55 if psum.ax_q14 != 5 { return 50 } 56 if psum.ay_q14 != 7 { return 51 } 57 if psum.az_q14 != 9 { return 52 } 58 59 // --- (f) Scale by 2 --- 60 let v: *NxAcceleration = nx_acceleration_new(Q14, 2 * Q14, 3 * Q14) 61 let v2: *NxAcceleration = nx_acceleration_scale(v, 2 * Q14) 62 if near(v2.ax_q14, 2 * Q14, 1) != 1 { return 60 } 63 if near(v2.ay_q14, 4 * Q14, 1) != 1 { return 61 } 64 if near(v2.az_q14, 6 * Q14, 1) != 1 { return 62 } 65 66 // --- (g) Newton's second law roundtrip: gravity × 1 kg ≈ direct --- 67 // F_direct = nx_force_gravity_on_kg(1 kg) -> fz ≈ -160665 68 // F_newton = nx_force_from_mass_accel(1 kg, g) 69 let f_direct: *NxForce = nx_force_gravity_on_kg(Q14) // 1 kg in Q14 70 let f_newton: *NxForce = nx_force_from_mass_accel(Q14, g) 71 if near(f_newton.fz_q14, f_direct.fz_q14, 2) != 1 { return 70 } 72 if f_newton.fx_q14 != 0 { return 71 } 73 if f_newton.fy_q14 != 0 { return 72 } 74 75 // --- (h) F = ma at 5 kg, accel (0, 0, -10 m/s²) --- 76 // Expected F = (0, 0, -50 N) -> fz_q14 ≈ -50 * 16384 = -819200 77 let a_neg10: *NxAcceleration = nx_acceleration_new(0, 0, 0 - (10 * Q14)) 78 let f5kg: *NxForce = nx_force_from_mass_accel(5 * Q14, a_neg10) 79 if near(f5kg.fz_q14, -50 * Q14, 2) != 1 { return 80 } 80 81 // --- (i) Verdict names --- 82 if (nx_acceleration_verdict_name(NX_ACCEL_OK) as i64) == 0 { return 90 } 83 if (nx_acceleration_verdict_name(NX_ACCEL_ERR_NULL_INPUT) as i64) == 0 { return 91 } 84 85 return 0 86}