Trait AssertIsCloseToWithinMargin

Source
pub trait AssertIsCloseToWithinMargin<E, M> {
    // Required methods
    fn is_close_to_with_margin(self, expected: E, margin: impl Into<M>) -> Self;
    fn is_not_close_to_with_margin(
        self,
        expected: E,
        margin: impl Into<M>,
    ) -> Self;
}
Available on crate feature float-cmp only.
Expand description

Assert approximate equality for floating point numbers.

§Examples

use asserting::prelude::*;

assert_that!(10.0_f32 / 3.0).is_close_to_with_margin(3.333, (0.001, 5));
assert_that!(10.0_f64 / 3.0).is_close_to_with_margin(3.333, (0.001, 5));

assert_that!(10.0_f32 / 3.0).is_not_close_to_with_margin(3.333, (0.0001, 5));
assert_that!(10.0_f64 / 3.0).is_not_close_to_with_margin(3.333, (0.0001, 5));

Required Methods§

Source

fn is_close_to_with_margin(self, expected: E, margin: impl Into<M>) -> Self

Verifies that the actual value is approximately equal to the expected value.

For comparison, the epsilon and ULPS values of the given margin are used.

§Examples
use asserting::prelude::*;

assert_that!(10.0_f32 / 3.0).is_close_to_with_margin(3.333, (0.001, 5));
assert_that!(10.0_f64 / 3.0).is_close_to_with_margin(3.333, (0.001, 5));

The following articles describe the challenges with comparing floating point numbers and the meaning of the epsilon and ULPS values:

Source

fn is_not_close_to_with_margin(self, expected: E, margin: impl Into<M>) -> Self

Verifies that the actual value not approximately equals to the expected value.

For comparison, the epsilon and ULPS values of the given margin are used.

§Examples
use asserting::prelude::*;

assert_that!(10.0_f32 / 3.0).is_not_close_to_with_margin(3.333, (0.0001, 5));
assert_that!(10.0_f64 / 3.0).is_not_close_to_with_margin(3.333, (0.0001, 5));

The following articles describe the challenges with comparing floating point numbers and the meaning of the epsilon and ULPS values:

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§