nuit_core/utils/
approx_eq.rs1pub trait ApproxEq<Rhs = Self> {
3 fn approx_eq(&self, other: &Rhs, tolerance: f64) -> bool;
5}
6
7macro_rules! impl_primitive_approx_eq {
8 ($($tys:ty),*) => {
9 $(impl ApproxEq for $tys {
10 fn approx_eq(&self, other: &Self, tolerance: f64) -> bool {
11 ((self - other) as f64).abs() <= tolerance
12 }
13 })*
14 };
15}
16
17#[macro_export]
18macro_rules! assert_approx_eq {
19 ($e1:expr, $e2:expr, $tolerance:expr) => {
20 assert!($crate::ApproxEq::approx_eq(&$e1, &$e2, $tolerance), "Assertion failed: Not approximately equal\n Left: {:?}\n Right: {:?}", $e1, $e2);
21 };
22 ($e1:expr, $e2:expr) => {
23 assert_approx_eq!($e1, $e2, 0.00000001);
24 };
25}
26
27impl_primitive_approx_eq!(
28 u8, u16, u32, u64, u128, usize,
29 i8, i16, i32, i64, i128, isize,
30 f32, f64
31);