pub trait FloatEq<Rhs: ?Sized = Self> {
type Tol: ?Sized + FloatEqUlpsTol;
Show 14 methods
fn eq_abs(&self, other: &Rhs, tol: &Self::Tol) -> bool;
fn eq_rmax(&self, other: &Rhs, tol: &Self::Tol) -> bool;
fn eq_rmin(&self, other: &Rhs, tol: &Self::Tol) -> bool;
fn eq_r1st(&self, other: &Rhs, tol: &Self::Tol) -> bool;
fn eq_r2nd(&self, other: &Rhs, tol: &Self::Tol) -> bool;
fn eq_ulps(&self, other: &Rhs, tol: &UlpsTol<Self::Tol>) -> bool;
fn ne_abs(&self, other: &Rhs, tol: &Self::Tol) -> bool { ... }
fn eq_rel(&self, other: &Rhs, tol: &Self::Tol) -> bool { ... }
fn ne_rel(&self, other: &Rhs, tol: &Self::Tol) -> bool { ... }
fn ne_rmax(&self, other: &Rhs, tol: &Self::Tol) -> bool { ... }
fn ne_rmin(&self, other: &Rhs, tol: &Self::Tol) -> bool { ... }
fn ne_r1st(&self, other: &Rhs, tol: &Self::Tol) -> bool { ... }
fn ne_r2nd(&self, other: &Rhs, tol: &Self::Tol) -> bool { ... }
fn ne_ulps(&self, other: &Rhs, tol: &UlpsTol<Self::Tol>) -> bool { ... }
}
Expand description
Compare IEEE floating point values for equality using per-field tolerances.
This trait is used in the implementation of the float_eq!
and assert_float_eq!
families of macros.
To implement this trait over a new type, see How to compare custom types.
Examples
assert!(4.0_f32.eq_abs(&4.000_001_5, &0.000_001_6));
assert!(4.0_f32.ne_abs(&4.000_001_5, &0.000_001_4));
assert!(4.0_f32.eq_rel(&4.000_001_5, &0.000_000_4));
assert!(4.0_f32.ne_rel(&4.000_001_5, &0.000_000_3));
assert!(4.0_f32.eq_ulps(&4.000_001_5, &3));
assert!(4.0_f32.ne_ulps(&4.000_001_5, &2));
Required Associated Types
type Tol: ?Sized + FloatEqUlpsTol
type Tol: ?Sized + FloatEqUlpsTol
Type of the maximum allowed difference between two values for them to be considered equal.
Required Methods
Check whether self
is equal to other
, using an absolute tolerance
comparison.
Implementations should be the equivalent of:
// the PartialEq check covers equality of infinities
self == other || (self - other).abs().le(tol)
Check whether self
is equal to other
, using a relative tolerance
comparison, scaled to the granularity of the input with the largest
magnitude.
The implementation should be the equivalent of:
// the PartialEq check covers equality of infinities
self == other || {
let largest = self.abs().max(other.abs());
let tolerance = largest * tol;
(self - other).abs() <= tolerance
}
Check whether self
is equal to other
, using a relative tolerance
comparison, scaled to the granularity of the input with the smallest
magnitude.
The implementation should be the equivalent of:
// the PartialEq check covers equality of infinities
self == other || {
let smallest = self.abs().min(other.abs());
let tolerance = smallest * tol;
(self - other).abs() <= tolerance
}
Check whether self
is equal to other
, using a relative tolerance
comparison, scaled to the granularity of the first input.
The implementation should be the equivalent of:
// the PartialEq check covers equality of infinities
self == other || {
let tolerance = self.abs() * tol;
(self - other).abs() <= tolerance
}
Check whether self
is equal to other
, using a relative tolerance
comparison, scaled to the granularity of the second input.
The implementation should be the equivalent of:
// the PartialEq check covers equality of infinities
self == other || {
let tolerance = other.abs() * tol;
(self - other).abs() <= tolerance
}
Check whether self
is equal to other
, using an ULPs comparison.
The implementation should be the equivalent of:
if self.is_nan() || other.is_nan() {
false // NaNs are never equal
}
else if self.is_sign_positive() != other.is_sign_positive() {
self == other // account for zero == negative zero
} else {
let a = self.to_bits();
let b = other.to_bits();
let max = a.max(b);
let min = a.min(b);
(max - min).le(tol)
}
Provided Methods
Check whether self
is not equal to other
, using an absolute tolerance
comparison.
Equal to !self.eq_abs(other, tol)
, there is no need to reimplement
this for your own types.
Check whether self
is equal to other
, using a relative tolerance
comparison.
Equal to self.eq_rmax(other, tol)
, there is no need to reimplement
this for your own types.
Check whether self
is not equal to other
, using a relative tolerance
comparison.
Equal to !self.eq_rel(other, tol)
, there is no need to reimplement
this for your own types.
Check whether self
is not equal to other
, using a relative tolerance
comparison.
Equal to !self.eq_rmax(other, tol)
, there is no need to reimplement
this for your own types.
Check whether self
is not equal to other
, using a relative tolerance
comparison.
Equal to !self.eq_rmin(other, tol)
, there is no need to reimplement
this for your own types.
Check whether self
is not equal to other
, using a relative tolerance
comparison.
Equal to !self.eq_r1st(other, tol)
, there is no need to reimplement
this for your own types.
Check whether self
is not equal to other
, using a relative tolerance
comparison.
Equal to !self.eq_r2nd(other, tol)
, there is no need to reimplement
this for your own types.