approx_eq/
lib.rs

1/// Macro to test that two values are approximately equal. It checks that the relative difference between
2/// the two values is less than some epsilon value.
3///
4/// ```ignore
5/// use approx_eq::assert_approx_eq;
6/// fn main() {
7///   assert_approx_eq!(1., 0.99999999999); // should pass
8///   assert_approx_eq!(1., 0.99999999999, 1e-5); // should pass
9///   assert_approx_eq!(1., 0.99999999999, 1e-20); // should fail
10///   assert_approx_eq!(0., 0.); // should pass
11///   assert_approx_eq!(1., 2.); // should fail
12/// }
13/// ```
14#[macro_use]
15pub mod approx_eq {
16    #[macro_export]
17    /// Epsilon takes a default value of 1e-6.
18    /// It is also possible to specify the error level to use.
19    macro_rules! assert_approx_eq {
20        ($x: expr, $y: expr) => {
21            let eps = 1e-6;
22            let (x, y): (f64, f64) = ($x, $y);
23            if x == 0. {
24                assert!(y.abs() < eps, "x = {}, y = {}", x, y);
25            } else if y == 0. {
26                assert!(x.abs() < eps, "x = {}, y = {}", x, y);
27            } else {
28                assert!(&x.signum() == &y.signum(), "x = {}, y = {}", x, y);
29                let (x, y): (f64, f64) = (x.abs(), y.abs());
30                assert!(
31                    (&x - &y).abs() / [x, y].iter().cloned().fold(f64::NAN, f64::min) < eps,
32                    "x = {}, y = {}",
33                    x,
34                    y
35                );
36            }
37        };
38        ($x: expr, $y: expr, $e: expr) => {
39            let (x, y): (f64, f64) = ($x, $y);
40            if x == 0. {
41                assert!(y.abs() < $e, "x = {}, y = {}", x, y);
42            } else if y == 0. {
43                assert!(x.abs() < $e, "x = {}, y = {}", x, y);
44            } else {
45                assert!(&x.signum() == &y.signum(), "x = {}, y = {}", x, y);
46                let (x, y): (f64, f64) = (x.abs(), y.abs());
47                assert!(
48                    (&x - &y).abs() / [x, y].iter().cloned().fold(f64::NAN, f64::min) < $e,
49                    "x = {}, y = {}",
50                    x,
51                    y
52                );
53            }
54        };
55    }
56}
57
58/// Function to find the relative difference between two values.
59pub fn rel_diff(x: f64, y: f64) -> f64 {
60    if x == 0. {
61        return y.abs();
62    } else if y == 0. {
63        return x.abs();
64    } else {
65        let (ax, ay) = (x.abs(), y.abs());
66        return (&ax - &ay).abs() / [ax, ay].iter().cloned().fold(f64::NAN, f64::min);
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn test_noeps() {
76        assert_approx_eq!(1., 1.);
77        assert_approx_eq!(1., 1.000001);
78    }
79
80    #[test]
81    fn test_witheps() {
82        assert_approx_eq!(1.0000000001, 1., 1e-5);
83    }
84
85    #[test]
86    fn test_nearzero() {
87        assert_approx_eq!(0., -0.0000000000245);
88    }
89
90    #[test]
91    fn test_zero() {
92        assert_approx_eq!(0., 0.);
93    }
94
95    #[test]
96    #[should_panic]
97    fn test_invalid() {
98        assert_approx_eq!(1.0000000001, 1., 1e-10);
99    }
100
101    #[test]
102    #[should_panic]
103    fn test_sign() {
104        assert_approx_eq!(1., -1.);
105    }
106}