Debian Codegraph @

third_party/rust/float-cmp/src/ulps_eq.rs

raw
1 // Copyright 2014-2018 Optimal Computing (NZ) Ltd.
2 // Licensed under the MIT license. See LICENSE for details.
3
4 use super::Ulps;
5
6 /// ApproxEqUlps is a trait for approximate equality comparisons.
7 /// The associated type Flt is a floating point type which implements Ulps, and is
8 /// required so that this trait can be implemented for compound types (e.g. vectors),
9 /// not just for the floats themselves.
10 pub trait ApproxEqUlps {
11 type Flt: Ulps;
12
13 /// This method tests for `self` and `other` values to be approximately equal
14 /// within ULPs (Units of Least Precision) floating point representations.
15 /// Differing signs are always unequal with this method, and zeroes are only
16 /// equal to zeroes. Use approx_eq() from the ApproxEq trait if that is more
17 /// appropriate.
18 fn approx_eq_ulps(&self, other: &Self, ulps: <Self::Flt as Ulps>::U) -> bool;
19
20 /// This method tests for `self` and `other` values to be not approximately
21 /// equal within ULPs (Units of Least Precision) floating point representations.
22 /// Differing signs are always unequal with this method, and zeroes are only
23 /// equal to zeroes. Use approx_eq() from the ApproxEq trait if that is more
24 /// appropriate.
25 #[inline]
26 fn approx_ne_ulps(&self, other: &Self, ulps: <Self::Flt as Ulps>::U) -> bool {
27 !self.approx_eq_ulps(other, ulps)
28 }
29 }
30
31 impl ApproxEqUlps for f32 {
32 type Flt = f32;
33
34 fn approx_eq_ulps(&self, other: &f32, ulps: i32) -> bool {
35 // -0 and +0 are drastically far in ulps terms, so
36 // we need a special case for that.
37 if *self==*other { return true; }
38
39 // Handle differing signs as a special case, even if
40 // they are very close, most people consider them
41 // unequal.
42 if self.is_sign_positive() != other.is_sign_positive() { return false; }
43
44 let diff: i32 = self.ulps(other);
45 diff >= -ulps && diff <= ulps
46 }
47 }
48
49 #[test]
50 fn f32_approx_eq_ulps_test1() {
51 let f: f32 = 0.1_f32;
52 let mut sum: f32 = 0.0_f32;
53 for _ in 0_isize..10_isize { sum += f; }
54 let product: f32 = f * 10.0_f32;
55 assert!(sum != product); // Should not be directly equal:
56 println!("Ulps Difference: {}",sum.ulps(&product));
57 assert!(sum.approx_eq_ulps(&product,1) == true); // But should be close
58 assert!(sum.approx_eq_ulps(&product,0) == false);
59 }
60 #[test]
61 fn f32_approx_eq_ulps_test2() {
62 let x: f32 = 1000000_f32;
63 let y: f32 = 1000000.1_f32;
64 assert!(x != y); // Should not be directly equal
65 println!("Ulps Difference: {}",x.ulps(&y));
66 assert!(x.approx_eq_ulps(&y,2) == true);
67 assert!(x.approx_eq_ulps(&y,1) == false);
68 }
69 #[test]
70 fn f32_approx_eq_ulps_test_zeroes() {
71 let x: f32 = 0.0_f32;
72 let y: f32 = -0.0_f32;
73 assert!(x.approx_eq_ulps(&y,0) == true);
74 }
75
76 impl ApproxEqUlps for f64 {
77 type Flt = f64;
78
79 fn approx_eq_ulps(&self, other: &f64, ulps: i64) -> bool {
80 // -0 and +0 are drastically far in ulps terms, so
81 // we need a special case for that.
82 if *self==*other { return true; }
83
84 // Handle differing signs as a special case, even if
85 // they are very close, most people consider them
86 // unequal.
87 if self.is_sign_positive() != other.is_sign_positive() { return false; }
88
89 let diff: i64 = self.ulps(other);
90 diff >= -ulps && diff <= ulps
91 }
92 }
93
94 #[test]
95 fn f64_approx_eq_ulps_test1() {
96 let f: f64 = 0.1_f64;
97 let mut sum: f64 = 0.0_f64;
98 for _ in 0_isize..10_isize { sum += f; }
99 let product: f64 = f * 10.0_f64;
100 assert!(sum != product); // Should not be directly equal:
101 println!("Ulps Difference: {}",sum.ulps(&product));
102 assert!(sum.approx_eq_ulps(&product,1) == true); // But should be close
103 assert!(sum.approx_eq_ulps(&product,0) == false);
104 }
105 #[test]
106 fn f64_approx_eq_ulps_test2() {
107 let x: f64 = 1000000_f64;
108 let y: f64 = 1000000.0000000003_f64;
109 assert!(x != y); // Should not be directly equal
110 println!("Ulps Difference: {}",x.ulps(&y));
111 assert!(x.approx_eq_ulps(&y,3) == true);
112 assert!(x.approx_eq_ulps(&y,2) == false);
113 }
114 #[test]
115 fn f64_approx_eq_ulps_test_zeroes() {
116 let x: f64 = 0.0_f64;
117 let y: f64 = -0.0_f64;
118 assert!(x.approx_eq_ulps(&y,0) == true);
119 }