| 1 | # float_eq |
| 2 | |
| 3 | Compare IEEE floating point primitives, structs and collections for equality. |
| 4 | |
| 5 | This crate provides an API with a focus on making the choices of comparison |
| 6 | algorithm(s) and tolerances intuitive to implementers and maintainers, and of |
| 7 | providing clear output for debugging and development iteration. |
| 8 | |
| 9 | This readme is a quick tour of the crate. For introductory material, guides and |
| 10 | discussion see [the float_eq guide]. |
| 11 | |
| 12 | ## Usage |
| 13 | |
| 14 | Add this to your cargo.toml: |
| 15 | |
| 16 | ``` |
| 17 | [dependencies] |
| 18 | float_eq = "1" |
| 19 | ``` |
| 20 | |
| 21 | And, if you're using the 2015 edition, this to your crate root: |
| 22 | |
| 23 | ```rust |
| 24 | extern crate float_eq; |
| 25 | ``` |
| 26 | |
| 27 | Then, you can import items with `use`: |
| 28 | |
| 29 | ```rust |
| 30 | use float_eq::{assert_float_eq, float_eq}; |
| 31 | ``` |
| 32 | |
| 33 | ## Comparisons |
| 34 | |
| 35 | This crate provides boolean comparison operations: |
| 36 | |
| 37 | ```rust |
| 38 | if (float_eq!(y_pos, 0.0, abs <= 0.000_1)) { |
| 39 | //... |
| 40 | } |
| 41 | ``` |
| 42 | |
| 43 | And asserts: |
| 44 | |
| 45 | ```rust |
| 46 | const RECIP_REL_TOL: f32 = 0.000_366_210_94; |
| 47 | assert_float_eq!(x.recip(), 10.0, r2nd <= RECIP_REL_TOL); |
| 48 | ``` |
| 49 | |
| 50 | Using absolute tolerance, relative tolerance or ULPs based [comparison |
| 51 | algorithms]. |
| 52 | |
| 53 | ## Composite types |
| 54 | |
| 55 | Composite types may implement the provided extension traits to be compared on a |
| 56 | field-by-field basis: |
| 57 | |
| 58 | ```rust |
| 59 | let a = Complex32 { re: 2.0, im: 4.000_002 }; |
| 60 | let b = Complex32 { re: 2.000_000_5, im: 4.0 }; |
| 61 | |
| 62 | assert_float_eq!(a, b, ulps <= ComplexUlps32 { re: 2, im: 4 }); |
| 63 | ``` |
| 64 | |
| 65 | ...and if they are homogeneous, with a uniformly applied tolerance across all |
| 66 | fields: |
| 67 | |
| 68 | ```rust |
| 69 | assert_float_eq!(a, b, ulps_all <= 4); |
| 70 | ``` |
| 71 | |
| 72 | Arrays of any size are supported: |
| 73 | |
| 74 | ```rust |
| 75 | let a = [1.0, -2.0, 3.0]; |
| 76 | let b = [-1.0, 2.0, 3.5]; |
| 77 | assert_float_eq!(a, b, abs <= [2.0, 4.0, 0.5]); |
| 78 | assert_float_eq!(a, b, abs_all <= 4.0); |
| 79 | ``` |
| 80 | |
| 81 | As are tuples up to size 12 (inclusive): |
| 82 | |
| 83 | ```rust |
| 84 | let a = (1.0f32, 2.0f64); |
| 85 | let b = (1.5f32, -2.0f64); |
| 86 | assert_float_eq!(a, b, r2nd <= (0.5, 2.0)); |
| 87 | ``` |
| 88 | |
| 89 | Many standard and core types like `Vec` are supported: |
| 90 | |
| 91 | ```rust |
| 92 | let a = vec![1.0, -2.0, 3.0]; |
| 93 | let b = vec![-1.0, 2.0, 3.5]; |
| 94 | assert_float_eq!(a, b, rmax <= vec![2.0, 2.0, 0.25]); |
| 95 | assert_float_eq!(a, b, rmax_all <= 2.0); |
| 96 | ``` |
| 97 | |
| 98 | There are blanket trait impls for comparing mutable and immutable reference |
| 99 | types, the contents of `Cell`, `RefCell`, `Rc`, `Arc` and `Box` instances, as |
| 100 | well as for slices, `Option`, `Vec`, `VecDeque`, `LinkedList`, `BTreeMap` and |
| 101 | `HashMap`. |
| 102 | |
| 103 | ## Derivable |
| 104 | |
| 105 | The extension traits may be derived for non-generic structs and tuple structs: |
| 106 | |
| 107 | ```rust |
| 108 | #[derive_float_eq( |
| 109 | ulps_tol = "PointUlps", |
| 110 | ulps_tol_derive = "Clone, Copy, Debug, PartialEq", |
| 111 | debug_ulps_diff = "PointUlpsDebugUlpsDiff", |
| 112 | debug_ulps_diff_derive = "Clone, Copy, Debug, PartialEq", |
| 113 | all_tol = "f64" |
| 114 | )] |
| 115 | #[derive(Debug, PartialEq, Clone, Copy)] |
| 116 | pub struct Point { |
| 117 | pub x: f64, |
| 118 | pub y: f64, |
| 119 | } |
| 120 | |
| 121 | let a = Point { x: 1.0, y: -2.0 }; |
| 122 | let c = Point { |
| 123 | x: 1.000_000_000_000_000_9, |
| 124 | y: -2.000_000_000_000_001_3 |
| 125 | }; |
| 126 | assert_float_eq!(a, c, ulps <= PointUlps { x: 4, y: 3 }); |
| 127 | assert_float_eq!(a, c, ulps_all <= 4); |
| 128 | ``` |
| 129 | |
| 130 | ## Error messages |
| 131 | |
| 132 | Asserts provide additional useful context information. For example: |
| 133 | |
| 134 | ```rust |
| 135 | assert_float_eq!(4.0f32, 4.000_008, rmax <= 0.000_001); |
| 136 | ``` |
| 137 | |
| 138 | Panics with this error message: |
| 139 | |
| 140 | ``` |
| 141 | thread 'main' panicked at 'assertion failed: `float_eq!(left, right, rmax <= t)` |
| 142 | left: `4.0`, |
| 143 | right: `4.000008`, |
| 144 | abs_diff: `0.000008106232`, |
| 145 | ulps_diff: `Some(17)`, |
| 146 | [rmax] t: `0.000004000008`', assert_failure.rs:15:5 |
| 147 | ``` |
| 148 | |
| 149 | Where `[rmax] t` shows the tolerance value that the absolute difference was |
| 150 | compared against after being appropriately scaled. |
| 151 | |
| 152 | ## Optional features |
| 153 | |
| 154 | This crate can be used without the standard library (`#![no_std]`) by disabling |
| 155 | the default `std` feature. Use this in `Cargo.toml`: |
| 156 | |
| 157 | ``` |
| 158 | [dependencies.float_eq] |
| 159 | version = "1" |
| 160 | default-features = false |
| 161 | ``` |
| 162 | |
| 163 | Other optional features: |
| 164 | - **derive** — provides custom derive macros for all traits. |
| 165 | - **num** — blanket trait impls for `num::Complex` where it is instanced with a |
| 166 | compatible type. |
| 167 | |
| 168 | ## Related efforts |
| 169 | |
| 170 | The [`approx`], [`float-cmp`], [`assert_float_eq`] and [`is_close`] crates provide |
| 171 | similar floating point comparison capabilities to `float_eq`. The [`almost`] crate |
| 172 | divides its API into comparison of floats against zero and non-zero values. The |
| 173 | [`efloat`] crate provides an `f32` equivalent type that tracks the maximum |
| 174 | possible error bounds that may have occured due to rounding. |
| 175 | |
| 176 | The [`ieee754`] crate is not a comparison library, but provides useful |
| 177 | functionality for decomposing floats into their component parts, iterating over |
| 178 | representable values and working with ULPs directly, amoung other things. |
| 179 | |
| 180 | ## Contributing |
| 181 | |
| 182 | Constructive feedback, suggestions and contributions welcomed, please |
| 183 | [open an issue]. |
| 184 | |
| 185 | ## Changelog |
| 186 | |
| 187 | Release information is available in [CHANGELOG.md](CHANGELOG.md). |
| 188 | |
| 189 | [comparison algorithms]: https://jtempest.github.io/float_eq-rs/book/background/float_comparison_algorithms.html |
| 190 | [open an issue]: https://github.com/jtempest/float_eq-rs/issues/ |
| 191 | [the float_eq guide]: https://jtempest.github.io/float_eq-rs/book/introduction.html |
| 192 | [`almost`]: https://crates.io/crates/almost |
| 193 | [`approx`]: https://crates.io/crates/approx |
| 194 | [`assert_float_eq`]: https://crates.io/crates/assert_float_eq |
| 195 | [`efloat`]: https://crates.io/crates/efloat |
| 196 | [`float-cmp`]: https://crates.io/crates/float-cmp |
| 197 | [`ieee754`]: https://crates.io/crates/ieee754 |
| 198 | [`is_close`]: https://docs.rs/is_close/latest/is_close/ |