mini_math/
nearly_equal.rs1pub trait NearlyEqual {
3 fn nearly_equals(self, rhs: Self) -> bool;
4}
5
6impl NearlyEqual for f32 {
7 fn nearly_equals(self, rhs: Self) -> bool {
8 (self - rhs).abs() < std::f32::EPSILON
9 }
10}
11
12impl<T> NearlyEqual for Option<T>
13where
14 T: NearlyEqual,
15{
16 fn nearly_equals(self, rhs: Self) -> bool {
17 match (self, rhs) {
18 (Some(a), Some(b)) => a.nearly_equals(b),
19 (None, None) => true,
20 _ => false,
21 }
22 }
23}
24
25#[macro_export]
27macro_rules! assert_nearly_eq {
28 ($left:expr, $right:expr) => {{
29 if !($left.nearly_equals($right)) {
30 panic!(
31 "assertion failed: `(left == right)`\nleft: `{:?}`,\nright: `{:?}`",
32 $left, $right
33 )
34 }
35 }};
36}