float_eq
Compare IEEE floating point values for equality.
Comparing floating point values for equality is notoriously difficult, getting it right requires careful reasoning and iteration. This API provides a variety of comparison algorithms and debugging tools to help make the process more intuitive and your choices explicit and clear to future maintainers.
Background
Given how widely algorithmic requirements can vary, float_eq
explores the
idea that there are no generally sensible default margins for comparisons.
This is in contrast to the approach taken by many other crates, which often
provide default epsilon values in checks or implicitly favour particular
algorithms. The author's hope is that by exposing the inherent complexity
in a uniform way, programmers will find it easier to develop an intuition
for effective use of floats.
This work builds on the definitions in Knuth's The Art Of Computer Programming, (Vol. 2, Seminumerical Algorithms, Third Edition, section 4.2.2), and that Random ASCII article on floating point comparison.
Usage
Add this to your cargo.toml:
[dependencies]
float_eq = "0.5"
and, if you're using the 2015 edition, this to your crate root:
extern crate float_eq;
then, you can import items with use
:
use ;
Comparisons
This crate provides boolean comparison operations:
assert!;
const ROUNDING_ERROR: f32 = 0.000_345_266_98;
assert!;
And asserts:
const RECIP_REL_EPSILON: f32 = 0.000_366_210_94;
assert_float_eq!;
assert_float_ne!;
Checks are invoked by name and with a threshold, so for example abs <= 0.000_05
should be read as "an absolute epsilon comparison with a maximum difference of
less than or equal to 0.000_05
". Similarly, rmax
, rmin
, r1st
and r2nd
provide a variety of kinds of relative epsilon comparison with thresholds that
scale to the granularity of one or input value or the other and ulps
is an
ULPs based comparison that takes advantage of the underlying bitwise
representation. See the API documentation for a long form introduction
to the different kinds of checks, their uses and limitations.
Combining checks
If more than one check is specified by a comparison then they are performed in order from left to right. If any check is true, then the two values are considered equal. For example, this expression:
float_eq!
Is equivalent to:
float_eq! || float_eq!
This allows you to build comparison expressions as needed, only paying for what you use.
Composite types
Composite types that implement FloatEq
may be compared on a field-by-field
basis, and types that implement FloatEqAll
may be compared with a uniformly
applied epsilon value across all fields:
let a = Complex32 ;
let b = Complex32 ;
assert_float_eq!;
assert_float_eq!;
Arrays of size 0 to 32 (inclusive) are supported:
let a = ;
let b = ;
assert_float_eq!;
assert_float_eq!;
As are tuples up to size 12 (inclusive):
let a = ;
let b = ;
assert_float_eq!;
There are also blanket trait impls for comparing mutable and immutable reference
types, the contents of Cell
, RefCell
, Rc
, Arc
and Box
instances, as
well as for slices, Option
, Vec
, VecDeque
, LinkedList
, BTreeMap
and
HashMap
.
Types that also implement AssertFloatEq
/AssertFloatEqAll
may be used in the
assert forms.
Derivable
If the optional "derive"
feature is enabled, all of the traits may be
implemented using #[derive]
. The easiest way to do so is to make use of the
#[derive_float_eq]
helper macro:
let a = Point ;
let c = Point ;
assert_float_eq!;
assert_float_eq!;
Error messages
Assertion failure output tries to provide useful context information without going overboard. For example, this call:
assert_float_eq!;
Panics with this error message:
thread 'main' panicked at 'assertion failed: `float_eq!(left, right, rmax <= ε)`
left: `4.0`,
right: `4.000008`,
abs_diff: `0.000008106232`,
ulps_diff: `Some(17)`,
[rmax] ε: `0.000004000008`', assert_failure.rs:15:5
The message shows the values of the expressions being compared and the
difference between them both in absolute terms and in terms of ULPs. The
[rmax] ε
line shows the epsilon value that the absolute difference was
compared against after being appropriately scaled.
Optional features
This crate can be used without the standard library (#![no_std]
) by disabling
the default std
feature. Use this in Cargo.toml
:
[dependencies.float_eq]
version = "0.5"
default-features = false
Other optional features:
- derive — provides custom derive macros for all traits.
- num — blanket trait impls for
num::Complex
where it is instanced with a compatible type.
Related efforts
The approx
and float-cmp
crates provide a similar style of general
comparison operations, whereas assert_float_eq
focuses specifically on
assertions. The almost
crate instead divides its API into algorithms
comparing against zero and non-zero values. In contrast, efloat
takes the
approach of tracking the error bounds of values as operations are applied.
Contributing
Constructive feedback, suggestions and contributions welcomed, please open an issue.
Changelog
Release information is available in CHANGELOG.md.