fixed_analytics 2.0.1

Fixed-point mathematical functions. Accurate, deterministic, and panic free.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Tests for exponential and logarithmic functions

#[cfg(test)]
#[allow(clippy::unwrap_used, reason = "test code uses unwrap for conciseness")]
mod tests {
    use fixed::types::I16F16;
    use fixed_analytics::{exp, ln, log2, log10, pow2};

    const TOLERANCE: f32 = 0.15;

    fn approx_eq(a: I16F16, b: f32, tolerance: f32) -> bool {
        (a.to_num::<f32>() - b).abs() < tolerance
    }

    #[test]
    fn exp_special_values() {
        assert!(approx_eq(exp(I16F16::ZERO), 1.0, TOLERANCE));
        assert!(approx_eq(exp(I16F16::ONE), core::f32::consts::E, TOLERANCE));
    }

    #[test]
    fn exp_negative() {
        let result = exp(-I16F16::ONE);
        let expected = 1.0 / core::f32::consts::E;
        assert!(approx_eq(result, expected, TOLERANCE));
    }

    #[test]
    fn ln_special_values() {
        assert!(approx_eq(ln(I16F16::ONE).unwrap(), 0.0, TOLERANCE));
        let ln_e: f32 = ln(I16F16::E).unwrap().to_num();
        assert!((ln_e - 1.0).abs() < 0.25, "ln(e) = {ln_e}, expected ~1.0");
    }

    #[test]
    fn ln_domain_check() {
        assert!(ln(I16F16::ZERO).is_err());
        assert!(ln(I16F16::from_num(-1.0)).is_err());
        assert!(ln(I16F16::from_num(0.5)).is_ok());
    }

    #[test]
    fn log2_powers_of_two() {
        assert!(approx_eq(
            log2(I16F16::from_num(1.0)).unwrap(),
            0.0,
            TOLERANCE
        ));
        assert!(approx_eq(log2(I16F16::from_num(2.0)).unwrap(), 1.0, 0.25));
        assert!(approx_eq(log2(I16F16::from_num(4.0)).unwrap(), 2.0, 0.3));
        assert!(approx_eq(log2(I16F16::from_num(8.0)).unwrap(), 3.0, 0.4));
    }

    #[test]
    fn log10_powers_of_ten() {
        assert!(approx_eq(
            log10(I16F16::from_num(1.0)).unwrap(),
            0.0,
            TOLERANCE
        ));
        assert!(approx_eq(log10(I16F16::from_num(10.0)).unwrap(), 1.0, 0.25));
        assert!(approx_eq(
            log10(I16F16::from_num(100.0)).unwrap(),
            2.0,
            0.35
        ));
    }

    #[test]
    fn exp_ln_inverse() {
        // exp(ln(x)) ≈ x (with limited precision due to CORDIC)
        for i in 1..5 {
            let x = I16F16::from_num(i);
            let result = exp(ln(x).unwrap());
            let x_f32: f32 = x.to_num();
            let result_f32: f32 = result.to_num();
            assert!(
                (result_f32 - x_f32).abs() < 0.5,
                "exp(ln({x_f32})) = {result_f32}, expected {x_f32}"
            );
        }
    }

    #[test]
    fn pow2_log2_inverse() {
        // pow2(log2(x)) ≈ x for positive x
        for i in 1..8 {
            let x = I16F16::from_num(i);
            let result = pow2(log2(x).unwrap());
            let x_f32: f32 = x.to_num();
            let result_f32: f32 = result.to_num();
            assert!(
                (result_f32 - x_f32).abs() < 0.6,
                "pow2(log2({x_f32})) = {result_f32}, expected {x_f32}"
            );
        }
    }

    #[test]
    fn ln_exp_inverse() {
        // ln(exp(x)) ≈ x for small x (where exp doesn't overflow)
        for i in -3..=2 {
            let x = I16F16::from_num(i);
            let result = ln(exp(x));
            assert!(result.is_ok());
            let x_f32: f32 = x.to_num();
            let result_f32: f32 = result.unwrap().to_num();
            assert!(
                (result_f32 - x_f32).abs() < 0.3,
                "ln(exp({x_f32})) = {result_f32}, expected {x_f32}"
            );
        }
    }

    #[test]
    fn ln_near_zero() {
        // ln approaches -infinity as x approaches 0
        // Test small positive values
        let small = I16F16::from_num(0.01);
        let result = ln(small);
        assert!(result.is_ok());
        let val: f32 = result.unwrap().to_num();
        // ln(0.01) ≈ -4.605
        assert!(val < -3.0, "ln(0.01) = {val}, expected < -3.0");

        // Very small value
        let very_small = I16F16::from_num(0.001);
        let result2 = ln(very_small);
        assert!(result2.is_ok());
        let val2: f32 = result2.unwrap().to_num();
        // ln(0.001) ≈ -6.908
        assert!(val2 < -5.0, "ln(0.001) = {val2}, expected < -5.0");
    }

