Skip to main content

predsync_core/
tol.rs

1//! Tolerance bounds for numeric predictor fields.
2
3/// How absolute and relative bounds combine when both are present.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum ToleranceMode {
6    All,
7    Any,
8}
9
10/// Tolerance bounds on the delivered value of a predictor field.
11#[derive(Debug, Clone, Copy, PartialEq)]
12pub struct Tolerance {
13    pub abs: Option<f64>,
14    pub rel: Option<f64>,
15    pub mode: ToleranceMode,
16}
17
18/// Whether `pred` is within `tol` of `truth`; both arguments are final values.
19pub fn within_tol(truth: f64, pred: f64, tol: &Tolerance) -> bool {
20    let delta = (truth - pred).abs();
21    let abs_ok = tol.abs.is_none_or(|bound| delta <= bound);
22    let rel_ok = tol.rel.is_none_or(|bound| delta <= bound * truth.abs());
23    match (tol.abs, tol.rel) {
24        (Some(_), Some(_)) if tol.mode == ToleranceMode::Any => abs_ok || rel_ok,
25        _ => abs_ok && rel_ok,
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn tolerance_abs_only() {
35        let tol = Tolerance {
36            abs: Some(1.0),
37            rel: None,
38            mode: ToleranceMode::All,
39        };
40        assert!(within_tol(2.0, 1.0, &tol));
41        assert!(!within_tol(2.0, 0.5, &tol));
42    }
43
44    #[test]
45    fn tolerance_rel_only() {
46        let tol = Tolerance {
47            abs: None,
48            rel: Some(0.1),
49            mode: ToleranceMode::All,
50        };
51        assert!(within_tol(10.0, 9.0, &tol));
52        assert!(!within_tol(10.0, 8.0, &tol));
53    }
54
55    #[test]
56    fn tolerance_both_all() {
57        let tol = Tolerance {
58            abs: Some(1.0),
59            rel: Some(0.1),
60            mode: ToleranceMode::All,
61        };
62        assert!(within_tol(10.0, 9.5, &tol));
63        assert!(!within_tol(10.0, 8.0, &tol));
64    }
65
66    #[test]
67    fn tolerance_both_any() {
68        let tol = Tolerance {
69            abs: Some(0.5),
70            rel: Some(0.1),
71            mode: ToleranceMode::Any,
72        };
73        assert!(within_tol(10.0, 9.0, &tol));
74        assert!(!within_tol(10.0, 8.0, &tol));
75    }
76
77    #[test]
78    fn tolerance_equal_boundary_within() {
79        let tol = Tolerance {
80            abs: Some(1.0),
81            rel: None,
82            mode: ToleranceMode::All,
83        };
84        assert!(within_tol(1.0, 0.0, &tol));
85    }
86
87    #[test]
88    fn tolerance_zero_truth_exact_match() {
89        let tol = Tolerance {
90            abs: None,
91            rel: Some(0.5),
92            mode: ToleranceMode::All,
93        };
94        assert!(within_tol(0.0, 0.0, &tol));
95    }
96
97    #[test]
98    fn tolerance_zero_truth_any_difference_fails() {
99        let tol = Tolerance {
100            abs: None,
101            rel: Some(0.5),
102            mode: ToleranceMode::All,
103        };
104        assert!(!within_tol(0.0, 0.1, &tol));
105    }
106}