light_curve_common/
allclose.rs

1use std::fmt::Debug;
2
3use num_traits::float::Float;
4
5/// Panics if two float slices are not close with respect to some absolute tolerance
6///
7/// # Examples
8///
9/// ```
10/// use light_curve_common::all_close;
11///
12/// all_close(&[0.0, 1.0], &[0.0001, 0.9998], 1e-2);
13/// ```
14///
15/// ```should_panic
16/// use light_curve_common::all_close;
17///
18/// all_close(&[0.0], &[0.0, 1.0], 1e-6);
19/// ```
20///
21/// ```should_panic
22/// use light_curve_common::all_close;
23///
24/// all_close(&[1e-3, 1.0], &[0.0, 1.0], 1e-4);
25/// ```
26pub fn all_close<T>(actual: &[T], desired: &[T], tol: T)
27where
28    T: Float + Debug,
29{
30    assert_eq!(actual.len(), desired.len());
31    let is_close = actual
32        .iter()
33        .cloned()
34        .zip(desired.iter().cloned())
35        .all(|(x, y)| (x - y).abs() < tol);
36    assert!(
37        is_close,
38        "Slices are not close:\n{:?}\n{:?}\n",
39        actual, desired
40    );
41}