    #[test]
    fn ln_at_one() {
        // ln(1) should be exactly 0
        let result = ln(I16F16::ONE);
        assert!(result.is_ok());
        let val: f32 = result.unwrap().to_num();
        assert!(val.abs() < 0.01, "ln(1) = {val}, expected 0");
    }

    #[test]
    fn exp_large_negative() {
        // exp of large negative values should approach 0
        let neg_large = I16F16::from_num(-10.0);
        let result: f32 = exp(neg_large).to_num();
        // exp(-10) ≈ 0.0000454
        assert!(
            (0.0..0.01).contains(&result),
            "exp(-10) = {result}, expected ~0"
        );
    }

    #[test]
    fn exp_zero() {
        // exp(0) should be exactly 1
        let result: f32 = exp(I16F16::ZERO).to_num();
        assert!(
            (result - 1.0).abs() < 0.001,
            "exp(0) = {result}, expected 1"
        );
    }

    #[test]
    fn log2_at_one() {
        // log2(1) should be exactly 0
        let result = log2(I16F16::ONE);
        assert!(result.is_ok());
        let val: f32 = result.unwrap().to_num();
        assert!(val.abs() < 0.01, "log2(1) = {val}, expected 0");
    }

    #[test]
    fn log10_at_one() {
        // log10(1) should be exactly 0
        let result = log10(I16F16::ONE);
        assert!(result.is_ok());
        let val: f32 = result.unwrap().to_num();
        assert!(val.abs() < 0.01, "log10(1) = {val}, expected 0");
    }

    #[test]
    fn exp_large_positive() {
        // exp of large positive values should exercise the argument reduction loop
        let large = I16F16::from_num(5.0);
        let result: f32 = exp(large).to_num();
        // exp(5) ≈ 148.41
        assert!(
            result > 100.0 && result < 200.0,
            "exp(5) = {result}, expected ~148"
        );

        // Test even larger value to ensure multiple reduction iterations
        let larger = I16F16::from_num(8.0);
        let result2: f32 = exp(larger).to_num();
        // exp(8) ≈ 2981
        assert!(result2 > 2000.0, "exp(8) = {result2}, expected > 2000");
    }

    #[test]
    fn ln_large_values() {
        // ln of large values should exercise the argument reduction loop
        let large = I16F16::from_num(1000.0);
        let result = ln(large);
        assert!(result.is_ok());
        let val: f32 = result.unwrap().to_num();
        // ln(1000) ≈ 6.9
        assert!((val - 6.9).abs() < 0.5, "ln(1000) = {val}, expected ~6.9");

        // Test even larger value
        let larger = I16F16::from_num(10000.0);
        let result2 = ln(larger);
        assert!(result2.is_ok());
        let val2: f32 = result2.unwrap().to_num();
        // ln(10000) ≈ 9.2
        assert!(
            (val2 - 9.2).abs() < 0.5,
            "ln(10000) = {val2}, expected ~9.2"
        );
    }

    #[test]
    fn ln_very_small_values() {
        // Test very small values to ensure the multiply-by-2 loop iterates
        let tiny = I16F16::from_num(0.0001);
        let result = ln(tiny);
        assert!(result.is_ok());
        let val: f32 = result.unwrap().to_num();
        // ln(0.0001) ≈ -9.2
        assert!(val < -8.0, "ln(0.0001) = {val}, expected < -8.0");
    }

    #[test]
    fn exp_overflow_to_max() {
        // exp of very large positive values should return max when scale > max_shift
        // For I16F16: total_bits = 32, max_shift = 31
        // Need scale > 31, i.e., x > 31 * ln(2) ≈ 21.5
        // exp(25) triggers scale=36 which exceeds max_shift=31
        let very_large = I16F16::from_num(25.0);
        let result: f32 = exp(very_large).to_num();
        // Should return max value
        let max: f32 = I16F16::MAX.to_num();
        assert!(
            (result - max).abs() < 1.0,
            "exp(25) = {result}, expected max {max}"
        );
    }

    #[test]
    fn exp_underflow_to_zero() {
        // exp of very large negative values should return zero when -scale > max_shift
        // For I16F16: total_bits = 32, max_shift = 31
        // Need -scale > 31, i.e., x < -31 * ln(2) ≈ -21.5
        // exp(-25) triggers scale=-36 which exceeds -max_shift=-31
        let very_negative = I16F16::from_num(-25.0);
        let result: f32 = exp(very_negative).to_num();
        // Should return zero
        assert!(result == 0.0, "exp(-25) = {result}, expected 0");
    }

