| 1 | //! Compare IEEE floating point primitives, structs and collections for equality. |
| 2 | //! |
| 3 | //! This is API reference documentation. For introductory material, guides and |
| 4 | //! discussion see [the float_eq guide]. |
| 5 | //! |
| 6 | //! # Basic usage |
| 7 | //! |
| 8 | //! This crate provides boolean comparisons via [`float_eq!`] and [`float_ne!`]: |
| 9 | //! |
| 10 | //! ``` |
| 11 | //! use float_eq::float_eq; |
| 12 | //! |
| 13 | //! # let y_pos = 0.000_1; |
| 14 | //! if (float_eq!(y_pos, 0.0, abs <= 0.000_1)) { |
| 15 | //! //... |
| 16 | //! } |
| 17 | //! ``` |
| 18 | //! |
| 19 | //! And asserts via [`assert_float_eq!`] and [`assert_float_ne!`]: |
| 20 | //! |
| 21 | //! ``` |
| 22 | //! use float_eq::assert_float_eq; |
| 23 | //! |
| 24 | //! const TOL: f32 = 0.000_366_210_94; |
| 25 | //! assert_float_eq!(0.1f32.recip(), 10.0, r2nd <= TOL); |
| 26 | //! ``` |
| 27 | //! |
| 28 | //! Each of which invokes a specific comparison algorithm with an explictly |
| 29 | //! provided toelrance. In these examples: |
| 30 | //! |
| 31 | //! - `abs <= 0.000_1` is an absolute tolerance comparison with a tolerance of `0.000_1`. |
| 32 | //! - `r2nd <= TOL` is a relative tolerance comparison with a tolerance of `TOL`, |
| 33 | //! scaled to the precision of the second operand. |
| 34 | //! |
| 35 | //! # Comparison algorithms |
| 36 | //! |
| 37 | //! These are always of the form `CHECK <= tol`, where `CHECK` is one of: |
| 38 | //! |
| 39 | //! - `abs`: an [absolute tolerance comparison]. |
| 40 | //! - `rmax`: a [relative tolerance comparison], scaled to the precision of the larger operand/field. |
| 41 | //! - `rmin`: a [relative tolerance comparison], scaled to the precision of the smaller operand/field. |
| 42 | //! - `r1st`: a [relative tolerance comparison], scaled to the precision of the first operand/field. |
| 43 | //! - `r2nd`: a [relative tolerance comparison], scaled to the precision of the second operand/field. |
| 44 | //! - `ulps`: an [ULPs comparison]. |
| 45 | //! |
| 46 | //! When comparing homogeneous composite types that implement [`FloatEqAll`], |
| 47 | //! variants that use a uniform `tol` across all fields are also available: |
| 48 | //! |
| 49 | //! - `abs_all`: an [absolute tolerance comparison]. |
| 50 | //! - `rmax_all`: a [relative tolerance comparison], scaled to the precision of the larger field. |
| 51 | //! - `rmin_all`: a [relative tolerance comparison], scaled to the precision of the smaller field. |
| 52 | //! - `r1st_all`: a [relative tolerance comparison], scaled to the precision of the first field. |
| 53 | //! - `r2nd_all`: a [relative tolerance comparison], scaled to the precision of the second field. |
| 54 | //! - `ulps_all`: an [ULPs comparison]. |
| 55 | //! |
| 56 | //! *Note: `rel` and `rel_all` are legacy aliases for `rmax` and `rmax_all`, but |
| 57 | //! using the more precise algorithm names is recommended.* |
| 58 | //! |
| 59 | //! # Combining checks |
| 60 | //! |
| 61 | //! If multiple checks are specified in either a boolean comparison or an assert, |
| 62 | //! they are applied left to right and will shortcut on success. For example: |
| 63 | //! |
| 64 | //! ``` |
| 65 | //! # use float_eq::float_eq; |
| 66 | //! # let a = 0.1; let b = 0.1; let abs_tol = 0.0; let ulps_tol: u64 = 0; |
| 67 | //! float_eq!(a, b, abs <= abs_tol, ulps <= ulps_tol) |
| 68 | //! # ; |
| 69 | //! ``` |
| 70 | //! |
| 71 | //! Is equivalent to: |
| 72 | //! |
| 73 | //! ``` |
| 74 | //! # use float_eq::float_eq; |
| 75 | //! # let a = 0.1; let b = 0.1; let abs_tol = 0.0; let ulps_tol: u64 = 0; |
| 76 | //! float_eq!(a, b, abs <= abs_tol) || float_eq!(a, b, ulps <= ulps_tol) |
| 77 | //! # ; |
| 78 | //! ``` |
| 79 | //! |
| 80 | //! # Extending float_eq over custom types |
| 81 | //! |
| 82 | //! See [How to compare custom types]. |
| 83 | //! |
| 84 | //! [How to compare custom types]: https://jtempest.github.io/float_eq-rs/book/how_to/compare_custom_types.html |
| 85 | //! [the float_eq guide]: https://jtempest.github.io/float_eq-rs/book/index.html |
| 86 | //! [absolute tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#absolute-tolerance-comparison |
| 87 | //! [relative tolerance comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#relative-tolerance-comparison |
| 88 | //! [ULPs comparison]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html#units-in-the-last-place-ulps-comparison |
| 89 | |
| 90 | #![warn(missing_docs)] |
| 91 | #![cfg_attr(not(feature = "std"), no_std)] |
| 92 | |
| 93 | #[macro_use] |
| 94 | mod macrosrust-analyzer cargo float_eq 1.0.1 macros/; |
| 95 | pub use craterust-analyzer cargo float_eq 1.0.1 crate/::macrosrust-analyzer cargo float_eq 1.0.1 macros/::*; |
| 96 | |
| 97 | mod traitsrust-analyzer cargo float_eq 1.0.1 traits/; |
| 98 | pub use craterust-analyzer cargo float_eq 1.0.1 crate/::traitsrust-analyzer cargo float_eq 1.0.1 traits/::*; |
| 99 | |
| 100 | mod trait_implsrust-analyzer cargo float_eq 1.0.1 trait_impls/; |
| 101 | pub use craterust-analyzer cargo float_eq 1.0.1 crate/::trait_implsrust-analyzer cargo float_eq 1.0.1 trait_impls/::*; |
| 102 | |
| 103 | #[cfg(feature = "float_eq_derive")] |
| 104 | pub use float_eq_deriverust-analyzer cargo float_eq_derive 1.0.1 crate/::*; |