nx_icbrt_test.nx source
↩ module page · 84 lines · 2530 B
1// nx_icbrt_test.nx -- verify integer cube root + Q14 wrapper.
2//
3// Round-trip check: nx_icbrt of nx_cube(k) returns k exactly.
4//
5// expect_exit: 0
6// license_tier: ORIGINAL
7
8import "nx_syscalls.nx"
9import "nx_icbrt.nx"
10import "nx_cube.nx"
11
12const Q14: i64 = 16384
13
14func main() -> i64 {
15 // --- (a) Edge cases ---
16 if nx_icbrt(-7) != 0 { return 10 }
17 if nx_icbrt(0) != 0 { return 11 }
18 if nx_icbrt(1) != 1 { return 12 }
19 if nx_icbrt(7) != 1 { return 13 } // floor(cbrt(7)) = 1
20 if nx_icbrt(8) != 2 { return 14 }
21 if nx_icbrt(26) != 2 { return 15 }
22 if nx_icbrt(27) != 3 { return 16 }
23
24 // --- (b) Perfect cubes round-trip via nx_cube ---
25 var k: i64 = 0
26 while k <= 100 {
27 let cubed: i64 = nx_cube(k)
28 if nx_icbrt(cubed) != k { return 20 }
29 k = k + 1
30 }
31
32 // --- (c) Boundary (k³ - 1) = k - 1 ---
33 var j: i64 = 2
34 while j <= 50 {
35 let c: i64 = nx_cube(j)
36 if nx_icbrt(c - 1) != j - 1 { return 30 }
37 if nx_icbrt(c) != j { return 31 }
38 if nx_icbrt(c + 1) != j { return 32 }
39 j = j + 1
40 }
41
42 // --- (d) Larger cubes ---
43 if nx_icbrt(1000) != 10 { return 40 }
44 if nx_icbrt(999) != 9 { return 41 }
45 if nx_icbrt(1000000) != 100 { return 42 }
46 if nx_icbrt(999999) != 99 { return 43 }
47
48 // --- (e) Q14 wrapper: cube root of 1.0 is 1.0 ---
49 if nx_icbrt_q14(0) != 0 { return 50 }
50 let r1: i64 = nx_icbrt_q14(Q14) // cbrt(1.0) ≈ 1.0
51 if r1 < Q14 - 50 { return 51 }
52 if r1 > Q14 + 50 { return 52 }
53
54 // --- (f) Q14: cbrt(8) = 2 (within +/-1 LSB Q14 = 1/16384 mm) ---
55 let r8: i64 = nx_icbrt_q14(8 * Q14)
56 let expect8: i64 = 2 * Q14
57 if r8 < expect8 - 50 { return 60 }
58 if r8 > expect8 + 50 { return 61 }
59
60 // --- (g) Q14: cbrt(27) = 3 ---
61 let r27: i64 = nx_icbrt_q14(27 * Q14)
62 let expect27: i64 = 3 * Q14
63 if r27 < expect27 - 50 { return 70 }
64 if r27 > expect27 + 50 { return 71 }
65
66 // --- (h) Q14: cbrt(0.125) = 0.5 ---
67 // 0.125 in Q14 = Q14 / 8 = 2048
68 let r_eighth: i64 = nx_icbrt_q14(Q14 / 8)
69 let expect_half: i64 = Q14 / 2
70 if r_eighth < expect_half - 50 { return 80 }
71 if r_eighth > expect_half + 50 { return 81 }
72
73 // --- (i) Monotonicity: larger input -> larger output ---
74 var prev: i64 = 0
75 var i: i64 = 0
76 while i <= 20 {
77 let r: i64 = nx_icbrt_q14(i * Q14)
78 if r < prev { return 90 }
79 prev = r
80 i = i + 1
81 }
82
83 return 0
84}