assert_approx_eq

Macro assert_approx_eq 

Source
macro_rules! assert_approx_eq {
    ($a:expr, $b:expr) => { ... };
    ($a:expr, $b:expr, eps = $eps:literal) => { ... };
    ($a:expr, $b:expr, $fmt:literal $(, $args:expr)*) => { ... };
    ($a:expr, $b:expr, eps = $eps:literal, $fmt:literal $(, $args:expr)*) => { ... };
}
Expand description

Asserts that two values are approximately equal. Requires that the left operand has an applicable ApproxEq impl and that both operands impl Debug unless a custom message is given.

§Panics

If the given values are not approximately equal.

§Examples

assert_eq would fail, but assert_approx_eq passes:

assert_ne!(0.1 + 0.2, 0.3);
assert_approx_eq!(0.1 + 0.2, 0.3);

A relative epsilon is used:

assert_ne!(1e7, 1e7 + 1.0);
assert_approx_eq!(1e7, 1e7 + 1.0);

A custom epsilon can be given:

assert_approx_eq!(100.0, 101.0, eps = 0.01);

Like assert_eq, this macro supports custom panic messages. The epsilon, if present, must come before the format string.

assert_approx_eq!(f32::sin(3.14), 0.0, eps = 0.001,
    "3.14 is not a good approximation of {}!", f32::consts::PI);