debug_assert_nearly

Macro debug_assert_nearly 

Source
debug_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.

Like debug_assert! this macro is only enabled in non optimized builds.

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::{debug_assert_nearly, Tolerance};

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

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

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

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

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