    mod saturation {
        use super::*;
        use fixed::types::I32F32;

        /// Check if I16F16 value is saturated to MAX (within 0.01%)
        fn is_max_16(val: I16F16) -> bool {
            val.to_num::<f32>() >= I16F16::MAX.to_num::<f32>() * 0.9999
        }

        /// Check if I16F16 value is exactly zero
        fn is_zero_16(val: I16F16) -> bool {
            val == I16F16::ZERO
        }

        /// Check if I32F32 value is saturated to MAX (within 0.01%)
        fn is_max_32(val: I32F32) -> bool {
            val.to_num::<f64>() >= I32F32::MAX.to_num::<f64>() * 0.9999
        }

        /// Check if I32F32 value is exactly zero
        fn is_zero_32(val: I32F32) -> bool {
            val == I32F32::ZERO
        }

        // ===== exp saturation thresholds =====
        // I16F16: saturates to MAX at x >= 10.4, to zero at x <= -11.1
        // I32F32: saturates to MAX at x >= 21.49, to zero at x <= -22.2

        #[test]
        fn exp_i16f16_upper_threshold() {
            // Below threshold: should NOT saturate
            assert!(
                !is_max_16(exp(I16F16::from_num(10.39))),
                "exp(10.39) should not saturate"
            );
            // At threshold: should saturate
            assert!(
                is_max_16(exp(I16F16::from_num(10.4))),
                "exp(10.4) should saturate to MAX"
            );
        }

        #[test]
        fn exp_i16f16_lower_threshold() {
            // Above threshold: should NOT be zero
            assert!(
                !is_zero_16(exp(I16F16::from_num(-11.0))),
                "exp(-11.0) should not be zero"
            );
            // At threshold: should be zero
            assert!(
                is_zero_16(exp(I16F16::from_num(-11.1))),
                "exp(-11.1) should be zero"
            );
        }

        #[test]
        fn exp_i32f32_upper_threshold() {
            // Below threshold: should NOT saturate
            assert!(
                !is_max_32(exp(I32F32::from_num(21.48))),
                "exp(21.48) should not saturate"
            );
            // At threshold: should saturate
            assert!(
                is_max_32(exp(I32F32::from_num(21.49))),
                "exp(21.49) should saturate to MAX"
            );
        }

        #[test]
        fn exp_i32f32_lower_threshold() {
            // Above threshold: should NOT be zero
            assert!(
                !is_zero_32(exp(I32F32::from_num(-22.1))),
                "exp(-22.1) should not be zero"
            );
            // At threshold: should be zero
            assert!(
                is_zero_32(exp(I32F32::from_num(-22.2))),
                "exp(-22.2) should be zero"
            );
        }

        // ===== pow2 saturation thresholds =====
        // I16F16: saturates to MAX at x >= 15.0, to zero at x <= -16.1
        // I32F32: saturates to MAX at x >= 31.0, to zero at x <= -32.1

        #[test]
        fn pow2_i16f16_upper_threshold() {
            // Below threshold: should NOT saturate
            assert!(
                !is_max_16(pow2(I16F16::from_num(14.9))),
                "pow2(14.9) should not saturate"
            );
            // At threshold: should saturate
            assert!(
                is_max_16(pow2(I16F16::from_num(15.0))),
                "pow2(15.0) should saturate to MAX"
            );
        }

        #[test]
        fn pow2_i16f16_lower_threshold() {
            // Above threshold: should NOT be zero
            assert!(
                !is_zero_16(pow2(I16F16::from_num(-16.0))),
                "pow2(-16.0) should not be zero"
            );
            // At threshold: should be zero
            assert!(
                is_zero_16(pow2(I16F16::from_num(-16.1))),
                "pow2(-16.1) should be zero"
            );
        }

        #[test]
        fn pow2_i32f32_upper_threshold() {
            // Below threshold: should NOT saturate
            assert!(
                !is_max_32(pow2(I32F32::from_num(30.9))),
                "pow2(30.9) should not saturate"
            );
            // At threshold: should saturate
            assert!(
                is_max_32(pow2(I32F32::from_num(31.0))),
                "pow2(31.0) should saturate to MAX"
            );
        }

        #[test]
        fn pow2_i32f32_lower_threshold() {
            // Above threshold: should NOT be zero
            assert!(
                !is_zero_32(pow2(I32F32::from_num(-32.0))),
                "pow2(-32.0) should not be zero"
            );
            // At threshold: should be zero
            assert!(
                is_zero_32(pow2(I32F32::from_num(-32.1))),
                "pow2(-32.1) should be zero"
            );
        }
    }
}