1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
// Copyright 2015 Brendan Zabarauskas // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! A crate that provides facilities for testing the approximate equality of floating-point //! based types, using either relative difference, or units in the last place (ULPs) //! comparisons. //! //! You can also use the `approx_{eq, ne}!` `assert_approx_{eq, ne}!` macros to test for equality //! using a more positional style. //! //! ```rust //! #[macro_use] //! extern crate approx; //! use std::f64; //! //! # fn main() { //! relative_eq!(1.0, 1.0); //! relative_eq!(1.0, 1.0, epsilon = f64::EPSILON); //! relative_eq!(1.0, 1.0, max_relative = 1.0); //! relative_eq!(1.0, 1.0, epsilon = f64::EPSILON, max_relative = 1.0); //! relative_eq!(1.0, 1.0, max_relative = 1.0, epsilon = f64::EPSILON); //! //! ulps_eq!(1.0, 1.0); //! ulps_eq!(1.0, 1.0, epsilon = f64::EPSILON); //! ulps_eq!(1.0, 1.0, max_ulps = 4); //! ulps_eq!(1.0, 1.0, epsilon = f64::EPSILON, max_ulps = 4); //! ulps_eq!(1.0, 1.0, max_ulps = 4, epsilon = f64::EPSILON); //! # } //! ``` //! //! ## Implementing approximate equality for custom types //! //! The `ApproxEq` trait allows approximate equalities to be implemented on types, based on the //! fundamental floating point implementations. //! //! For example, we might want to be able to do approximate assertions on a complex number type: //! //! ```rust //! #[macro_use] //! extern crate approx; //! # use approx::ApproxEq; //! #[derive(Debug)] //! struct Complex<T> { //! x: T, //! i: T, //! } //! # impl<T: ApproxEq> ApproxEq for Complex<T> where T::Epsilon: Copy { //! # type Epsilon = T::Epsilon; //! # fn default_epsilon() -> T::Epsilon { T::default_epsilon() } //! # fn default_max_relative() -> T::Epsilon { T::default_max_relative() } //! # fn default_max_ulps() -> u32 { T::default_max_ulps() } //! # fn relative_eq(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool { T::relative_eq(&self.x, &other.x, epsilon, max_relative) && T::relative_eq(&self.i, &other.i, epsilon, max_relative) } //! # fn ulps_eq(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool { T::ulps_eq(&self.x, &other.x, epsilon, max_ulps) && T::ulps_eq(&self.i, &other.i, epsilon, max_ulps) } //! # } //! //! # fn main() { //! let x = Complex { x: 1.2, i: 2.3 }; //! //! assert_relative_eq!(x, x); //! assert_ulps_eq!(x, x, max_ulps = 4); //! # } //! ``` //! //! To do this we can implement `ApproxEq` generically in terms of a type parameter that also //! implements `ApproxEq`. This means that we can make comparisons for either `Complex<f32>` or //! `Complex<f64>`: //! //! ```rust //! # use approx::ApproxEq; //! # #[derive(Debug)] //! # struct Complex<T> { x: T, i: T, } //! # //! impl<T: ApproxEq> ApproxEq for Complex<T> where //! T::Epsilon: Copy, //! { //! type Epsilon = T::Epsilon; //! //! fn default_epsilon() -> T::Epsilon { //! T::default_epsilon() //! } //! //! fn default_max_relative() -> T::Epsilon { //! T::default_max_relative() //! } //! //! fn default_max_ulps() -> u32 { //! T::default_max_ulps() //! } //! //! fn relative_eq(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool { //! T::relative_eq(&self.x, &other.x, epsilon, max_relative) && //! T::relative_eq(&self.i, &other.i, epsilon, max_relative) //! } //! //! fn ulps_eq(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool { //! T::ulps_eq(&self.x, &other.x, epsilon, max_ulps) && //! T::ulps_eq(&self.i, &other.i, epsilon, max_ulps) //! } //! } //! ``` //! //! # References //! //! Floating point is hard! Thanks goes to these links for helping to make things a _little_ //! easier to understand: //! //! - [Comparing Floating Point Numbers, 2012 Edition] //! (https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/) //! - [The Floating Point Guide - Comparison](http://floating-point-gui.de/errors/comparison/) //! - [What Every Computer Scientist Should Know About Floating-Point Arithmetic] //! (https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) #![cfg_attr(feature="no_std", no_std)] #![cfg_attr(feature="no_std", feature(core_float))] #[cfg(feature="no_std")] use core as std; #[cfg(feature="no_std")] #[allow(unused_imports)] // Get a warning otherwise. This seems like a bug. use core::num::Float; mod macros; /// Equality comparisons based on floating point tolerances. pub trait ApproxEq: Sized { /// Used for specifying relative comparisons. type Epsilon; /// The default tolerance to use when testing values that are close together. /// /// This is used when no `epsilon` value is supplied to the `relative_eq` or `ulps_eq` macros. fn default_epsilon() -> Self::Epsilon; /// The default relative tolerance for testing values that are far-apart. /// /// This is used when no `max_relative` value is supplied to the `relative_eq` macro. fn default_max_relative() -> Self::Epsilon; /// The default ULPs to tolerate when testing values that are far-apart. /// /// This is used when no `max_relative` value is supplied to the `relative_eq` macro. fn default_max_ulps() -> u32; /// A test for equality that uses a relative comparison if the values are far apart. fn relative_eq(&self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon) -> bool; /// The inverse of `ApproxEq::relative_eq`. fn relative_ne(&self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon) -> bool { !Self::relative_eq(self, other, epsilon, max_relative) } /// A test for equality that uses units in the last place (ULP) if the values are far apart. fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool; /// The inverse of `ApproxEq::ulps_eq`. fn ulps_ne(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool { !Self::ulps_eq(self, other, epsilon, max_ulps) } } macro_rules! impl_float_relative_eq { ($T:ident, $U:ident) => { impl ApproxEq for $T { type Epsilon = $T; #[inline] fn default_epsilon() -> $T { std::$T::EPSILON } #[inline] fn default_max_relative() -> $T { std::$T::EPSILON } #[inline] fn default_max_ulps() -> u32 { 4 } #[inline] fn relative_eq(&self, other: &$T, epsilon: $T, max_relative: $T) -> bool { // Implementation based on: [Comparing Floating Point Numbers, 2012 Edition] // (https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/) // Handle infinities if self == other { return true; } let abs_diff = $T::abs(self - other); // For when the numbers are really close together if abs_diff <= epsilon { return true }; // Use a relative difference comparison let abs_self = $T::abs(*self); let abs_other = $T::abs(*other); let largest = if abs_other > abs_self { abs_other } else { abs_self }; abs_diff <= largest * max_relative } #[inline] fn ulps_eq(&self, other: &$T, epsilon: $T, max_ulps: u32) -> bool { // Implementation based on: [Comparing Floating Point Numbers, 2012 Edition] // (https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/) // For when the numbers are really close together if $T::abs(self - other) <= epsilon { return true } // Trivial negative sign check if self.signum() != other.signum() { // Handle -0 == +0 return self == other; } let int_self: $U = unsafe { std::mem::transmute(*self) }; let int_other: $U = unsafe { std::mem::transmute(*other) }; // ULPS difference comparison $U::abs(int_self - int_other) < max_ulps as $U } } } } impl_float_relative_eq!(f32, i32); impl_float_relative_eq!(f64, i64); impl<'a, T: ApproxEq> ApproxEq for &'a T { type Epsilon = T::Epsilon; #[inline] fn default_epsilon() -> Self::Epsilon { T::default_epsilon() } #[inline] fn default_max_relative() -> Self::Epsilon { T::default_max_relative() } #[inline] fn default_max_ulps() -> u32 { T::default_max_ulps() } #[inline] fn relative_eq(&self, other: &&'a T, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool { T::relative_eq(*self, *other, epsilon, max_relative) } #[inline] fn ulps_eq(&self, other: &&'a T, epsilon: T::Epsilon, max_ulps: u32) -> bool { T::ulps_eq(*self, *other, epsilon, max_ulps) } } impl<'a, T: ApproxEq> ApproxEq for &'a mut T { type Epsilon = T::Epsilon; #[inline] fn default_epsilon() -> Self::Epsilon { T::default_epsilon() } #[inline] fn default_max_relative() -> Self::Epsilon { T::default_max_relative() } #[inline] fn default_max_ulps() -> u32 { T::default_max_ulps() } #[inline] fn relative_eq(&self, other: &&'a mut T, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool { T::relative_eq(*self, *other, epsilon, max_relative) } #[inline] fn ulps_eq(&self, other: &&'a mut T, epsilon: T::Epsilon, max_ulps: u32) -> bool { T::ulps_eq(*self, *other, epsilon, max_ulps) } } /// The requisite parameters for testing for approximate equality. pub struct Relative<'a, T: 'a + ApproxEq> { /// The left-hand side of the approximate equality comparison. pub lhs: &'a T, /// The right-hand side of the approximate equality comparison. pub rhs: &'a T, /// The tolerance to use when testing values that are close together. pub epsilon: T::Epsilon, /// The relative tolerance for testing values that are far-apart. pub max_relative: T::Epsilon, } impl<'a, T> Relative<'a, T> where T: ApproxEq { /// The beginning of a chained equality expression, using a relative based comparison. /// /// ```rust /// use std::f64; /// use approx::Relative; /// /// Relative::new(&1.0, &1.0).eq(); /// Relative::new(&1.0, &1.0).epsilon(f64::EPSILON).eq(); /// Relative::new(&1.0, &1.0).max_relative(1.0).eq(); /// Relative::new(&1.0, &1.0).epsilon(f64::EPSILON).max_relative(1.0).eq(); /// Relative::new(&1.0, &1.0).max_relative(1.0).epsilon(f64::EPSILON).eq(); /// ``` #[inline] pub fn new(lhs: &'a T, rhs: &'a T) -> Relative<'a, T> { Relative { lhs: lhs, rhs: rhs, epsilon: T::default_epsilon(), max_relative: T::default_max_relative(), } } /// Replace the epsilon value with the one specified. #[inline] pub fn epsilon(self, epsilon: T::Epsilon) -> Relative<'a, T> { Relative { epsilon: epsilon, ..self } } /// Replace the maximum relative value with the one specified. #[inline] pub fn max_relative(self, max_relative: T::Epsilon) -> Relative<'a, T> { Relative { max_relative: max_relative, ..self } } /// Peform the equality comparison #[inline] pub fn eq(self) -> bool { let Relative { lhs, rhs, epsilon, max_relative } = self; T::relative_eq(lhs, rhs, epsilon, max_relative) } /// Peform the inequality comparison #[inline] pub fn ne(self) -> bool { let Relative { lhs, rhs, epsilon, max_relative } = self; T::relative_ne(lhs, rhs, epsilon, max_relative) } } /// The requisite parameters for testing for approximate equality. pub struct Ulps<'a, T: 'a + ApproxEq> { /// The left-hand side of the approximate equality comparison. pub lhs: &'a T, /// The right-hand side of the approximate equality comparison. pub rhs: &'a T, /// The tolerance to use when testing values that are close together. pub epsilon: T::Epsilon, /// The ULPs to tolerate when testing values that are far-apart. pub max_ulps: u32, } impl<'a, T> Ulps<'a, T> where T: ApproxEq { /// The beginning of a chained equality expression, using an ULPs based comparison. /// /// ```rust /// use std::f64; /// use approx::Ulps; /// /// Ulps::new(&1.0, &1.0).eq(); /// Ulps::new(&1.0, &1.0).epsilon(f64::EPSILON).eq(); /// Ulps::new(&1.0, &1.0).max_ulps(4).eq(); /// Ulps::new(&1.0, &1.0).epsilon(f64::EPSILON).max_ulps(4).eq(); /// Ulps::new(&1.0, &1.0).max_ulps(4).epsilon(f64::EPSILON).eq(); /// ``` #[inline] pub fn new(lhs: &'a T, rhs: &'a T) -> Ulps<'a, T> { Ulps { lhs: lhs, rhs: rhs, epsilon: T::default_epsilon(), max_ulps: T::default_max_ulps(), } } /// Replace the epsilon value with the one specified. #[inline] pub fn epsilon(self, epsilon: T::Epsilon) -> Ulps<'a, T> { Ulps { epsilon: epsilon, ..self } } /// Replace the max ulps value with the one specified. #[inline] pub fn max_ulps(self, max_ulps:u32) -> Ulps<'a, T> { Ulps { max_ulps: max_ulps, ..self } } /// Peform the equality comparison #[inline] pub fn eq(self) -> bool { let Ulps { lhs, rhs, epsilon, max_ulps } = self; T::ulps_eq(lhs, rhs, epsilon, max_ulps) } /// Peform the inequality comparison #[inline] pub fn ne(self) -> bool { let Ulps { lhs, rhs, epsilon, max_ulps } = self; T::ulps_ne(lhs, rhs, epsilon, max_ulps) } }