webgl_matrix/
utils.rs

1// we redfine it, because of no_std
2// pub const EPSILON : f32 = 1.19209290e-07_f32;
3pub const EPSILON: f32 = 1e-5_f32;
4
5/// Checks if two sequences of numbers are equal up to EPSILON precision.
6pub fn almost_eq(a: &[f32], b: &[f32]) -> bool {
7    if a.len() == b.len() {
8        a.iter()
9            .zip(b.iter())
10            .all(|(&ai, &bi)| ai - EPSILON <= bi && ai + EPSILON >= bi)
11    } else {
12        false
13    }
14}
15
16#[cfg(test)]
17mod tests {
18    use super::*;
19
20    #[test]
21    fn almost_eq_if_equal() {
22        let a = [1., 2., 3., 4.];
23        let b = [1., 2., 3., 4.];
24
25        assert!(almost_eq(&a, &b));
26    }
27
28    #[test]
29    fn almost_eq_if_not_equal() {
30        let a = [1., 2., 3., 4.];
31        let b = [1., 2.001, 3., 4.];
32
33        assert!(!almost_eq(&a, &b));
34
35        let a = [1., 2., 3., 4.];
36        let b = [1., 2., 2.9999, 4.];
37
38        assert!(!almost_eq(&a, &b));
39    }
40}