| 1 | #![allow(clippy::float_cmp)] |
| 2 | |
| 3 | use crate::{ |
| 4 | AssertFloatEq, AssertFloatEqAll, DebugUlpsDiff, FloatEq, FloatEqAll, FloatEqDebugUlpsDiff, |
| 5 | FloatEqUlpsTol, UlpsTol, |
| 6 | }; |
| 7 | |
| 8 | macro_rules! impl_traits { |
| 9 | ($float:ident, $uint:ident) => { |
| 10 | mod $float { |
| 11 | #[cfg(feature = "std")] |
| 12 | #[inline] |
| 13 | pub(crate) fn abs(value: $float) -> $float { |
| 14 | // use the intrinsic for std builds |
| 15 | value.abs() |
| 16 | } |
| 17 | |
| 18 | #[cfg(not(feature = "std"))] |
| 19 | #[inline] |
| 20 | pub(crate) fn abs(value: $float) -> $float { |
| 21 | // mask away only the sign bit for no_std builds since the abs |
| 22 | // method is not available |
| 23 | const MASK: $uint = !(1 << ((::core::mem::size_of::<$float>() * 8) - 1)); |
| 24 | $float::from_bits(value.to_bits() & MASK) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | impl FloatEqUlpsTol for $float { |
| 29 | type UlpsTol = $uint; |
| 30 | } |
| 31 | |
| 32 | impl FloatEqDebugUlpsDiff for $float { |
| 33 | type DebugUlpsDiff = Option<$uint>; |
| 34 | } |
| 35 | |
| 36 | impl FloatEq for $float { |
| 37 | type Tol = Self; |
| 38 | |
| 39 | #[inline] |
| 40 | fn eq_abs(&self, other: &Self, tol: &Self::Tol) -> bool { |
| 41 | // the PartialEq check covers equality of infinities |
| 42 | self == other || $float::abs(self - other).le(tol) |
| 43 | } |
| 44 | |
| 45 | #[inline] |
| 46 | fn eq_rmax(&self, other: &Self, tol: &Self::Tol) -> bool { |
| 47 | // the PartialEq check covers equality of infinities |
| 48 | self == other || { |
| 49 | let largest = $float::abs(*self).max($float::abs(*other)); |
| 50 | let tol = largest * tol; |
| 51 | $float::abs(self - other) <= tol |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | #[inline] |
| 56 | fn eq_rmin(&self, other: &Self, tol: &Self::Tol) -> bool { |
| 57 | // the PartialEq check covers equality of infinities |
| 58 | self == other || { |
| 59 | let largest = $float::abs(*self).min($float::abs(*other)); |
| 60 | let tol = largest * tol; |
| 61 | $float::abs(self - other) <= tol |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | #[inline] |
| 66 | fn eq_r1st(&self, other: &Self, tol: &Self::Tol) -> bool { |
| 67 | // the PartialEq check covers equality of infinities |
| 68 | self == other || { |
| 69 | let tol = $float::abs(*self) * tol; |
| 70 | $float::abs(self - other) <= tol |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | #[inline] |
| 75 | fn eq_r2nd(&self, other: &Self, tol: &Self::Tol) -> bool { |
| 76 | // the PartialEq check covers equality of infinities |
| 77 | self == other || { |
| 78 | let tol = $float::abs(*other) * tol; |
| 79 | $float::abs(self - other) <= tol |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | #[inline] |
| 84 | fn eq_ulps(&self, other: &Self, tol: &UlpsTol<Self::Tol>) -> bool { |
| 85 | if self.is_nan() || other.is_nan() { |
| 86 | false // NaNs are never equal |
| 87 | } else if self.is_sign_positive() != other.is_sign_positive() { |
| 88 | self == other // account for zero == negative zero |
| 89 | } else { |
| 90 | let a = self.to_bits(); |
| 91 | let b = other.to_bits(); |
| 92 | let max = a.max(b); |
| 93 | let min = a.min(b); |
| 94 | (max - min).le(tol) |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | impl FloatEqAll for $float { |
| 100 | type AllTol = $float; |
| 101 | |
| 102 | #[inline] |
| 103 | fn eq_abs_all(&self, other: &Self, tol: &Self::AllTol) -> bool { |
| 104 | self.eq_abs(other, tol) |
| 105 | } |
| 106 | |
| 107 | #[inline] |
| 108 | fn eq_rmax_all(&self, other: &Self, tol: &Self::AllTol) -> bool { |
| 109 | self.eq_rmax(other, tol) |
| 110 | } |
| 111 | |
| 112 | #[inline] |
| 113 | fn eq_rmin_all(&self, other: &Self, tol: &Self::AllTol) -> bool { |
| 114 | self.eq_rmin(other, tol) |
| 115 | } |
| 116 | |
| 117 | #[inline] |
| 118 | fn eq_r1st_all(&self, other: &Self, tol: &Self::AllTol) -> bool { |
| 119 | self.eq_r1st(other, tol) |
| 120 | } |
| 121 | |
| 122 | #[inline] |
| 123 | fn eq_r2nd_all(&self, other: &Self, tol: &Self::AllTol) -> bool { |
| 124 | self.eq_r2nd(other, tol) |
| 125 | } |
| 126 | |
| 127 | #[inline] |
| 128 | fn eq_ulps_all(&self, other: &Self, tol: &UlpsTol<Self::AllTol>) -> bool { |
| 129 | self.eq_ulps(other, tol) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | impl AssertFloatEq for $float { |
| 134 | type DebugAbsDiff = Self; |
| 135 | type DebugTol = Self::Tol; |
| 136 | |
| 137 | #[inline] |
| 138 | fn debug_abs_diff(&self, other: &Self) -> Self::DebugAbsDiff { |
| 139 | $float::abs(self - other) |
| 140 | } |
| 141 | |
| 142 | #[inline] |
| 143 | fn debug_ulps_diff(&self, other: &Self) -> DebugUlpsDiff<Self::DebugAbsDiff> { |
| 144 | if self == other { |
| 145 | Some(0) |
| 146 | } else if self.is_nan() || other.is_nan() { |
| 147 | None |
| 148 | } else if self.is_sign_positive() != other.is_sign_positive() { |
| 149 | None |
| 150 | } else { |
| 151 | let a = self.to_bits(); |
| 152 | let b = other.to_bits(); |
| 153 | let max = a.max(b); |
| 154 | let min = a.min(b); |
| 155 | Some(max - min) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | #[inline] |
| 160 | fn debug_abs_tol(&self, _other: &Self, tol: &Self::Tol) -> Self::DebugTol { |
| 161 | *tol |
| 162 | } |
| 163 | |
| 164 | #[inline] |
| 165 | fn debug_rmax_tol(&self, other: &Self, tol: &Self::Tol) -> Self::DebugTol { |
| 166 | $float::abs(*self).max($float::abs(*other)) * tol |
| 167 | } |
| 168 | |
| 169 | #[inline] |
| 170 | fn debug_rmin_tol(&self, other: &Self, tol: &Self::Tol) -> Self::DebugTol { |
| 171 | $float::abs(*self).min($float::abs(*other)) * tol |
| 172 | } |
| 173 | |
| 174 | #[inline] |
| 175 | fn debug_r1st_tol(&self, _other: &Self, tol: &Self::Tol) -> Self::DebugTol { |
| 176 | $float::abs(*self) * tol |
| 177 | } |
| 178 | |
| 179 | #[inline] |
| 180 | fn debug_r2nd_tol(&self, other: &Self, tol: &Self::Tol) -> Self::DebugTol { |
| 181 | $float::abs(*other) * tol |
| 182 | } |
| 183 | |
| 184 | #[inline] |
| 185 | fn debug_ulps_tol( |
| 186 | &self, |
| 187 | _other: &Self, |
| 188 | tol: &UlpsTol<Self::Tol>, |
| 189 | ) -> UlpsTol<Self::DebugTol> { |
| 190 | *tol |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | impl AssertFloatEqAll for $float { |
| 195 | type AllDebugTol = Self::AllTol; |
| 196 | |
| 197 | #[inline] |
| 198 | fn debug_abs_all_tol(&self, other: &Self, tol: &Self::AllTol) -> Self::AllDebugTol { |
| 199 | self.debug_abs_tol(other, tol) |
| 200 | } |
| 201 | |
| 202 | #[inline] |
| 203 | fn debug_rmax_all_tol(&self, other: &Self, tol: &Self::AllTol) -> Self::AllDebugTol { |
| 204 | self.debug_rmax_tol(other, tol) |
| 205 | } |
| 206 | |
| 207 | #[inline] |
| 208 | fn debug_rmin_all_tol(&self, other: &Self, tol: &Self::AllTol) -> Self::AllDebugTol { |
| 209 | self.debug_rmin_tol(other, tol) |
| 210 | } |
| 211 | |
| 212 | #[inline] |
| 213 | fn debug_r1st_all_tol(&self, other: &Self, tol: &Self::AllTol) -> Self::AllDebugTol { |
| 214 | self.debug_r1st_tol(other, tol) |
| 215 | } |
| 216 | |
| 217 | #[inline] |
| 218 | fn debug_r2nd_all_tol(&self, other: &Self, tol: &Self::AllTol) -> Self::AllDebugTol { |
| 219 | self.debug_r2nd_tol(other, tol) |
| 220 | } |
| 221 | |
| 222 | #[inline] |
| 223 | fn debug_ulps_all_tol( |
| 224 | &self, |
| 225 | other: &Self, |
| 226 | tol: &UlpsTol<Self::AllTol>, |
| 227 | ) -> UlpsTol<Self::AllDebugTol> { |
| 228 | self.debug_ulps_tol(other, tol) |
| 229 | } |
| 230 | } |
| 231 | }; |
| 232 | } |
| 233 | |
| 234 | impl_traits!(f32, u32); |
| 235 | impl_traits!(f64, u64); |