| 1 |
// Copyright 2014-2018 Optimal Computing (NZ) Ltd. |
| 2 |
// Licensed under the MIT license. See LICENSE for details. |
| 3 |
|
| 4 |
use std::{f32,f64}; |
| 5 |
use super::Ulps; |
| 6 |
|
| 7 |
/// ApproxEq is a trait for approximate equality comparisons. |
| 8 |
pub trait ApproxEq: Sized { |
| 9 |
/// The Margin type defines a margin within which two values are to be |
| 10 |
/// considered approximately equal. It must implement Default so that |
| 11 |
/// approx_eq() can be called on unknown types. |
| 12 |
type Margin: Copy + Default; |
| 13 |
|
| 14 |
/// This method tests for `self` and `other` values to be approximately equal |
| 15 |
/// within `margin`. |
| 16 |
fn approx_eq<M: Into<Self::Margin>>(self, other: Self, margin: M) -> bool; |
| 17 |
|
| 18 |
/// This method tests for `self` and `other` values to be not approximately |
| 19 |
/// equal within `margin`. |
| 20 |
fn approx_ne<M: Into<Self::Margin>>(self, other: Self, margin: M) -> bool { |
| 21 |
!self.approx_eq(other, margin) |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
/// This type defines a margin within two f32s might be considered equal |
| 26 |
/// and is intended as the associated type for the `ApproxEq` trait. |
| 27 |
/// |
| 28 |
/// Two methods are used to determine approximate equality. |
| 29 |
/// |
| 30 |
/// First an epsilon method is used, considering them approximately equal if they |
| 31 |
/// differ by <= `epsilon`. This will only succeed for very small numbers. |
| 32 |
/// Note that it may succeed even if the parameters are of differing signs straddling |
| 33 |
/// zero. |
| 34 |
/// |
| 35 |
/// The second method considers how many ULPs (units of least precision, units in |
| 36 |
/// the last place, which is the integer number of floating point representations |
| 37 |
/// that the parameters are separated by) different the parameters are and considers |
| 38 |
/// them approximately equal if this is <= `ulps`. For large floating point numbers, |
| 39 |
/// an ULP can be a rather large gap, but this kind of comparison is necessary |
| 40 |
/// because floating point operations must round to the nearest representable value |
| 41 |
/// and so larger floating point values accumulate larger errors. |
| 42 |
#[repr(C)] |
| 43 |
#[derive(Debug, Clone, Copy)] |
| 44 |
pub struct F32Margin { |
| 45 |
pub epsilon: f32, |
| 46 |
pub ulps: i32 |
| 47 |
} |
| 48 |
impl Default for F32Margin { |
| 49 |
#[inline] |
| 50 |
fn default() -> F32Margin { |
| 51 |
F32Margin { |
| 52 |
epsilon: f32::EPSILON, |
| 53 |
ulps: 4 |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
impl F32Margin { |
| 58 |
#[inline] |
| 59 |
pub fn zero() -> F32Margin { |
| 60 |
F32Margin { |
| 61 |
epsilon: 0.0, |
| 62 |
ulps: 0 |
| 63 |
} |
| 64 |
} |
| 65 |
pub fn epsilon(self, epsilon: f32) -> Self { |
| 66 |
F32Margin { |
| 67 |
epsilon: epsilon, |
| 68 |
..self |
| 69 |
} |
| 70 |
} |
| 71 |
pub fn ulps(self, ulps: i32) -> Self { |
| 72 |
F32Margin { |
| 73 |
ulps: ulps, |
| 74 |
..self |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
impl From<(f32, i32)> for F32Margin { |
| 79 |
fn from(m: (f32, i32)) -> F32Margin { |
| 80 |
F32Margin { |
| 81 |
epsilon: m.0, |
| 82 |
ulps: m.1 |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
impl ApproxEq for f32 { |
| 88 |
type Margin = F32Margin; |
| 89 |
|
| 90 |
fn approx_eq<M: Into<Self::Margin>>(self, other: f32, margin: M) -> bool { |
| 91 |
let margin = margin.into(); |
| 92 |
|
| 93 |
// Check for exact equality first. This is often true, and so we get the |
| 94 |
// performance benefit of only doing one compare in most cases. |
| 95 |
self==other || |
| 96 |
|
| 97 |
// Perform epsilon comparison next |
| 98 |
((self - other).abs() <= margin.epsilon) || |
| 99 |
|
| 100 |
{ |
| 101 |
// Perform ulps comparion last |
| 102 |
let diff: i32 = self.ulps(&other); |
| 103 |
saturating_abs_i32!(diff) <= margin.ulps |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
#[test] |
| 109 |
fn f32_approx_eq_test1() { |
| 110 |
let f: f32 = 0.0_f32; |
| 111 |
let g: f32 = -0.0000000000000005551115123125783_f32; |
| 112 |
assert!(f != g); // Should not be directly equal |
| 113 |
assert!(f.approx_eq(g, (f32::EPSILON, 0)) == true); |
| 114 |
} |
| 115 |
#[test] |
| 116 |
fn f32_approx_eq_test2() { |
| 117 |
let f: f32 = 0.0_f32; |
| 118 |
let g: f32 = -0.0_f32; |
| 119 |
assert!(f.approx_eq(g, (f32::EPSILON, 0)) == true); |
| 120 |
} |
| 121 |
#[test] |
| 122 |
fn f32_approx_eq_test3() { |
| 123 |
let f: f32 = 0.0_f32; |
| 124 |
let g: f32 = 0.00000000000000001_f32; |
| 125 |
assert!(f.approx_eq(g, (f32::EPSILON, 0)) == true); |
| 126 |
} |
| 127 |
#[test] |
| 128 |
fn f32_approx_eq_test4() { |
| 129 |
let f: f32 = 0.00001_f32; |
| 130 |
let g: f32 = 0.00000000000000001_f32; |
| 131 |
assert!(f.approx_eq(g, (f32::EPSILON, 0)) == false); |
| 132 |
} |
| 133 |
#[test] |
| 134 |
fn f32_approx_eq_test5() { |
| 135 |
let f: f32 = 0.1_f32; |
| 136 |
let mut sum: f32 = 0.0_f32; |
| 137 |
for _ in 0_isize..10_isize { sum += f; } |
| 138 |
let product: f32 = f * 10.0_f32; |
| 139 |
assert!(sum != product); // Should not be directly equal: |
| 140 |
println!("Ulps Difference: {}",sum.ulps(&product)); |
| 141 |
assert!(sum.approx_eq(product, (f32::EPSILON, 1)) == true); |
| 142 |
assert!(sum.approx_eq(product, F32Margin::zero()) == false); |
| 143 |
} |
| 144 |
#[test] |
| 145 |
fn f32_approx_eq_test6() { |
| 146 |
let x: f32 = 1000000_f32; |
| 147 |
let y: f32 = 1000000.1_f32; |
| 148 |
assert!(x != y); // Should not be directly equal |
| 149 |
assert!(x.approx_eq(y, (0.0, 2)) == true); // 2 ulps does it |
| 150 |
// epsilon method no good here: |
| 151 |
assert!(x.approx_eq(y, (1000.0 * f32::EPSILON, 0)) == false); |
| 152 |
} |
| 153 |
|
| 154 |
/// This type defines a margin within two f32s might be considered equal |
| 155 |
/// and is intended as the associated type for the `ApproxEq` trait. |
| 156 |
/// |
| 157 |
/// Two methods are used to determine approximate equality. |
| 158 |
/// |
| 159 |
/// First an epsilon method is used, considering them approximately equal if they |
| 160 |
/// differ by <= `epsilon`. This will only succeed for very small numbers. |
| 161 |
/// Note that it may succeed even if the parameters are of differing signs straddling |
| 162 |
/// zero. |
| 163 |
/// |
| 164 |
/// The second method considers how many ULPs (units of least precision, units in |
| 165 |
/// the last place, which is the integer number of floating point representations |
| 166 |
/// that the parameters are separated by) different the parameters are and considers |
| 167 |
/// them approximately equal if this <= `ulps`. For large floating point numbers, |
| 168 |
/// an ULP can be a rather large gap, but this kind of comparison is necessary |
| 169 |
/// because floating point operations must round to the nearest representable value |
| 170 |
/// and so larger floating point values accumulate larger errors. |
| 171 |
#[derive(Debug, Clone, Copy)] |
| 172 |
pub struct F64Margin { |
| 173 |
pub epsilon: f64, |
| 174 |
pub ulps: i64 |
| 175 |
} |
| 176 |
impl Default for F64Margin { |
| 177 |
#[inline] |
| 178 |
fn default() -> F64Margin { |
| 179 |
F64Margin { |
| 180 |
epsilon: f64::EPSILON, |
| 181 |
ulps: 4 |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
impl F64Margin { |
| 186 |
#[inline] |
| 187 |
pub fn zero() -> F64Margin { |
| 188 |
F64Margin { |
| 189 |
epsilon: 0.0, |
| 190 |
ulps: 0 |
| 191 |
} |
| 192 |
} |
| 193 |
pub fn epsilon(self, epsilon: f64) -> Self { |
| 194 |
F64Margin { |
| 195 |
epsilon: epsilon, |
| 196 |
..self |
| 197 |
} |
| 198 |
} |
| 199 |
pub fn ulps(self, ulps: i64) -> Self { |
| 200 |
F64Margin { |
| 201 |
ulps: ulps, |
| 202 |
..self |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
impl From<(f64, i64)> for F64Margin { |
| 207 |
fn from(m: (f64, i64)) -> F64Margin { |
| 208 |
F64Margin { |
| 209 |
epsilon: m.0, |
| 210 |
ulps: m.1 |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
impl ApproxEq for f64 { |
| 216 |
type Margin = F64Margin; |
| 217 |
|
| 218 |
fn approx_eq<M: Into<Self::Margin>>(self, other: f64, margin: M) -> bool { |
| 219 |
let margin = margin.into(); |
| 220 |
|
| 221 |
// Check for exact equality first. This is often true, and so we get the |
| 222 |
// performance benefit of only doing one compare in most cases. |
| 223 |
self==other || |
| 224 |
|
| 225 |
// Perform epsilon comparison next |
| 226 |
((self - other).abs() <= margin.epsilon) || |
| 227 |
|
| 228 |
{ |
| 229 |
// Perform ulps comparion last |
| 230 |
let diff: i64 = self.ulps(&other); |
| 231 |
saturating_abs_i64!(diff) <= margin.ulps |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
#[test] |
| 237 |
fn f64_approx_eq_test1() { |
| 238 |
let f: f64 = 0.0_f64; |
| 239 |
let g: f64 = -0.0000000000000005551115123125783_f64; |
| 240 |
assert!(f != g); // Should not be directly equal |
| 241 |
assert!(f.approx_eq(g, (3.0 * f64::EPSILON, 0)) == true); // 3e is enough |
| 242 |
// ulps test wont ever call these equal |
| 243 |
} |
| 244 |
#[test] |
| 245 |
fn f64_approx_eq_test2() { |
| 246 |
let f: f64 = 0.0_f64; |
| 247 |
let g: f64 = -0.0_f64; |
| 248 |
assert!(f.approx_eq(g, (f64::EPSILON, 0)) == true); |
| 249 |
} |
| 250 |
#[test] |
| 251 |
fn f64_approx_eq_test3() { |
| 252 |
let f: f64 = 0.0_f64; |
| 253 |
let g: f64 = 1e-17_f64; |
| 254 |
assert!(f.approx_eq(g, (f64::EPSILON, 0)) == true); |
| 255 |
} |
| 256 |
#[test] |
| 257 |
fn f64_approx_eq_test4() { |
| 258 |
let f: f64 = 0.00001_f64; |
| 259 |
let g: f64 = 0.00000000000000001_f64; |
| 260 |
assert!(f.approx_eq(g, (f64::EPSILON, 0)) == false); |
| 261 |
} |
| 262 |
#[test] |
| 263 |
fn f64_approx_eq_test5() { |
| 264 |
let f: f64 = 0.1_f64; |
| 265 |
let mut sum: f64 = 0.0_f64; |
| 266 |
for _ in 0_isize..10_isize { sum += f; } |
| 267 |
let product: f64 = f * 10.0_f64; |
| 268 |
assert!(sum != product); // Should not be directly equal: |
| 269 |
println!("Ulps Difference: {}",sum.ulps(&product)); |
| 270 |
assert!(sum.approx_eq(product, (f64::EPSILON, 0)) == true); |
| 271 |
assert!(sum.approx_eq(product, (0.0, 1)) == true); |
| 272 |
} |
| 273 |
#[test] |
| 274 |
fn f64_approx_eq_test6() { |
| 275 |
let x: f64 = 1000000_f64; |
| 276 |
let y: f64 = 1000000.0000000003_f64; |
| 277 |
assert!(x != y); // Should not be directly equal |
| 278 |
println!("Ulps Difference: {}",x.ulps(&y)); |
| 279 |
assert!(x.approx_eq(y, (0.0, 3)) == true); |
| 280 |
} |
| 281 |
#[test] |
| 282 |
fn f64_code_triggering_issue_20() { |
| 283 |
assert_eq!((-25.0f64).approx_eq(25.0, (0.00390625, 1)), false); |
| 284 |
} |