integer_or_float/cmp/
approx.rs1use crate::{IntegerOrFloat, *};
2
3#[cfg(not(feature = "x64-backing-store"))]
4use float_cmp::F32Margin as FMargin;
5#[cfg(feature = "x64-backing-store")]
6use float_cmp::F64Margin as FMargin;
7use float_cmp::{ApproxEq, Ulps};
8
9impl ApproxEq for IntegerOrFloat {
10 type Margin = FMargin;
11
12 fn approx_eq<M: Into<Self::Margin>>(self, other: Self, margin: M) -> bool {
13 let margin: FMargin = margin.into();
14 if let (Ok(i1), Ok(i2), true) = (
15 self.holding_integer(),
16 other.holding_integer(),
17 0.0 == margin.epsilon && margin.ulps == FMargin::zero().ulps,
18 ) {
19 return i1 == i2;
20 }
21
22 f_iof::from(self).approx_eq(other.into(), margin)
23 }
24}
25
26impl Ulps for IntegerOrFloat {
27 type U = Self;
28
29 fn ulps(&self, other: &Self) -> Self {
30 Integer(self.unwrap_float().ulps(&other.unwrap_float()))
31 }
32 fn next(&self) -> Self {
33 Float(self.unwrap_float().next())
34 }
35 fn prev(&self) -> Self {
36 Float(self.unwrap_float().prev())
37 }
38}