imath/
math.rs

1use super::traits::{Real, Scalar};
2
3/// Returns true if x and y are equal with an absolute error of e
4pub fn equal_with_abs_error<T>(x: T, y: T, e: T) -> bool
5where
6    T: Scalar,
7{
8    let a = if x > y { x - y } else { y - x };
9    a <= e
10}
11
12/// Returns true if x and y are equal with a relative error of e
13pub fn equal_with_rel_error<T>(x: T, y: T, e: T) -> bool
14where
15    T: Real,
16{
17    let a = if x > y { x - y } else { y - x };
18    let b = if x > T::zero() { x } else { -x };
19    a <= e * b
20}
21
22#[test]
23fn test_equal_with_error() {
24    let x: f32 = 1.0;
25    let y: f32 = 1.0001;
26    assert!(equal_with_abs_error(x, y, 0.001));
27    assert!(!equal_with_abs_error(x, y, 0.00001));
28
29    assert!(equal_with_rel_error(x, y, 0.001));
30    assert!(!equal_with_rel_error(x, y, 0.00001));
31}