Debian Codegraph @

src/traits.rs

raw symbols SCIP
1 use core::fmt;
2
3 /// Per-field tolerances for [ULPs comparisons](https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#units-in-the-last-place-ulps-comparison).
4 ///
5 /// This trait establishes a one-to-one relation between an IEEE floating point
6 /// type and a type whose fields are expected to be structurally identical but
7 /// specified in [ULPs]. It is used by ULPs equality checks to specify per-field
8 /// tolerances.
9 ///
10 /// By convention, this type is named `FooUlps` for a given type `Foo`.
11 ///
12 /// The [`UlpsTol`] type alias exists to simplify usage, for example `UlpsTol<f32>`
13 /// is `u32`.
14 ///
15 /// To implement this trait over a new type, see [How to compare custom types].
16 ///
17 /// [How to compare custom types]: https://jtempest.github.io/float_eq-rs/book/how_to/compare_custom_types.html
18 /// [ULPs]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#units-in-the-last-place-ulps-comparison
19 pub trait FloatEqUlpsTol {
20 /// A structurally identical type to `Self`, with fields recursively wrapped
21 /// by `UlpsTol`.
22 type UlpsTol: ?Sized;
23 }
24
25 /// Per-field tolerances for [ULPs comparisons].
26 ///
27 /// [ULPs comparisons]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#units-in-the-last-place-ulps-comparison
28 pub type UlpsTol<T> = <T as FloatEqUlpsTol>::UlpsTol;
29
30 /// Per-field results of [ULPs](https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#units-in-the-last-place-ulps-comparison)
31 /// based diff calculations.
32 ///
33 /// This trait establishes a one-to-one relation between an IEEE floating point
34 /// type and a type whose fields are expected to be structurally identical but
35 /// specified as the result of calculating a diff in [ULPs]. This is usually
36 /// [`UlpsTol`] wrapped in an `Option`, since the ULPs diff between two floats is
37 /// a partial function that returns `None` when the signs differ. It is used by
38 /// testing and debugging tools to show the difference between two values on a
39 /// per-field basis and is built for clarity, not runtime efficiency.
40 ///
41 /// By convention, this type is named `FooDebugUlpsDiff` for a given type `Foo`.
42 ///
43 /// The [`DebugUlpsDiff`] type alias exists to simplify usage, for example
44 /// `DebugUlpsDiff<f32>` is `Option<u32>`.
45 ///
46 /// To implement this trait over a new type, see [How to compare custom types].
47 ///
48 /// [How to compare custom types]: https://jtempest.github.io/float_eq-rs/book/how_to/compare_custom_types.html
49 /// [ULPs]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#units-in-the-last-place-ulps-comparison
50 pub trait FloatEqDebugUlpsDiff {
51 /// A structurally identical type to `Self`, with fields recursively wrapped
52 /// by `DebugUlpsDiff`.
53 type DebugUlpsDiff;
54 }
55
56 /// Per-field results of [ULPs] based diff calculations.
57 ///
58 /// [ULPs]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#units-in-the-last-place-ulps-comparison
59 pub type DebugUlpsDiff<T> = <T as FloatEqDebugUlpsDiff>::DebugUlpsDiff;
60
61 /// Compare IEEE floating point values for equality using per-field tolerances.
62 ///
63 /// This trait is used in the implementation of the [`float_eq!`] and [`assert_float_eq!`]
64 /// families of macros.
65 ///
66 /// To implement this trait over a new type, see [How to compare custom types].
67 ///
68 /// ## Examples
69 ///
70 /// ```
71 /// # use float_eq::FloatEq;
72 /// assert!(4.0_f32.eq_abs(&4.000_001_5, &0.000_001_6));
73 /// assert!(4.0_f32.ne_abs(&4.000_001_5, &0.000_001_4));
74 ///
75 /// assert!(4.0_f32.eq_rel(&4.000_001_5, &0.000_000_4));
76 /// assert!(4.0_f32.ne_rel(&4.000_001_5, &0.000_000_3));
77 ///
78 /// assert!(4.0_f32.eq_ulps(&4.000_001_5, &3));
79 /// assert!(4.0_f32.ne_ulps(&4.000_001_5, &2));
80 /// ```
81 ///
82 /// [How to compare custom types]: https://jtempest.github.io/float_eq-rs/book/how_to/compare_custom_types.html
83 pub trait FloatEq<Rhs: ?Sized = Self> {
84 /// Type of the maximum allowed difference between two values for them to be
85 /// considered equal.
86 type Tol: ?Sized + FloatEqUlpsTol;
87
88 /// Check whether `self` is equal to `other`, using an [absolute tolerance
89 /// comparison].
90 ///
91 /// Implementations should be the equivalent of:
92 ///
93 /// ```
94 /// # trait TestFloatEq { fn eq_abs(&self, other: &Self, tol: &Self) -> bool; }
95 /// # impl TestFloatEq for f32 {
96 /// # fn eq_abs(&self, other: &Self, tol: &Self) -> bool {
97 /// // the PartialEq check covers equality of infinities
98 /// self == other || (self - other).abs().le(tol)
99 /// # }}
100 /// ```
101 ///
102 /// [absolute tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#absolute-tolerance-comparison
103 fn eq_abs(&self, other: &Rhs, tol: &Self::Tol) -> bool;
104
105 /// Check whether `self` is not equal to `other`, using an [absolute tolerance
106 /// comparison].
107 ///
108 /// Equal to `!self.eq_abs(other, tol)`, there is no need to reimplement
109 /// this for your own types.
110 ///
111 /// [absolute tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#absolute-tolerance-comparison
112 #[inline]
113 fn ne_abs(&self, other: &Rhs, tol: &Self::Tol) -> bool {
114 !self.eq_abs(other, tol)
115 }
116
117 /// Check whether `self` is equal to `other`, using a [relative tolerance
118 /// comparison].
119 ///
120 /// Equal to `self.eq_rmax(other, tol)`, there is no need to reimplement
121 /// this for your own types.
122 ///
123 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
124 #[inline]
125 fn eq_rel(&self, other: &Rhs, tol: &Self::Tol) -> bool {
126 self.eq_rmax(other, tol)
127 }
128
129 /// Check whether `self` is not equal to `other`, using a [relative tolerance
130 /// comparison].
131 ///
132 /// Equal to `!self.eq_rel(other, tol)`, there is no need to reimplement
133 /// this for your own types.
134 ///
135 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
136 #[inline]
137 fn ne_rel(&self, other: &Rhs, tol: &Self::Tol) -> bool {
138 !self.eq_rel(other, tol)
139 }
140
141 /// Check whether `self` is equal to `other`, using a [relative tolerance
142 /// comparison], scaled to the granularity of the input with the largest
143 /// magnitude.
144 ///
145 /// The implementation should be the equivalent of:
146 ///
147 /// ```
148 /// # trait TestFloatEq { fn eq_rel(&self, other: &Self, tol: &Self) -> bool; }
149 /// # impl TestFloatEq for f32 {
150 /// # fn eq_rel(&self, other: &Self, tol: &Self) -> bool {
151 /// // the PartialEq check covers equality of infinities
152 /// self == other || {
153 /// let largest = self.abs().max(other.abs());
154 /// let tolerance = largest * tol;
155 /// (self - other).abs() <= tolerance
156 /// }
157 /// # }}
158 /// ```
159 ///
160 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
161 fn eq_rmax(&self, other: &Rhs, tol: &Self::Tol) -> bool;
162
163 /// Check whether `self` is not equal to `other`, using a [relative tolerance
164 /// comparison].
165 ///
166 /// Equal to `!self.eq_rmax(other, tol)`, there is no need to reimplement
167 /// this for your own types.
168 ///
169 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
170 #[inline]
171 fn ne_rmax(&self, other: &Rhs, tol: &Self::Tol) -> bool {
172 !self.eq_rmax(other, tol)
173 }
174
175 /// Check whether `self` is equal to `other`, using a [relative tolerance
176 /// comparison], scaled to the granularity of the input with the smallest
177 /// magnitude.
178 ///
179 /// The implementation should be the equivalent of:
180 ///
181 /// ```
182 /// # trait TestFloatEq { fn eq_rel(&self, other: &Self, tol: &Self) -> bool; }
183 /// # impl TestFloatEq for f32 {
184 /// # fn eq_rel(&self, other: &Self, tol: &Self) -> bool {
185 /// // the PartialEq check covers equality of infinities
186 /// self == other || {
187 /// let smallest = self.abs().min(other.abs());
188 /// let tolerance = smallest * tol;
189 /// (self - other).abs() <= tolerance
190 /// }
191 /// # }}
192 /// ```
193 ///
194 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
195 fn eq_rmin(&self, other: &Rhs, tol: &Self::Tol) -> bool;
196
197 /// Check whether `self` is not equal to `other`, using a [relative tolerance
198 /// comparison].
199 ///
200 /// Equal to `!self.eq_rmin(other, tol)`, there is no need to reimplement
201 /// this for your own types.
202 ///
203 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
204 #[inline]
205 fn ne_rmin(&self, other: &Rhs, tol: &Self::Tol) -> bool {
206 !self.eq_rmin(other, tol)
207 }
208
209 /// Check whether `self` is equal to `other`, using a [relative tolerance
210 /// comparison], scaled to the granularity of the first input.
211 ///
212 /// The implementation should be the equivalent of:
213 ///
214 /// ```
215 /// # trait TestFloatEq { fn eq_rel(&self, other: &Self, tol: &Self) -> bool; }
216 /// # impl TestFloatEq for f32 {
217 /// # fn eq_rel(&self, other: &Self, tol: &Self) -> bool {
218 /// // the PartialEq check covers equality of infinities
219 /// self == other || {
220 /// let tolerance = self.abs() * tol;
221 /// (self - other).abs() <= tolerance
222 /// }
223 /// # }}
224 /// ```
225 ///
226 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
227 fn eq_r1st(&self, other: &Rhs, tol: &Self::Tol) -> bool;
228
229 /// Check whether `self` is not equal to `other`, using a [relative tolerance
230 /// comparison].
231 ///
232 /// Equal to `!self.eq_r1st(other, tol)`, there is no need to reimplement
233 /// this for your own types.
234 ///
235 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
236 #[inline]
237 fn ne_r1st(&self, other: &Rhs, tol: &Self::Tol) -> bool {
238 !self.eq_r1st(other, tol)
239 }
240
241 /// Check whether `self` is equal to `other`, using a [relative tolerance
242 /// comparison], scaled to the granularity of the second input.
243 ///
244 /// The implementation should be the equivalent of:
245 ///
246 /// ```
247 /// # trait TestFloatEq { fn eq_rel(&self, other: &Self, tol: &Self) -> bool; }
248 /// # impl TestFloatEq for f32 {
249 /// # fn eq_rel(&self, other: &Self, tol: &Self) -> bool {
250 /// // the PartialEq check covers equality of infinities
251 /// self == other || {
252 /// let tolerance = other.abs() * tol;
253 /// (self - other).abs() <= tolerance
254 /// }
255 /// # }}
256 /// ```
257 ///
258 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
259 fn eq_r2nd(&self, other: &Rhs, tol: &Self::Tol) -> bool;
260
261 /// Check whether `self` is not equal to `other`, using a [relative tolerance
262 /// comparison].
263 ///
264 /// Equal to `!self.eq_r2nd(other, tol)`, there is no need to reimplement
265 /// this for your own types.
266 ///
267 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
268 #[inline]
269 fn ne_r2nd(&self, other: &Rhs, tol: &Self::Tol) -> bool {
270 !self.eq_r2nd(other, tol)
271 }
272
273 /// Check whether `self` is equal to `other`, using an [ULPs comparison](https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#units-in-the-last-place-ulps-comparison).
274 ///
275 /// The implementation should be the equivalent of:
276 ///
277 /// ```
278 /// # trait TestFloatEq { fn eq_ulps(&self, other: &Self, tol: &u32) -> bool; }
279 /// # impl TestFloatEq for f32 {
280 /// # fn eq_ulps(&self, other: &Self, tol: &u32) -> bool {
281 /// if self.is_nan() || other.is_nan() {
282 /// false // NaNs are never equal
283 /// }
284 /// else if self.is_sign_positive() != other.is_sign_positive() {
285 /// self == other // account for zero == negative zero
286 /// } else {
287 /// let a = self.to_bits();
288 /// let b = other.to_bits();
289 /// let max = a.max(b);
290 /// let min = a.min(b);
291 /// (max - min).le(tol)
292 /// }
293 /// # }}
294 /// ```
295 ///
296 /// [ULPs comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#units-in-the-last-place-ulps-comparison
297 fn eq_ulps(&self, other: &Rhs, tol: &UlpsTol<Self::Tol>) -> bool;
298
299 /// Check whether `self` is not equal to `other`, using an [ULPs comparison].
300 ///
301 /// Equal to `!self.eq_ulps(other, tol)`, there is no need to reimplement
302 /// this for your own types.
303 ///
304 /// [ULPs comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#units-in-the-last-place-ulps-comparison
305 #[inline]
306 fn ne_ulps(&self, other: &Rhs, tol: &UlpsTol<Self::Tol>) -> bool {
307 !self.eq_ulps(other, tol)
308 }
309 }
310
311 /// Compare IEEE floating point values for equality using a uniform tolerance.
312 ///
313 /// This trait is used in the implementation of the [`float_eq!`] and [`assert_float_eq!`]
314 /// families of macros to provide the `*_all` variants of comparison algorithms
315 /// for homogeneous types.
316 ///
317 /// To implement this trait over a new type, see [How to compare custom types].
318 ///
319 /// ## Examples
320 ///
321 /// ```
322 /// # use float_eq::FloatEqAll;
323 /// let a = [1.000_000_2f32, -2.0];
324 /// let b = [1.0f32, -2.000_002];
325 ///
326 /// assert!(a.eq_abs_all(&b, &0.000_002));
327 /// assert!(a.ne_abs_all(&b, &0.000_001));
328 ///
329 /// assert!(a.eq_rmax_all(&b, &0.000_001));
330 /// assert!(a.ne_rmax_all(&b, &0.000_000_5));
331 ///
332 /// assert!(a.eq_ulps_all(&b, &8));
333 /// assert!(a.ne_ulps_all(&b, &7));
334 /// ```
335 ///
336 /// [How to compare custom types]: https://jtempest.github.io/float_eq-rs/book/how_to/compare_custom_types.html
337 /// [ULPs]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#units-in-the-last-place-ulps-comparison
338 /// [`AllTol`]: trait.FloatEqAll.html#associatedtype.AllTol
339 pub trait FloatEqAll<Rhs: ?Sized = Self> {
340 /// Type of the maximum allowed difference between each of two values' fields
341 /// for them to be considered equal.
342 type AllTol: ?Sized + FloatEqUlpsTol;
343
344 /// Check whether `self` is equal to `other`, using an [absolute tolerance
345 /// comparison].
346 ///
347 /// This must use the same algorithm as [`FloatEq::eq_abs`].
348 ///
349 /// [absolute tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#absolute-tolerance-comparison
350 fn eq_abs_all(&self, other: &Rhs, tol: &Self::AllTol) -> bool;
351
352 /// Check whether `self` is not equal to `other`, using an [absolute tolerance
353 /// comparison].
354 ///
355 /// Equal to `!self.eq_abs_all(other, tol)`, there is no need to reimplement
356 /// this for your own types.
357 ///
358 /// [absolute tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#absolute-tolerance-comparison
359 #[inline]
360 fn ne_abs_all(&self, other: &Rhs, tol: &Self::AllTol) -> bool {
361 !self.eq_abs_all(other, tol)
362 }
363
364 /// Check whether `self` is equal to `other`, using a [relative tolerance
365 /// comparison].
366 ///
367 /// Equal to `self.eq_rmax_all(other, tol)`, there is no need to reimplement
368 /// this for your own types.
369 ///
370 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
371 #[inline]
372 fn eq_rel_all(&self, other: &Rhs, tol: &Self::AllTol) -> bool {
373 self.eq_rmax_all(other, tol)
374 }
375
376 /// Check whether `self` is not equal to `other`, using a [relative tolerance
377 /// comparison].
378 ///
379 /// Equal to `!self.eq_rel_all(other, tol)`, there is no need to reimplement
380 /// this for your own types.
381 ///
382 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
383 #[inline]
384 fn ne_rel_all(&self, other: &Rhs, tol: &Self::AllTol) -> bool {
385 !self.eq_rel_all(other, tol)
386 }
387
388 /// Check whether `self` is equal to `other`, using a [relative tolerance
389 /// comparison].
390 ///
391 /// This must use the same algorithm as [`FloatEq::eq_rmax`].
392 ///
393 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
394 fn eq_rmax_all(&self, other: &Rhs, tol: &Self::AllTol) -> bool;
395
396 /// Check whether `self` is not equal to `other`, using a [relative tolerance
397 /// comparison].
398 ///
399 /// Equal to `!self.eq_rmax_all(other, tol)`, there is no need to reimplement
400 /// this for your own types.
401 ///
402 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
403 #[inline]
404 fn ne_rmax_all(&self, other: &Rhs, tol: &Self::AllTol) -> bool {
405 !self.eq_rmax_all(other, tol)
406 }
407
408 /// Check whether `self` is equal to `other`, using a [relative tolerance
409 /// comparison].
410 ///
411 /// This must use the same algorithm as [`FloatEq::eq_rmin`].
412 ///
413 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
414 fn eq_rmin_all(&self, other: &Rhs, tol: &Self::AllTol) -> bool;
415
416 /// Check whether `self` is not equal to `other`, using a [relative tolerance
417 /// comparison].
418 ///
419 /// Equal to `!self.eq_rmin_all(other, tol)`, there is no need to reimplement
420 /// this for your own types.
421 ///
422 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
423 #[inline]
424 fn ne_rmin_all(&self, other: &Rhs, tol: &Self::AllTol) -> bool {
425 !self.eq_rmin_all(other, tol)
426 }
427
428 /// Check whether `self` is equal to `other`, using a [relative tolerance
429 /// comparison].
430 ///
431 /// This must use the same algorithm as [`FloatEq::eq_r1st`].
432 ///
433 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
434 fn eq_r1st_all(&self, other: &Rhs, tol: &Self::AllTol) -> bool;
435
436 /// Check whether `self` is not equal to `other`, using a [relative tolerance
437 /// comparison].
438 ///
439 /// Equal to `!self.eq_r1st_all(other, tol)`, there is no need to reimplement
440 /// this for your own types.
441 ///
442 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
443 #[inline]
444 fn ne_r1st_all(&self, other: &Rhs, tol: &Self::AllTol) -> bool {
445 !self.eq_r1st_all(other, tol)
446 }
447
448 /// Check whether `self` is equal to `other`, using a [relative tolerance
449 /// comparison].
450 ///
451 /// This must use the same algorithm as [`FloatEq::eq_r2nd`].
452 ///
453 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
454 fn eq_r2nd_all(&self, other: &Rhs, tol: &Self::AllTol) -> bool;
455
456 /// Check whether `self` is not equal to `other`, using a [relative tolerance
457 /// comparison].
458 ///
459 /// Equal to `!self.eq_r2nd_all(other, tol)`, there is no need to reimplement
460 /// this for your own types.
461 ///
462 /// [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison
463 #[inline]
464 fn ne_r2nd_all(&self, other: &Rhs, tol: &Self::AllTol) -> bool {
465 !self.eq_r2nd_all(other, tol)
466 }
467
468 /// Check whether `self` is equal to `other`, using an [ULPs comparison].
469 ///
470 /// This must use the same algorithm as [`FloatEq::eq_ulps`].
471 ///
472 /// [ULPs comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#units-in-the-last-place-ulps-comparison
473 fn eq_ulps_all(&self, other: &Rhs, tol: &UlpsTol<Self::AllTol>) -> bool;
474
475 /// Check whether `self` is not equal to `other`, using an [ULPs comparison].
476 ///
477 /// Equal to `!self.eq_ulps_all(other, tol)`, there is no need to reimplement
478 /// this for your own types.
479 ///
480 /// [ULPs comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#units-in-the-last-place-ulps-comparison
481 #[inline]
482 fn ne_ulps_all(&self, other: &Rhs, tol: &UlpsTol<Self::AllTol>) -> bool {
483 !self.eq_ulps_all(other, tol)
484 }
485 }
486
487 /// Debug context for when an assert fails.
488 ///
489 /// This trait is used by [`assert_float_eq!`] and [`assert_float_ne!`].
490 ///
491 /// To implement this trait over a new type, see [How to compare custom types].
492 ///
493 /// [How to compare custom types]: https://jtempest.github.io/float_eq-rs/book/how_to/compare_custom_types.html
494 /// [ULPs]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#units-in-the-last-place-ulps-comparison
495 pub trait AssertFloatEq<Rhs: ?Sized = Self>: FloatEq<Rhs> {
496 /// The absolute difference between two values, displayed to the user via
497 /// `fmt::Debug` when an assert fails.
498 ///
499 /// This is usually the wider of `Self` and `Rhs`.
500 type DebugAbsDiff: fmt::Debug + Sized + FloatEqDebugUlpsDiff;
501
502 /// The per-field tolerance value used for comparison between two values,
503 /// displayed to the user via `fmt::Debug` when an assert fails.
504 ///
505 /// This should match [`Self::Tol`].
506 ///
507 /// [`Self::Tol`]: trait.FloatEq.html#associatedtype.Tol
508 type DebugTol: fmt::Debug + FloatEqUlpsTol;
509
510 /// Always positive absolute difference between two values.
511 ///
512 /// Implementations should be the equivalent of:
513 ///
514 /// ```
515 /// # trait TestFloatDiff { fn abs_diff(&self, other: &Self) -> Self; }
516 /// # impl TestFloatDiff for f32 {
517 /// # fn abs_diff(&self, other: &Self) -> Self {
518 /// (self - other).abs()
519 /// # }}
520 /// ```
521 fn debug_abs_diff(&self, other: &Rhs) -> Self::DebugAbsDiff;
522
523 /// Always positive absolute difference between two values in terms of [ULPs].
524 ///
525 /// For primitive values, this should be a partial function that returns:
526 /// - `Some(0)` if both arguments are either `0.0` or `-0.0`
527 /// - `None` if either argument is `NaN`
528 /// - `None` if the arguments have differing signs
529 /// - `Some(bitwise-difference)` otherwise
530 ///
531 /// For composite types, this should return per-field recursively calculated
532 /// results in order to present the most possible context to the user.
533 ///
534 /// Implementations over primitive types should be the equivalent of (using
535 /// `f32` as an example):
536 ///
537 /// ```
538 /// # trait TestFloatDiff { fn ulps_diff(&self, other: &Self) -> Option<u32>; }
539 /// # impl TestFloatDiff for f32 {
540 /// # fn ulps_diff(&self, other: &Self) -> Option<u32> {
541 /// if self == other {
542 /// Some(0)
543 /// } else if self.is_nan() || other.is_nan() {
544 /// None
545 /// } else if self.is_sign_positive() != other.is_sign_positive() {
546 /// None
547 /// } else {
548 /// let a = self.to_bits();
549 /// let b = other.to_bits();
550 /// let max = a.max(b);
551 /// let min = a.min(b);
552 /// Some(max - min)
553 /// }
554 /// # }}
555 /// ```
556 ///
557 /// [ULPs]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#units-in-the-last-place-ulps-comparison
558 fn debug_ulps_diff(&self, other: &Rhs) -> DebugUlpsDiff<Self::DebugAbsDiff>;
559
560 /// The tolerance used by an `abs` [comparison], displayed when an assert fails.
561 ///
562 /// [comparison]: index.html#comparison-algorithms
563 fn debug_abs_tol(&self, other: &Rhs, tol: &Self::Tol) -> Self::DebugTol;
564
565 /// The tolerance used by a `rel` [comparison], displayed when an assert fails.
566 ///
567 /// Equivalent to `self.debug_rmax_tol(self, other, tol)`, there is
568 /// no need to reimplement this for your own types.
569 ///
570 /// [comparison]: index.html#comparison-algorithms
571 #[inline]
572 fn debug_rel_tol(&self, other: &Rhs, tol: &Self::Tol) -> Self::DebugTol {
573 self.debug_rmax_tol(other, tol)
574 }
575
576 /// The tolerance used by an `rmax` [comparison], displayed when an assert fails.
577 ///
578 /// Returns `tol` scaled by the magnitude of the larger operand.
579 ///
580 /// [comparison]: index.html#comparison-algorithms
581 fn debug_rmax_tol(&self, other: &Rhs, tol: &Self::Tol) -> Self::DebugTol;
582
583 /// The tolerance used by an `rmin` [comparison], displayed when an assert fails.
584 ///
585 /// Returns `tol` scaled by the magnitude of the smaller operand.
586 ///
587 /// [comparison]: index.html#comparison-algorithms
588 fn debug_rmin_tol(&self, other: &Rhs, tol: &Self::Tol) -> Self::DebugTol;
589
590 /// The tolerance used by an `r1st` [comparison], displayed when an assert fails.
591 ///
592 /// Returns `tol` scaled by the magnitude of the first operand.
593 ///
594 /// [comparison]: index.html#comparison-algorithms
595 fn debug_r1st_tol(&self, other: &Rhs, tol: &Self::Tol) -> Self::DebugTol;
596
597 /// The tolerance used by an `r2nd` [comparison], displayed when an assert fails.
598 ///
599 /// Returns `tol` scaled by the magnitude of the second operand.
600 ///
601 /// [comparison]: index.html#comparison-algorithms
602 fn debug_r2nd_tol(&self, other: &Rhs, tol: &Self::Tol) -> Self::DebugTol;
603
604 /// The tolerance used by an `ulps` [comparison], displayed when an assert fails.
605 ///
606 /// [comparison]: index.html#comparison-algorithms
607 fn debug_ulps_tol(&self, other: &Rhs, tol: &UlpsTol<Self::Tol>) -> UlpsTol<Self::DebugTol>
608 where
609 UlpsTol<Self::DebugTol>: Sized;
610 }
611
612 /// Debug context for when an assert using an `all` check fails.
613 ///
614 /// This trait is used by [`assert_float_eq!`] and [`assert_float_ne!`].
615 ///
616 /// To implement this trait over a new type, see [How to compare custom types].
617 ///
618 /// [How to compare custom types]: https://jtempest.github.io/float_eq-rs/book/how_to/compare_custom_types.html
619 /// [ULPs]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#units-in-the-last-place-ulps-comparison
620 pub trait AssertFloatEqAll<Rhs: ?Sized = Self>: FloatEqAll<Rhs> {
621 /// Displayed to the user when an assert fails, using `fmt::Debug`.
622 ///
623 /// This should match the fields of the the most complex type in the comparison.
624 type AllDebugTol: fmt::Debug + FloatEqUlpsTol;
625
626 /// The tolerance used by an `abs_all` [comparison], displayed when an assert fails.
627 ///
628 /// [comparison]: index.html#comparison-algorithms
629 fn debug_abs_all_tol(&self, other: &Rhs, tol: &Self::AllTol) -> Self::AllDebugTol;
630
631 /// The tolerance used by a `rel_all` [comparison], displayed when an assert fails.
632 ///
633 /// Equivalent to `self.debug_rmax_all_tol(self, other, tol)`, there
634 /// is no need to reimplement this for your own types.
635 ///
636 /// [comparison]: index.html#comparison-algorithms
637 #[inline]
638 fn debug_rel_all_tol(&self, other: &Rhs, tol: &Self::AllTol) -> Self::AllDebugTol {
639 self.debug_rmax_all_tol(other, tol)
640 }
641
642 /// The tolerance used by an `rmax_all` [comparison], displayed when an assert fails.
643 ///
644 /// Returns `tol` scaled by the magnitude of the larger operand.
645 ///
646 /// [comparison]: index.html#comparison-algorithms
647 fn debug_rmax_all_tol(&self, other: &Rhs, tol: &Self::AllTol) -> Self::AllDebugTol;
648
649 /// The tolerance used by an `rmin_all` [comparison], displayed when an assert fails.
650 ///
651 /// Returns `tol` scaled by the magnitude of the smaller operand.
652 ///
653 /// [comparison]: index.html#comparison-algorithms
654 fn debug_rmin_all_tol(&self, other: &Rhs, tol: &Self::AllTol) -> Self::AllDebugTol;
655
656 /// The tolerance used by an `r1st_all` [comparison], displayed
657 /// when an assert fails.
658 ///
659 /// Returns `tol` scaled by the magnitude of the first operand.
660 ///
661 /// [comparison]: index.html#comparison-algorithms
662 fn debug_r1st_all_tol(&self, other: &Rhs, tol: &Self::AllTol) -> Self::AllDebugTol;
663
664 /// The tolerance used by an `r2nd_all` [comparison], displayed when an assert fails.
665 ///
666 /// Returns `tol` scaled by the magnitude of the second operand.
667 ///
668 /// [comparison]: index.html#comparison-algorithms
669 fn debug_r2nd_all_tol(&self, other: &Rhs, tol: &Self::AllTol) -> Self::AllDebugTol;
670
671 /// The tolerance used by an `ulps_all` [comparison], displayed when an assert fails.
672 ///
673 /// [comparison]: index.html#comparison-algorithms
674 fn debug_ulps_all_tol(
675 &self,
676 other: &Rhs,
677 tol: &UlpsTol<Self::AllTol>,
678 ) -> UlpsTol<Self::AllDebugTol>
679 where
680 UlpsTol<Self::AllDebugTol>: Sized;
681 }