| 1 | use crate::{AssertFloatEq, AssertFloatEqAll, FloatEq, FloatEqAll, UlpsTol}; |
| 2 | |
| 3 | /// Checks if two floating point expressions are equal to each other. |
| 4 | /// |
| 5 | /// See the top level documentation for a list of available [comparison algorithms]. |
| 6 | /// |
| 7 | /// # Examples |
| 8 | /// ``` |
| 9 | /// # use float_eq::float_eq; |
| 10 | /// # use std::f32; |
| 11 | /// let a: f32 = 4.0; |
| 12 | /// let b: f32 = 4.000_002_5; |
| 13 | /// |
| 14 | /// assert!(float_eq!(a, b, ulps <= 10)); |
| 15 | /// assert!(float_eq!(a, 3.999_999_6, rmax <= 2.0 * f32::EPSILON)); |
| 16 | /// assert!(float_eq!(a - b, 0.0, abs <= 0.000_01)); |
| 17 | /// ``` |
| 18 | /// |
| 19 | /// [comparison algorithms]: index.html#comparison-algorithms |
| 20 | /// [from left to right]: index.html#combining-checks |
| 21 | #[macro_export] |
| 22 | macro_rules! float_eq { |
| 23 | ($a:expr, $b:expr, $($eq:ident <= $tol:expr),+) => ({ |
| 24 | match (&$a, &$b) { |
| 25 | (a_val, b_val) => { |
| 26 | false $(|| $crate::FloatEqCmp::$eq(a_val, b_val, &$tol))+ |
| 27 | } |
| 28 | } |
| 29 | }); |
| 30 | ($a:expr, $b:expr, $($eq:ident <= $tol:expr),+,) => ({ |
| 31 | $crate::float_eq!($a, $b $(, $eq <= $tol)+) |
| 32 | }) |
| 33 | } |
| 34 | |
| 35 | /// Checks if two floating point expressions are not equal to each other. |
| 36 | /// |
| 37 | /// See the top level documentation for a list of available [comparison algorithms]. |
| 38 | /// |
| 39 | /// # Examples |
| 40 | /// ``` |
| 41 | /// # use float_eq::float_ne; |
| 42 | /// # use std::f32; |
| 43 | /// let a: f32 = 4.0; |
| 44 | /// let b: f32 = 4.1; |
| 45 | /// |
| 46 | /// assert!(float_ne!(a, b, ulps <= 10)); |
| 47 | /// assert!(float_ne!(a, b, rmax <= 2.0 * f32::EPSILON)); |
| 48 | /// assert!(float_ne!(a - b, 0.0, abs <= 0.000_01)); |
| 49 | /// ``` |
| 50 | /// |
| 51 | /// [comparison algorithms]: index.html#comparison-algorithms |
| 52 | /// [from left to right]: index.html#combining-checks |
| 53 | #[macro_export] |
| 54 | macro_rules! float_ne { |
| 55 | ($a:expr, $b:expr, $($eq:ident <= $tol:expr),+) => ({ |
| 56 | !$crate::float_eq!($a, $b $(, $eq <= $tol)+) |
| 57 | }); |
| 58 | ($a:expr, $b:expr, $($eq:ident <= $tol:expr),+,) => ({ |
| 59 | !$crate::float_eq!($a, $b $(, $eq <= $tol)+) |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | /// Asserts that two floating point expressions are equal to each other. |
| 64 | /// |
| 65 | /// See the top level documentation for a list of available [comparison algorithms]. |
| 66 | /// |
| 67 | /// On panic, this macro will print the values of the expressions with their debug |
| 68 | /// representations, with [additional information] from the comparison operations. |
| 69 | /// Like [`assert!`], this macro has a second form, where a custom panic message can |
| 70 | /// be provided. |
| 71 | /// |
| 72 | /// # Examples |
| 73 | /// ``` |
| 74 | /// # use float_eq::assert_float_eq; |
| 75 | /// # use std::f32; |
| 76 | /// let a: f32 = 4.0; |
| 77 | /// let b: f32 = 4.000_002_5; |
| 78 | /// |
| 79 | /// assert_float_eq!(a, b, ulps <= 10); |
| 80 | /// assert_float_eq!(a, 3.999_999_6, rmax <= 2.0 * f32::EPSILON); |
| 81 | /// assert_float_eq!(a - b, 0.0, abs <= 0.000_01, "Checking that {} == {}", a, b); |
| 82 | /// ``` |
| 83 | /// |
| 84 | /// [`assert!`]: https://doc.rust-lang.org/std/macro.assert.html |
| 85 | /// [additional information]: https://jtempest.github.io/float_eq-rs/book/how_to/interpret_assert_failure_messages.html |
| 86 | /// [comparison algorithms]: index.html#comparison-algorithms |
| 87 | /// [from left to right]: index.html#combining-checks |
| 88 | #[macro_export] |
| 89 | macro_rules! assert_float_eq { |
| 90 | // the order of these rules matters a *lot* for the format string functionality |
| 91 | // to work, otherwise we end up consuming the general case too early. |
| 92 | ($left:expr, $right:expr, $eq1:ident <= $tol_1:expr, $eq2:ident <= $tol_2:expr, $eq3:ident <= $tol_3:expr) => ({ |
| 93 | match (&$left, &$right, &$tol_1, &$tol_2, &$tol_3) { |
| 94 | (left_val, right_val, tol_1_val, tol_2_val, tol_3_val) => { |
| 95 | if !$crate::float_eq!( |
| 96 | *left_val, |
| 97 | *right_val, |
| 98 | $eq1 <= *tol_1_val, |
| 99 | $eq2 <= *tol_2_val, |
| 100 | $eq3 <= *tol_3_val) { |
| 101 | // The reborrows below are intentional. See assert_eq! in the standard library. |
| 102 | panic!(concat!( |
| 103 | "assertion failed: `float_eq!(left, right, ", stringify!($eq1), " <= t, ", stringify!($eq2), " <= t, ", stringify!($eq3), r#" <= t)` |
| 104 | left: `{:?}`, |
| 105 | right: `{:?}`, |
| 106 | abs_diff: `{:?}`, |
| 107 | ulps_diff: `{:?}`, |
| 108 | {:>10} t: `{:?}`, |
| 109 | {:>10} t: `{:?}`, |
| 110 | {:>10} t: `{:?}`"#), |
| 111 | &*left_val, |
| 112 | &*right_val, |
| 113 | $crate::AssertFloatEq::debug_abs_diff(&*left_val, &*right_val), |
| 114 | $crate::AssertFloatEq::debug_ulps_diff(&*left_val, &*right_val), |
| 115 | concat!("[", stringify!($eq1), "]"), |
| 116 | $crate::FloatCmpOpTol::$eq1(&*left_val, &*right_val, &*tol_1_val), |
| 117 | concat!("[", stringify!($eq2), "]"), |
| 118 | $crate::FloatCmpOpTol::$eq2(&*left_val, &*right_val, &*tol_2_val), |
| 119 | concat!("[", stringify!($eq3), "]"), |
| 120 | $crate::FloatCmpOpTol::$eq3(&*left_val, &*right_val, &*tol_3_val) |
| 121 | ) |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | }); |
| 126 | ($left:expr, $right:expr, $eq1:ident <= $tol_1:expr, $eq2:ident <= $tol_2:expr) => ({ |
| 127 | match (&$left, &$right, &$tol_1, &$tol_2) { |
| 128 | (left_val, right_val, tol_1_val, tol_2_val) => { |
| 129 | if !$crate::float_eq!( |
| 130 | *left_val, |
| 131 | *right_val, |
| 132 | $eq1 <= *tol_1_val, |
| 133 | $eq2 <= *tol_2_val) { |
| 134 | // The reborrows below are intentional. See assert_eq! in the standard library. |
| 135 | panic!(concat!( |
| 136 | "assertion failed: `float_eq!(left, right, ", stringify!($eq1), " <= t, ", stringify!($eq2), r#" <= t)` |
| 137 | left: `{:?}`, |
| 138 | right: `{:?}`, |
| 139 | abs_diff: `{:?}`, |
| 140 | ulps_diff: `{:?}`, |
| 141 | {:>10} t: `{:?}`, |
| 142 | {:>10} t: `{:?}`"#), |
| 143 | &*left_val, |
| 144 | &*right_val, |
| 145 | $crate::AssertFloatEq::debug_abs_diff(&*left_val, &*right_val), |
| 146 | $crate::AssertFloatEq::debug_ulps_diff(&*left_val, &*right_val), |
| 147 | concat!("[", stringify!($eq1), "]"), |
| 148 | $crate::FloatCmpOpTol::$eq1(&*left_val, &*right_val, &*tol_1_val), |
| 149 | concat!("[", stringify!($eq2), "]"), |
| 150 | $crate::FloatCmpOpTol::$eq2(&*left_val, &*right_val, &*tol_2_val), |
| 151 | ) |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | }); |
| 156 | ($left:expr, $right:expr, $eq1:ident <= $tol_1:expr) => ({ |
| 157 | match (&$left, &$right, &$tol_1) { |
| 158 | (left_val, right_val, tol_1_val) => { |
| 159 | if !$crate::float_eq!( |
| 160 | *left_val, |
| 161 | *right_val, |
| 162 | $eq1 <= *tol_1_val) { |
| 163 | // The reborrows below are intentional. See assert_eq! in the standard library. |
| 164 | panic!(concat!( |
| 165 | "assertion failed: `float_eq!(left, right, ", stringify!($eq1), r#" <= t)` |
| 166 | left: `{:?}`, |
| 167 | right: `{:?}`, |
| 168 | abs_diff: `{:?}`, |
| 169 | ulps_diff: `{:?}`, |
| 170 | {:>10} t: `{:?}`"#), |
| 171 | &*left_val, |
| 172 | &*right_val, |
| 173 | $crate::AssertFloatEq::debug_abs_diff(&*left_val, &*right_val), |
| 174 | $crate::AssertFloatEq::debug_ulps_diff(&*left_val, &*right_val), |
| 175 | concat!("[", stringify!($eq1), "]"), |
| 176 | $crate::FloatCmpOpTol::$eq1(&*left_val, &*right_val, &*tol_1_val), |
| 177 | ) |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | }); |
| 182 | ($left:expr, $right:expr, $($eq:ident <= $tol:expr,)+) => ({ |
| 183 | $crate::assert_float_eq!($left, $right $(, $eq <= $tol)+) |
| 184 | }); |
| 185 | ($left:expr, $right:expr, $eq1:ident <= $tol_1:expr, $eq2:ident <= $tol_2:expr, $eq3:ident <= $tol_3:expr, $($arg:tt)+) => ({ |
| 186 | match (&$left, &$right, &$tol_1, &$tol_2, &$tol_3) { |
| 187 | (left_val, right_val, tol_1_val, tol_2_val, tol_3_val) => { |
| 188 | if !$crate::float_eq!( |
| 189 | *left_val, |
| 190 | *right_val, |
| 191 | $eq1 <= *tol_1_val, |
| 192 | $eq2 <= *tol_2_val, |
| 193 | $eq3 <= *tol_3_val) { |
| 194 | // The reborrows below are intentional. See assert_eq! in the standard library. |
| 195 | panic!(concat!( |
| 196 | "assertion failed: `float_eq!(left, right, ", stringify!($eq1), " <= t, ", stringify!($eq2), " <= t, ", stringify!($eq3), r#" <= t)` |
| 197 | left: `{:?}`, |
| 198 | right: `{:?}`, |
| 199 | abs_diff: `{:?}`, |
| 200 | ulps_diff: `{:?}`, |
| 201 | {:>10} t: `{:?}`, |
| 202 | {:>10} t: `{:?}`, |
| 203 | {:>10} t: `{:?}`: {}"#), |
| 204 | &*left_val, |
| 205 | &*right_val, |
| 206 | $crate::AssertFloatEq::debug_abs_diff(&*left_val, &*right_val), |
| 207 | $crate::AssertFloatEq::debug_ulps_diff(&*left_val, &*right_val), |
| 208 | concat!("[", stringify!($eq1), "]"), |
| 209 | $crate::FloatCmpOpTol::$eq1(&*left_val, &*right_val, &*tol_1_val), |
| 210 | concat!("[", stringify!($eq2), "]"), |
| 211 | $crate::FloatCmpOpTol::$eq2(&*left_val, &*right_val, &*tol_2_val), |
| 212 | concat!("[", stringify!($eq3), "]"), |
| 213 | $crate::FloatCmpOpTol::$eq3(&*left_val, &*right_val, &*tol_3_val), |
| 214 | format_args!($($arg)+) |
| 215 | ) |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | }); |
| 220 | ($left:expr, $right:expr, $eq1:ident <= $tol_1:expr, $eq2:ident <= $tol_2:expr, $($arg:tt)+) => ({ |
| 221 | match (&$left, &$right, &$tol_1, &$tol_2) { |
| 222 | (left_val, right_val, tol_1_val, tol_2_val) => { |
| 223 | if !$crate::float_eq!( |
| 224 | *left_val, |
| 225 | *right_val, |
| 226 | $eq1 <= *tol_1_val, |
| 227 | $eq2 <= *tol_2_val) { |
| 228 | // The reborrows below are intentional. See assert_eq! in the standard library. |
| 229 | panic!(concat!( |
| 230 | "assertion failed: `float_eq!(left, right, ", stringify!($eq1), " <= t, ", stringify!($eq2), r#" <= t)` |
| 231 | left: `{:?}`, |
| 232 | right: `{:?}`, |
| 233 | abs_diff: `{:?}`, |
| 234 | ulps_diff: `{:?}`, |
| 235 | {:>10} t: `{:?}`, |
| 236 | {:>10} t: `{:?}`: {}"#), |
| 237 | &*left_val, |
| 238 | &*right_val, |
| 239 | $crate::AssertFloatEq::debug_abs_diff(&*left_val, &*right_val), |
| 240 | $crate::AssertFloatEq::debug_ulps_diff(&*left_val, &*right_val), |
| 241 | concat!("[", stringify!($eq1), "]"), |
| 242 | $crate::FloatCmpOpTol::$eq1(&*left_val, &*right_val, &*tol_1_val), |
| 243 | concat!("[", stringify!($eq2), "]"), |
| 244 | $crate::FloatCmpOpTol::$eq2(&*left_val, &*right_val, &*tol_2_val), |
| 245 | format_args!($($arg)+) |
| 246 | ) |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | }); |
| 251 | ($left:expr, $right:expr, $eq1:ident <= $tol_1:expr, $($arg:tt)+) => ({ |
| 252 | match (&$left, &$right, &$tol_1) { |
| 253 | (left_val, right_val, tol_1_val) => { |
| 254 | if !$crate::float_eq!( |
| 255 | *left_val, |
| 256 | *right_val, |
| 257 | $eq1 <= *tol_1_val) { |
| 258 | // The reborrows below are intentional. See assert_eq! in the standard library. |
| 259 | panic!(concat!( |
| 260 | "assertion failed: `float_eq!(left, right, ", stringify!($eq1), r#" <= t)` |
| 261 | left: `{:?}`, |
| 262 | right: `{:?}`, |
| 263 | abs_diff: `{:?}`, |
| 264 | ulps_diff: `{:?}`, |
| 265 | {:>10} t: `{:?}`: {}"#), |
| 266 | &*left_val, |
| 267 | &*right_val, |
| 268 | $crate::AssertFloatEq::debug_abs_diff(&*left_val, &*right_val), |
| 269 | $crate::AssertFloatEq::debug_ulps_diff(&*left_val, &*right_val), |
| 270 | concat!("[", stringify!($eq1), "]"), |
| 271 | $crate::FloatCmpOpTol::$eq1(&*left_val, &*right_val, &*tol_1_val), |
| 272 | format_args!($($arg)+) |
| 273 | ) |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | }); |
| 278 | } |
| 279 | |
| 280 | /// Asserts that two floating point expressions are not equal to each other. |
| 281 | /// |
| 282 | /// See the top level documentation for a list of available [comparison algorithms]. |
| 283 | /// |
| 284 | /// On panic, this macro will print the values of the expressions with their debug |
| 285 | /// representations, with [additional information] from the comparison operations. |
| 286 | /// Like [`assert!`], this macro has a second form, where a custom panic message can |
| 287 | /// be provided. |
| 288 | /// |
| 289 | /// # Examples |
| 290 | /// ``` |
| 291 | /// # use float_eq::assert_float_ne; |
| 292 | /// # use std::f32; |
| 293 | /// let a: f32 = 4.0; |
| 294 | /// let b: f32 = 4.1; |
| 295 | /// |
| 296 | /// assert_float_ne!(a, b, ulps <= 10); |
| 297 | /// assert_float_ne!(a, b, rmax <= 2.0 * f32::EPSILON); |
| 298 | /// assert_float_ne!(a - b, 0.0, abs <= 0.000_01, "Checking that {} != {}", a, b); |
| 299 | /// ``` |
| 300 | /// |
| 301 | /// [`assert!`]: https://doc.rust-lang.org/std/macro.assert.html |
| 302 | /// [additional information]: https://jtempest.github.io/float_eq-rs/book/how_to/interpret_assert_failure_messages.html |
| 303 | /// [comparison algorithms]: index.html#comparison-algorithms |
| 304 | /// [from left to right]: index.html#combining-checks |
| 305 | #[macro_export] |
| 306 | macro_rules! assert_float_ne { |
| 307 | // the order of these rules matters a *lot* for the format string functionality |
| 308 | // to work, otherwise we end up consuming the general case too early. |
| 309 | ($left:expr, $right:expr, $eq1:ident <= $tol_1:expr, $eq2:ident <= $tol_2:expr, $eq3:ident <= $tol_3:expr) => ({ |
| 310 | match (&$left, &$right, &$tol_1, &$tol_2, &$tol_3) { |
| 311 | (left_val, right_val, tol_1_val, tol_2_val, tol_3_val) => { |
| 312 | if !$crate::float_ne!( |
| 313 | *left_val, |
| 314 | *right_val, |
| 315 | $eq1 <= *tol_1_val, |
| 316 | $eq2 <= *tol_2_val, |
| 317 | $eq3 <= *tol_3_val) { |
| 318 | // The reborrows below are intentional. See assert_eq! in the standard library. |
| 319 | panic!(concat!( |
| 320 | "assertion failed: `float_ne!(left, right, ", stringify!($eq1), " <= t, ", stringify!($eq2), " <= t, ", stringify!($eq3), r#" <= t)` |
| 321 | left: `{:?}`, |
| 322 | right: `{:?}`, |
| 323 | abs_diff: `{:?}`, |
| 324 | ulps_diff: `{:?}`, |
| 325 | {:>10} t: `{:?}`, |
| 326 | {:>10} t: `{:?}`, |
| 327 | {:>10} t: `{:?}`"#), |
| 328 | &*left_val, |
| 329 | &*right_val, |
| 330 | $crate::AssertFloatEq::debug_abs_diff(&*left_val, &*right_val), |
| 331 | $crate::AssertFloatEq::debug_ulps_diff(&*left_val, &*right_val), |
| 332 | concat!("[", stringify!($eq1), "]"), |
| 333 | $crate::FloatCmpOpTol::$eq1(&*left_val, &*right_val, &*tol_1_val), |
| 334 | concat!("[", stringify!($eq2), "]"), |
| 335 | $crate::FloatCmpOpTol::$eq2(&*left_val, &*right_val, &*tol_2_val), |
| 336 | concat!("[", stringify!($eq3), "]"), |
| 337 | $crate::FloatCmpOpTol::$eq3(&*left_val, &*right_val, &*tol_3_val) |
| 338 | ) |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | }); |
| 343 | ($left:expr, $right:expr, $eq1:ident <= $tol_1:expr, $eq2:ident <= $tol_2:expr) => ({ |
| 344 | match (&$left, &$right, &$tol_1, &$tol_2) { |
| 345 | (left_val, right_val, tol_1_val, tol_2_val) => { |
| 346 | if !$crate::float_ne!( |
| 347 | *left_val, |
| 348 | *right_val, |
| 349 | $eq1 <= *tol_1_val, |
| 350 | $eq2 <= *tol_2_val) { |
| 351 | // The reborrows below are intentional. See assert_eq! in the standard library. |
| 352 | panic!(concat!( |
| 353 | "assertion failed: `float_ne!(left, right, ", stringify!($eq1), " <= t, ", stringify!($eq2), r#" <= t)` |
| 354 | left: `{:?}`, |
| 355 | right: `{:?}`, |
| 356 | abs_diff: `{:?}`, |
| 357 | ulps_diff: `{:?}`, |
| 358 | {:>10} t: `{:?}`, |
| 359 | {:>10} t: `{:?}`"#), |
| 360 | &*left_val, |
| 361 | &*right_val, |
| 362 | $crate::AssertFloatEq::debug_abs_diff(&*left_val, &*right_val), |
| 363 | $crate::AssertFloatEq::debug_ulps_diff(&*left_val, &*right_val), |
| 364 | concat!("[", stringify!($eq1), "]"), |
| 365 | $crate::FloatCmpOpTol::$eq1(&*left_val, &*right_val, &*tol_1_val), |
| 366 | concat!("[", stringify!($eq2), "]"), |
| 367 | $crate::FloatCmpOpTol::$eq2(&*left_val, &*right_val, &*tol_2_val), |
| 368 | ) |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | }); |
| 373 | ($left:expr, $right:expr, $eq1:ident <= $tol_1:expr) => ({ |
| 374 | match (&$left, &$right, &$tol_1) { |
| 375 | (left_val, right_val, tol_1_val) => { |
| 376 | if !$crate::float_ne!( |
| 377 | *left_val, |
| 378 | *right_val, |
| 379 | $eq1 <= *tol_1_val) { |
| 380 | // The reborrows below are intentional. See assert_eq! in the standard library. |
| 381 | panic!(concat!( |
| 382 | "assertion failed: `float_ne!(left, right, ", stringify!($eq1), r#" <= t)` |
| 383 | left: `{:?}`, |
| 384 | right: `{:?}`, |
| 385 | abs_diff: `{:?}`, |
| 386 | ulps_diff: `{:?}`, |
| 387 | {:>10} t: `{:?}`"#), |
| 388 | &*left_val, |
| 389 | &*right_val, |
| 390 | $crate::AssertFloatEq::debug_abs_diff(&*left_val, &*right_val), |
| 391 | $crate::AssertFloatEq::debug_ulps_diff(&*left_val, &*right_val), |
| 392 | concat!("[", stringify!($eq1), "]"), |
| 393 | $crate::FloatCmpOpTol::$eq1(&*left_val, &*right_val, &*tol_1_val), |
| 394 | ) |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | }); |
| 399 | ($left:expr, $right:expr, $($eq:ident <= $tol:expr,)+) => ({ |
| 400 | $crate::assert_float_ne!($left, $right $(, $eq <= $tol)+) |
| 401 | }); |
| 402 | ($left:expr, $right:expr, $eq1:ident <= $tol_1:expr, $eq2:ident <= $tol_2:expr, $eq3:ident <= $tol_3:expr, $($arg:tt)+) => ({ |
| 403 | match (&$left, &$right, &$tol_1, &$tol_2, &$tol_3) { |
| 404 | (left_val, right_val, tol_1_val, tol_2_val, tol_3_val) => { |
| 405 | if !$crate::float_ne!( |
| 406 | *left_val, |
| 407 | *right_val, |
| 408 | $eq1 <= *tol_1_val, |
| 409 | $eq2 <= *tol_2_val, |
| 410 | $eq3 <= *tol_3_val) { |
| 411 | // The reborrows below are intentional. See assert_eq! in the standard library. |
| 412 | panic!(concat!( |
| 413 | "assertion failed: `float_ne!(left, right, ", stringify!($eq1), " <= t, ", stringify!($eq2), " <= t, ", stringify!($eq3), r#" <= t)` |
| 414 | left: `{:?}`, |
| 415 | right: `{:?}`, |
| 416 | abs_diff: `{:?}`, |
| 417 | ulps_diff: `{:?}`, |
| 418 | {:>10} t: `{:?}`, |
| 419 | {:>10} t: `{:?}`, |
| 420 | {:>10} t: `{:?}`: {}"#), |
| 421 | &*left_val, |
| 422 | &*right_val, |
| 423 | $crate::AssertFloatEq::debug_abs_diff(&*left_val, &*right_val), |
| 424 | $crate::AssertFloatEq::debug_ulps_diff(&*left_val, &*right_val), |
| 425 | concat!("[", stringify!($eq1), "]"), |
| 426 | $crate::FloatCmpOpTol::$eq1(&*left_val, &*right_val, &*tol_1_val), |
| 427 | concat!("[", stringify!($eq2), "]"), |
| 428 | $crate::FloatCmpOpTol::$eq2(&*left_val, &*right_val, &*tol_2_val), |
| 429 | concat!("[", stringify!($eq3), "]"), |
| 430 | $crate::FloatCmpOpTol::$eq3(&*left_val, &*right_val, &*tol_3_val), |
| 431 | format_args!($($arg)+) |
| 432 | ) |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | }); |
| 437 | ($left:expr, $right:expr, $eq1:ident <= $tol_1:expr, $eq2:ident <= $tol_2:expr, $($arg:tt)+) => ({ |
| 438 | match (&$left, &$right, &$tol_1, &$tol_2) { |
| 439 | (left_val, right_val, tol_1_val, tol_2_val) => { |
| 440 | if !$crate::float_ne!( |
| 441 | *left_val, |
| 442 | *right_val, |
| 443 | $eq1 <= *tol_1_val, |
| 444 | $eq2 <= *tol_2_val) { |
| 445 | // The reborrows below are intentional. See assert_eq! in the standard library. |
| 446 | panic!(concat!( |
| 447 | "assertion failed: `float_ne!(left, right, ", stringify!($eq1), " <= t, ", stringify!($eq2), r#" <= t)` |
| 448 | left: `{:?}`, |
| 449 | right: `{:?}`, |
| 450 | abs_diff: `{:?}`, |
| 451 | ulps_diff: `{:?}`, |
| 452 | {:>10} t: `{:?}`, |
| 453 | {:>10} t: `{:?}`: {}"#), |
| 454 | &*left_val, |
| 455 | &*right_val, |
| 456 | $crate::AssertFloatEq::debug_abs_diff(&*left_val, &*right_val), |
| 457 | $crate::AssertFloatEq::debug_ulps_diff(&*left_val, &*right_val), |
| 458 | concat!("[", stringify!($eq1), "]"), |
| 459 | $crate::FloatCmpOpTol::$eq1(&*left_val, &*right_val, &*tol_1_val), |
| 460 | concat!("[", stringify!($eq2), "]"), |
| 461 | $crate::FloatCmpOpTol::$eq2(&*left_val, &*right_val, &*tol_2_val), |
| 462 | format_args!($($arg)+) |
| 463 | ) |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | }); |
| 468 | ($left:expr, $right:expr, $eq1:ident <= $tol_1:expr, $($arg:tt)+) => ({ |
| 469 | match (&$left, &$right, &$tol_1) { |
| 470 | (left_val, right_val, tol_1_val) => { |
| 471 | if !$crate::float_ne!( |
| 472 | *left_val, |
| 473 | *right_val, |
| 474 | $eq1 <= *tol_1_val) { |
| 475 | // The reborrows below are intentional. See assert_eq! in the standard library. |
| 476 | panic!(concat!( |
| 477 | "assertion failed: `float_ne!(left, right, ", stringify!($eq1), r#" <= t)` |
| 478 | left: `{:?}`, |
| 479 | right: `{:?}`, |
| 480 | abs_diff: `{:?}`, |
| 481 | ulps_diff: `{:?}`, |
| 482 | {:>10} t: `{:?}`: {}"#), |
| 483 | &*left_val, |
| 484 | &*right_val, |
| 485 | $crate::AssertFloatEq::debug_abs_diff(&*left_val, &*right_val), |
| 486 | $crate::AssertFloatEq::debug_ulps_diff(&*left_val, &*right_val), |
| 487 | concat!("[", stringify!($eq1), "]"), |
| 488 | $crate::FloatCmpOpTol::$eq1(&*left_val, &*right_val, &*tol_1_val), |
| 489 | format_args!($($arg)+) |
| 490 | ) |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | }); |
| 495 | } |
| 496 | |
| 497 | /// Asserts that two floating point expressions are equal to each other. |
| 498 | /// |
| 499 | /// See the top level documentation for a list of available [comparison algorithms]. |
| 500 | /// |
| 501 | /// On panic, this macro will print the values of the expressions with their debug |
| 502 | /// representations, with [additional information] from the comparison operations. |
| 503 | /// Like [`assert!`], this macro has a second form, where a custom panic message can |
| 504 | /// be provided. |
| 505 | /// |
| 506 | /// Unlike [`assert_float_eq!`], `debug_assert_float_eq!` statements are only enabled in |
| 507 | /// non optimized builds by default. See [`debug_assert_eq!`] for more details. |
| 508 | /// |
| 509 | /// # Examples |
| 510 | /// ``` |
| 511 | /// # use float_eq::debug_assert_float_eq; |
| 512 | /// # use std::f32; |
| 513 | /// let a: f32 = 4.0; |
| 514 | /// let b: f32 = 4.000_002_5; |
| 515 | /// |
| 516 | /// debug_assert_float_eq!(a, b, ulps <= 10); |
| 517 | /// debug_assert_float_eq!(a, 3.999_999_6, rmax <= 2.0 * f32::EPSILON); |
| 518 | /// debug_assert_float_eq!(a - b, 0.0, abs <= 0.000_01, "Checking that {} == {}", a, b); |
| 519 | /// ``` |
| 520 | /// |
| 521 | /// [`assert!`]: https://doc.rust-lang.org/std/macro.assert.html |
| 522 | /// [`debug_assert_eq!`]: https://doc.rust-lang.org/std/macro.debug_assert_eq.html |
| 523 | /// [additional information]: https://jtempest.github.io/float_eq-rs/book/how_to/interpret_assert_failure_messages.html |
| 524 | /// [comparison algorithms]: index.html#comparison-algorithms |
| 525 | /// [from left to right]: index.html#combining-checks |
| 526 | #[macro_export] |
| 527 | macro_rules! debug_assert_float_eq { |
| 528 | ($($arg:tt)*) => (if cfg!(debug_assertions) { $crate::assert_float_eq!($($arg)*); }) |
| 529 | } |
| 530 | |
| 531 | /// Asserts that two floating point expressions are not equal to each other. |
| 532 | /// |
| 533 | /// See the top level documentation for a list of available [comparison algorithms]. |
| 534 | /// |
| 535 | /// On panic, this macro will print the values of the expressions with their debug |
| 536 | /// representations, with [additional information] from the comparison operations. |
| 537 | /// Like [`assert!`], this macro has a second form, where a custom panic message can |
| 538 | /// be provided. |
| 539 | /// |
| 540 | /// Unlike [`assert_float_ne!`], `debug_assert_float_ne!` statements are only enabled in |
| 541 | /// non optimized builds by default. See [`debug_assert_ne!`] for more details. |
| 542 | /// |
| 543 | /// # Examples |
| 544 | /// ``` |
| 545 | /// # use float_eq::debug_assert_float_ne; |
| 546 | /// # use std::f32; |
| 547 | /// let a: f32 = 4.0; |
| 548 | /// let b: f32 = 4.1; |
| 549 | /// |
| 550 | /// debug_assert_float_ne!(a, b, ulps <= 10); |
| 551 | /// debug_assert_float_ne!(a, b, rmax <= 2.0 * f32::EPSILON); |
| 552 | /// debug_assert_float_ne!(a - b, 0.0, abs <= 0.000_01, "Checking that {} != {}", a, b); |
| 553 | /// ``` |
| 554 | /// |
| 555 | /// [`assert!`]: https://doc.rust-lang.org/std/macro.assert.html |
| 556 | /// [`debug_assert_ne!`]: https://doc.rust-lang.org/std/macro.debug_assert_ne.html |
| 557 | /// [additional information]: https://jtempest.github.io/float_eq-rs/book/how_to/interpret_assert_failure_messages.html |
| 558 | /// [comparison algorithms]: index.html#comparison-algorithms |
| 559 | /// [from left to right]: index.html#combining-checks |
| 560 | #[macro_export] |
| 561 | macro_rules! debug_assert_float_ne { |
| 562 | ($($arg:tt)*) => (if cfg!(debug_assertions) { $crate::assert_float_ne!($($arg)*); }) |
| 563 | } |
| 564 | |
| 565 | #[doc(hidden)] |
| 566 | pub struct FloatEqCmp; |
| 567 | |
| 568 | #[doc(hidden)] |
| 569 | impl FloatEqCmp { |
| 570 | #[inline] |
| 571 | pub fn abs<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::Tol) -> bool |
| 572 | where |
| 573 | A: FloatEq<B>, |
| 574 | { |
| 575 | a.eq_abs(b, tol) |
| 576 | } |
| 577 | |
| 578 | #[inline] |
| 579 | pub fn abs_all<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::AllTol) -> bool |
| 580 | where |
| 581 | A: FloatEqAll<B>, |
| 582 | { |
| 583 | a.eq_abs_all(b, tol) |
| 584 | } |
| 585 | |
| 586 | #[inline] |
| 587 | pub fn rel<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::Tol) -> bool |
| 588 | where |
| 589 | A: FloatEq<B>, |
| 590 | { |
| 591 | a.eq_rel(b, tol) |
| 592 | } |
| 593 | |
| 594 | #[inline] |
| 595 | pub fn rel_all<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::AllTol) -> bool |
| 596 | where |
| 597 | A: FloatEqAll<B>, |
| 598 | { |
| 599 | a.eq_rel_all(b, tol) |
| 600 | } |
| 601 | |
| 602 | #[inline] |
| 603 | pub fn rmax<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::Tol) -> bool |
| 604 | where |
| 605 | A: FloatEq<B>, |
| 606 | { |
| 607 | a.eq_rmax(b, tol) |
| 608 | } |
| 609 | |
| 610 | #[inline] |
| 611 | pub fn rmax_all<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::AllTol) -> bool |
| 612 | where |
| 613 | A: FloatEqAll<B>, |
| 614 | { |
| 615 | a.eq_rmax_all(b, tol) |
| 616 | } |
| 617 | |
| 618 | #[inline] |
| 619 | pub fn rmin<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::Tol) -> bool |
| 620 | where |
| 621 | A: FloatEq<B>, |
| 622 | { |
| 623 | a.eq_rmin(b, tol) |
| 624 | } |
| 625 | |
| 626 | #[inline] |
| 627 | pub fn rmin_all<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::AllTol) -> bool |
| 628 | where |
| 629 | A: FloatEqAll<B>, |
| 630 | { |
| 631 | a.eq_rmin_all(b, tol) |
| 632 | } |
| 633 | |
| 634 | #[inline] |
| 635 | pub fn r1st<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::Tol) -> bool |
| 636 | where |
| 637 | A: FloatEq<B>, |
| 638 | { |
| 639 | a.eq_r1st(b, tol) |
| 640 | } |
| 641 | |
| 642 | #[inline] |
| 643 | pub fn r1st_all<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::AllTol) -> bool |
| 644 | where |
| 645 | A: FloatEqAll<B>, |
| 646 | { |
| 647 | a.eq_r1st_all(b, tol) |
| 648 | } |
| 649 | |
| 650 | #[inline] |
| 651 | pub fn r2nd<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::Tol) -> bool |
| 652 | where |
| 653 | A: FloatEq<B>, |
| 654 | { |
| 655 | a.eq_r2nd(b, tol) |
| 656 | } |
| 657 | |
| 658 | #[inline] |
| 659 | pub fn r2nd_all<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::AllTol) -> bool |
| 660 | where |
| 661 | A: FloatEqAll<B>, |
| 662 | { |
| 663 | a.eq_r2nd_all(b, tol) |
| 664 | } |
| 665 | |
| 666 | #[inline] |
| 667 | pub fn ulps<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &UlpsTol<A::Tol>) -> bool |
| 668 | where |
| 669 | A: FloatEq<B>, |
| 670 | { |
| 671 | a.eq_ulps(b, tol) |
| 672 | } |
| 673 | |
| 674 | #[inline] |
| 675 | pub fn ulps_all<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &UlpsTol<A::AllTol>) -> bool |
| 676 | where |
| 677 | A: FloatEqAll<B>, |
| 678 | { |
| 679 | a.eq_ulps_all(b, tol) |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | #[doc(hidden)] |
| 684 | pub struct FloatCmpOpTol; |
| 685 | |
| 686 | #[doc(hidden)] |
| 687 | impl FloatCmpOpTol { |
| 688 | #[inline] |
| 689 | pub fn abs<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::Tol) -> A::DebugTol |
| 690 | where |
| 691 | A: FloatEq<B> + AssertFloatEq<B>, |
| 692 | { |
| 693 | a.debug_abs_tol(b, tol) |
| 694 | } |
| 695 | |
| 696 | #[inline] |
| 697 | pub fn abs_all<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::AllTol) -> A::AllDebugTol |
| 698 | where |
| 699 | A: FloatEqAll<B> + AssertFloatEqAll<B>, |
| 700 | { |
| 701 | a.debug_abs_all_tol(b, tol) |
| 702 | } |
| 703 | |
| 704 | #[inline] |
| 705 | pub fn rel<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::Tol) -> A::DebugTol |
| 706 | where |
| 707 | A: FloatEq<B> + AssertFloatEq<B>, |
| 708 | { |
| 709 | a.debug_rel_tol(b, tol) |
| 710 | } |
| 711 | |
| 712 | #[inline] |
| 713 | pub fn rel_all<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::AllTol) -> A::AllDebugTol |
| 714 | where |
| 715 | A: FloatEqAll<B> + AssertFloatEqAll<B>, |
| 716 | { |
| 717 | a.debug_rel_all_tol(b, tol) |
| 718 | } |
| 719 | |
| 720 | #[inline] |
| 721 | pub fn rmax<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::Tol) -> A::DebugTol |
| 722 | where |
| 723 | A: FloatEq<B> + AssertFloatEq<B>, |
| 724 | { |
| 725 | a.debug_rmax_tol(b, tol) |
| 726 | } |
| 727 | |
| 728 | #[inline] |
| 729 | pub fn rmax_all<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::AllTol) -> A::AllDebugTol |
| 730 | where |
| 731 | A: FloatEqAll<B> + AssertFloatEqAll<B>, |
| 732 | { |
| 733 | a.debug_rmax_all_tol(b, tol) |
| 734 | } |
| 735 | |
| 736 | #[inline] |
| 737 | pub fn rmin<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::Tol) -> A::DebugTol |
| 738 | where |
| 739 | A: FloatEq<B> + AssertFloatEq<B>, |
| 740 | { |
| 741 | a.debug_rmin_tol(b, tol) |
| 742 | } |
| 743 | |
| 744 | #[inline] |
| 745 | pub fn rmin_all<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::AllTol) -> A::AllDebugTol |
| 746 | where |
| 747 | A: FloatEqAll<B> + AssertFloatEqAll<B>, |
| 748 | { |
| 749 | a.debug_rmin_all_tol(b, tol) |
| 750 | } |
| 751 | |
| 752 | #[inline] |
| 753 | pub fn r1st<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::Tol) -> A::DebugTol |
| 754 | where |
| 755 | A: FloatEq<B> + AssertFloatEq<B>, |
| 756 | { |
| 757 | a.debug_r1st_tol(b, tol) |
| 758 | } |
| 759 | |
| 760 | #[inline] |
| 761 | pub fn r1st_all<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::AllTol) -> A::AllDebugTol |
| 762 | where |
| 763 | A: FloatEqAll<B> + AssertFloatEqAll<B>, |
| 764 | { |
| 765 | a.debug_r1st_all_tol(b, tol) |
| 766 | } |
| 767 | |
| 768 | #[inline] |
| 769 | pub fn r2nd<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::Tol) -> A::DebugTol |
| 770 | where |
| 771 | A: FloatEq<B> + AssertFloatEq<B>, |
| 772 | { |
| 773 | a.debug_r2nd_tol(b, tol) |
| 774 | } |
| 775 | |
| 776 | #[inline] |
| 777 | pub fn r2nd_all<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &A::AllTol) -> A::AllDebugTol |
| 778 | where |
| 779 | A: FloatEqAll<B> + AssertFloatEqAll<B>, |
| 780 | { |
| 781 | a.debug_r2nd_all_tol(b, tol) |
| 782 | } |
| 783 | |
| 784 | #[inline] |
| 785 | pub fn ulps<A: ?Sized, B: ?Sized>(a: &A, b: &B, tol: &UlpsTol<A::Tol>) -> UlpsTol<A::DebugTol> |
| 786 | where |
| 787 | A: FloatEq<B> + AssertFloatEq<B>, |
| 788 | UlpsTol<A::DebugTol>: Sized, |
| 789 | { |
| 790 | a.debug_ulps_tol(b, tol) |
| 791 | } |
| 792 | |
| 793 | #[inline] |
| 794 | pub fn ulps_all<A: ?Sized, B: ?Sized>( |
| 795 | a: &A, |
| 796 | b: &B, |
| 797 | tol: &UlpsTol<A::AllTol>, |
| 798 | ) -> UlpsTol<A::AllDebugTol> |
| 799 | where |
| 800 | A: FloatEqAll<B> + AssertFloatEqAll<B>, |
| 801 | UlpsTol<A::AllDebugTol>: Sized, |
| 802 | { |
| 803 | a.debug_ulps_all_tol(b, tol) |
| 804 | } |
| 805 | } |