Debian Codegraph @

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

raw
1 // Copyright 2014-2018 Optimal Computing (NZ) Ltd.
2 // Licensed under the MIT license. See LICENSE for details.
3
4 //! # float-cmp
5 //!
6 //! float-cmp defines and implements traits for approximate comparison of floating point types
7 //! which have fallen away from exact equality due to the limited precision available within
8 //! floating point representations. Implementations of these traits are provided for `f32`
9 //! and `f64` types.
10 //!
11 //! When I was a kid in the '80s, the programming rule was "Never compare floating point
12 //! numbers". If you can follow that rule and still get the outcome you desire, then more
13 //! power to you. However, if you really do need to compare them, this crate provides a
14 //! reasonable way to do so.
15 //!
16 //! Another crate `efloat` offers another solution by providing a floating point type that
17 //! tracks its error bounds as operations are performed on it, and thus can implement the
18 //! `ApproxEq` trait in this crate more accurately, without specifying a `Margin`.
19 //!
20 //! The recommended go-to solution (although it may not be appropriate in all cases) is the
21 //! `approx_eq()` function in the `ApproxEq` trait (or better yet, the macros). For `f32`
22 //! and `f64`, the `F32Margin` and `F64Margin` types are provided for specifying margins as
23 //! both an epsilon value and an ULPs value, and defaults are provided via `Default`
24 //! (although there is no perfect default value that is always appropriate, so beware).
25 //!
26 //! Several other traits are provided including `Ulps`, `ApproxEqUlps`, `ApproxOrdUlps`, and
27 //! `ApproxEqRatio`.
28 //!
29 //! ## The problem
30 //!
31 //! Floating point operations must round answers to the nearest representable number. Multiple
32 //! operations may result in an answer different from what you expect. In the following example,
33 //! the assert will fail, even though the printed output says "0.45 == 0.45":
34 //!
35 //! ```should_panic
36 //! # extern crate float_cmp;
37 //! # use float_cmp::ApproxEq;
38 //! # fn main() {
39 //! let a: f32 = 0.15 + 0.15 + 0.15;
40 //! let b: f32 = 0.1 + 0.1 + 0.25;
41 //! println!("{} == {}", a, b);
42 //! assert!(a==b) // Fails, because they are not exactly equal
43 //! # }
44 //! ```
45 //!
46 //! This fails because the correct answer to most operations isn't exactly representable, and so
47 //! your computer's processor chooses to represent the answer with the closest value it has
48 //! available. This introduces error, and this error can accumulate as multiple operations are
49 //! performed.
50 //!
51 //! ## The solution
52 //!
53 //! With `ApproxEq`, we can get the answer we intend:
54 //!
55 //! ```
56 //! # #[macro_use]
57 //! # extern crate float_cmp;
58 //! # use float_cmp::{ApproxEq, F32Margin};
59 //! # fn main() {
60 //! let a: f32 = 0.15 + 0.15 + 0.15;
61 //! let b: f32 = 0.1 + 0.1 + 0.25;
62 //! println!("{} == {}", a, b);
63 //! // They are equal, within 2 ulps
64 //! assert!( approx_eq!(f32, a, b, ulps = 2) );
65 //! # }
66 //! ```
67 //!
68 //! ## Some explanation
69 //!
70 //! We use the term ULP (units of least precision, or units in the last place) to mean the
71 //! difference between two adjacent floating point representations (adjacent meaning that there is
72 //! no floating point number between them). This term is borrowed from prior work (personally I
73 //! would have chosen "quanta"). The size of an ULP (measured as a float) varies
74 //! depending on the exponents of the floating point numbers in question. That is a good thing,
75 //! because as numbers fall away from equality due to the imprecise nature of their representation,
76 //! they fall away in ULPs terms, not in absolute terms. Pure epsilon-based comparisons are
77 //! absolute and thus don't map well to the nature of the additive error issue. They work fine
78 //! for many ranges of numbers, but not for others (consider comparing -0.0000000028
79 //! to +0.00000097).
80 //!
81 //! ## Using this crate
82 //!
83 //! You can use the `ApproxEq` trait directly like so:
84 //!
85 //! ```
86 //! # extern crate float_cmp;
87 //! # use float_cmp::{ApproxEq, F32Margin};
88 //! # fn main() {
89 //! # let a: f32 = 0.15 + 0.15 + 0.15;
90 //! # let b: f32 = 0.1 + 0.1 + 0.25;
91 //! assert!( a.approx_eq(b, F32Margin { ulps: 2, epsilon: 0.0 }) );
92 //! # }
93 //! ```
94 //!
95 //! We have implemented `From<(f32,i32)>` for `F32Margin` (and similarly for `F64Margin`)
96 //! so you can use this shorthand:
97 //!
98 //! ```
99 //! # extern crate float_cmp;
100 //! # use float_cmp::{ApproxEq, F32Margin};
101 //! # fn main() {
102 //! # let a: f32 = 0.15 + 0.15 + 0.15;
103 //! # let b: f32 = 0.1 + 0.1 + 0.25;
104 //! assert!( a.approx_eq(b, (0.0, 2)) );
105 //! # }
106 //! ```
107 //!
108 //! With macros, it is easier to be explicit about which type of margin you wish to set,
109 //! without mentioning the other one (the other one will be zero). But the downside is
110 //! that you have to specify the type you are dealing with:
111 //!
112 //! ```
113 //! # #[macro_use]
114 //! # extern crate float_cmp;
115 //! # use float_cmp::{ApproxEq, F32Margin};
116 //! # fn main() {
117 //! # let a: f32 = 0.15 + 0.15 + 0.15;
118 //! # let b: f32 = 0.1 + 0.1 + 0.25;
119 //! assert!( approx_eq!(f32, a, b, ulps = 2) );
120 //! assert!( approx_eq!(f32, a, b, epsilon = 0.00000003) );
121 //! assert!( approx_eq!(f32, a, b, epsilon = 0.00000003, ulps = 2) );
122 //! assert!( approx_eq!(f32, a, b, (0.0, 2)) );
123 //! assert!( approx_eq!(f32, a, b, F32Margin { epsilon: 0.0, ulps: 2 }) );
124 //! assert!( approx_eq!(f32, a, b, F32Margin::default()) );
125 //! assert!( approx_eq!(f32, a, b) ); // uses the default
126 //! # }
127 //! ```
128 //!
129 //! For most cases, I recommend you use a smallish integer for the `ulps` parameter (1 to 5
130 //! or so), and a similar small multiple of the floating point's EPSILON constant (1.0 to 5.0
131 //! or so), but there are *plenty* of cases where this is insufficient.
132 //!
133 //! ## Implementing these traits
134 //!
135 //! You can implement `ApproxEq` for your own complex types like shown below.
136 //! The floating point type `F` must be `Copy`, but for large types you can implement
137 //! it for references to your type as shown.
138 //!
139 //! ```
140 //! use float_cmp::ApproxEq;
141 //!
142 //! pub struct Vec2<F> {
143 //! pub x: F,
144 //! pub y: F,
145 //! }
146 //!
147 //! impl<'a, M: Copy + Default, F: Copy + ApproxEq<Margin=M>> ApproxEq for &'a Vec2<F> {
148 //! type Margin = M;
149 //!
150 //! fn approx_eq<T: Into<Self::Margin>>(self, other: Self, margin: T) -> bool {
151 //! let margin = margin.into();
152 //! self.x.approx_eq(other.x, margin)
153 //! && self.y.approx_eq(other.y, margin)
154 //! }
155 //! }
156 //! ```
157 //!
158 //! ## Non floating-point types
159 //!
160 //! `ApproxEq` can be implemented for non floating-point types as well, since `Margin` is
161 //! an associated type.
162 //!
163 //! The `efloat` crate implements (or soon will implement) `ApproxEq` for a compound type
164 //! that tracks floating point error bounds by checking if the error bounds overlap.
165 //! In that case `type Margin = ()`.
166 //!
167 //! ## Inspiration
168 //!
169 //! This crate was inspired by this Random ASCII blog post:
170 //!
171 //! [https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/)
172
173 #[cfg(feature="num-traits")]
174 extern crate num_traits;
175
176 #[macro_use]
177 mod macros;
178
179 pub fn trials() {
180 println!("are they approximately equal?: {:?}",
181 approx_eq!(f32, 1.0, 1.0000001));
182 }
183
184 mod ulps;
185 pub use self::ulps::Ulps;
186
187 mod ulps_eq;
188 pub use self::ulps_eq::ApproxEqUlps;
189
190 mod eq;
191 pub use self::eq::{ApproxEq, F32Margin, F64Margin};
192
193 #[cfg(feature="num-traits")]
194 mod ratio;
195 #[cfg(feature="num-traits")]
196 pub use self::ratio::ApproxEqRatio;