| 1 | // Copyright 2014-2020 Optimal Computing (NZ) Ltd. |
| 2 | // Licensed under the MIT license. See LICENSE for details. |
| 3 | |
| 4 | use core::cmp::PartialOrd; |
| 5 | use core::ops::{Div, Neg, Sub}; |
| 6 | use num_traits::Zero; |
| 7 | |
| 8 | /// ApproxEqRatio is a trait for approximate equality comparisons bounding the ratio |
| 9 | /// of the difference to the larger. |
| 10 | pub trait ApproxEqRatio: |
| 11 | Div<Output = Self> + Sub<Output = Self> + Neg<Output = Self> + PartialOrd + Zero + Sized + Copy |
| 12 | { |
| 13 | /// This method tests if `self` and `other` are nearly equal by bounding the |
| 14 | /// difference between them to some number much less than the larger of the two. |
| 15 | /// This bound is set as the ratio of the difference to the larger. |
| 16 | fn approx_eq_ratio(&self, other: &Self, ratio: Self) -> bool { |
| 17 | // Not equal if signs are not equal |
| 18 | if *self < Self::zero() && *other > Self::zero() { |
| 19 | return false; |
| 20 | } |
| 21 | if *self > Self::zero() && *other < Self::zero() { |
| 22 | return false; |
| 23 | } |
| 24 | |
| 25 | // Handle all zero cases |
| 26 | match (*self == Self::zero(), *other == Self::zero()) { |
| 27 | (true, true) => return true, |
| 28 | (true, false) => return false, |
| 29 | (false, true) => return false, |
| 30 | _ => {} |
| 31 | } |
| 32 | |
| 33 | // abs |
| 34 | let (s, o) = if *self < Self::zero() { |
| 35 | (-*self, -*other) |
| 36 | } else { |
| 37 | (*self, *other) |
| 38 | }; |
| 39 | |
| 40 | let (smaller, larger) = if s < o { (s, o) } else { (o, s) }; |
| 41 | let difference: Self = larger.sub(smaller); |
| 42 | let actual_ratio: Self = difference.div(larger); |
| 43 | actual_ratio < ratio |
| 44 | } |
| 45 | |
| 46 | /// This method tests if `self` and `other` are not nearly equal by bounding the |
| 47 | /// difference between them to some number much less than the larger of the two. |
| 48 | /// This bound is set as the ratio of the difference to the larger. |
| 49 | #[inline ] |
| 50 | fn approx_ne_ratio(&self, other: &Self, ratio: Self) -> bool { |
| 51 | !self.approx_eq_ratio(other, ratio) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | impl ApproxEqRatio for f32 {} |
| 56 | |
| 57 | #[test ] |
| 58 | fn f32_approx_eq_ratio_test1() { |
| 59 | let x: f32 = 0.00004_f32; |
| 60 | let y: f32 = 0.00004001_f32; |
| 61 | assert!(x.approx_eq_ratio(&y, 0.00025)); |
| 62 | assert!(y.approx_eq_ratio(&x, 0.00025)); |
| 63 | assert!(x.approx_ne_ratio(&y, 0.00024)); |
| 64 | assert!(y.approx_ne_ratio(&x, 0.00024)); |
| 65 | } |
| 66 | |
| 67 | #[test ] |
| 68 | fn f32_approx_eq_ratio_test2() { |
| 69 | let x: f32 = 0.00000000001_f32; |
| 70 | let y: f32 = 0.00000000005_f32; |
| 71 | assert!(x.approx_eq_ratio(&y, 0.81)); |
| 72 | assert!(y.approx_ne_ratio(&x, 0.79)); |
| 73 | } |
| 74 | |
| 75 | #[test ] |
| 76 | fn f32_approx_eq_ratio_test_zero_eq_zero_returns_true() { |
| 77 | let x: f32 = 0.0_f32; |
| 78 | assert!(x.approx_eq_ratio(&x, 0.1) == true); |
| 79 | } |
| 80 | |
| 81 | #[test ] |
| 82 | fn f32_approx_eq_ratio_test_zero_ne_zero_returns_false() { |
| 83 | let x: f32 = 0.0_f32; |
| 84 | assert!(x.approx_ne_ratio(&x, 0.1) == false); |
| 85 | } |
| 86 | |
| 87 | #[test ] |
| 88 | fn f32_approx_eq_ratio_test_against_a_zero_is_false() { |
| 89 | let x: f32 = 0.0_f32; |
| 90 | let y: f32 = 0.1_f32; |
| 91 | assert!(x.approx_eq_ratio(&y, 0.1) == false); |
| 92 | assert!(y.approx_eq_ratio(&x, 0.1) == false); |
| 93 | } |
| 94 | |
| 95 | #[test ] |
| 96 | fn f32_approx_eq_ratio_test_negative_numbers() { |
| 97 | let x: f32 = -3.0_f32; |
| 98 | let y: f32 = -4.0_f32; |
| 99 | // -3 and -4 should not be equal at a ratio of 0.1 |
| 100 | assert!(x.approx_eq_ratio(&y, 0.1) == false); |
| 101 | } |
| 102 | |
| 103 | impl ApproxEqRatio for f64 {} |
| 104 | |
| 105 | #[test ] |
| 106 | fn f64_approx_eq_ratio_test1() { |
| 107 | let x: f64 = 0.000000004_f64; |
| 108 | let y: f64 = 0.000000004001_f64; |
| 109 | assert!(x.approx_eq_ratio(&y, 0.00025)); |
| 110 | assert!(y.approx_eq_ratio(&x, 0.00025)); |
| 111 | assert!(x.approx_ne_ratio(&y, 0.00024)); |
| 112 | assert!(y.approx_ne_ratio(&x, 0.00024)); |
| 113 | } |
| 114 | |
| 115 | #[test ] |
| 116 | fn f64_approx_eq_ratio_test2() { |
| 117 | let x: f64 = 0.0000000000000001_f64; |
| 118 | let y: f64 = 0.0000000000000005_f64; |
| 119 | assert!(x.approx_eq_ratio(&y, 0.81)); |
| 120 | assert!(y.approx_ne_ratio(&x, 0.79)); |
| 121 | } |
| 122 | |
| 123 | #[test ] |
| 124 | fn f64_approx_eq_ratio_test_zero_eq_zero_returns_true() { |
| 125 | let x: f64 = 0.0_f64; |
| 126 | assert!(x.approx_eq_ratio(&x, 0.1) == true); |
| 127 | } |
| 128 | |
| 129 | #[test ] |
| 130 | fn f64_approx_eq_ratio_test_zero_ne_zero_returns_false() { |
| 131 | let x: f64 = 0.0_f64; |
| 132 | assert!(x.approx_ne_ratio(&x, 0.1) == false); |
| 133 | } |
| 134 | |
| 135 | #[test ] |
| 136 | fn f64_approx_eq_ratio_test_negative_numbers() { |
| 137 | let x: f64 = -3.0_f64; |
| 138 | let y: f64 = -4.0_f64; |
| 139 | // -3 and -4 should not be equal at a ratio of 0.1 |
| 140 | assert!(x.approx_eq_ratio(&y, 0.1) == false); |
| 141 | } |
| 142 | |