nx_exploop_test.nx source
↩ module page · 25 lines · 830 B
1// nx_exploop_test.nx -- does nx_f32_exp misbehave in a loop / on far-negative inputs (as softmax feeds)?
2// license_tier: ORIGINAL
3import "nx_syscalls.nx"
4import "nx_f32.nx"
5import "nx_f32_div.nx"
6import "nx_f32_cvt.nx"
7import "nx_f32_exp.nx"
8
9func main() -> i64 {
10 let five: i64 = nx_i32_to_f32(5)
11 var sum: i64 = 0
12 var i: i64 = 0
13 while i < 200 {
14 let x: i64 = nx_f32_neg(nx_f32_div(nx_i32_to_f32(i), five)) // -i/5 : 0 .. -39.8
15 let e: i64 = nx_f32_exp(x)
16 sum = nx_f32_add(sum, e)
17 i = i + 1
18 }
19 if nx_f32_is_nan(sum) == 1 { return 1 }
20 if nx_f32_is_inf(sum) == 1 { return 2 }
21 // Σ e^(-i/5) ~= 1/(1-e^-0.2) ~= 5.52 ; sanity band [1,20]
22 if nx_f32_lt(sum, nx_i32_to_f32(1)) == 1 { return 3 }
23 if nx_f32_lt(nx_i32_to_f32(20), sum) == 1 { return 4 }
24 return 0
25}