assert_nearly

Macro assert_nearly 

Source
assert_nearly!() { /* proc-macro */ }
Expand description

Asserts that the given comparison is nearly true using the provided tolerance.

On panic, this macro will print the values of the comparison with their debug representations as well as the values of the provided tolerance.

The comparison can be:

  • a == b for testing whether a is nearly equal to b
  • a != b for testing whether a is not nearly equal to b

The tolerance used can be:

  • eps for an absolute epsilon tolerance
  • ulps for an ulps based tolerance
  • tol for an absolute epsilon and ulps based tolerance
  • default for an absolute epsilon and ulps based tolerance using default values

ยงExamples

use nearly::{assert_nearly, Tolerance};

let a: f32 = 1.0;
let b: f32 = 1.0;

// use absolute epsilon tolerance
assert_nearly!(a == b, eps = 0.01);

// use ulps based tolerance
assert_nearly!(a == b, ulps = 5);

// use absolute epsilon and ulps based tolerance
assert_nearly!(a == b, eps = 0.01, ulps = 5);
assert_nearly!(a == b, tol = Tolerance::new(0.01, 5));

// use default absolute epsilon and default ulps based tolerance
assert_nearly!(a == b);