Skip to main content

assert_close

Macro assert_close 

Source
macro_rules! assert_close {
    ($a:expr, $b:expr $(, $msg:literal $(, $($args:tt),*)?)?) => { ... };
}
Expand description

Asserts that two floating-point values are approximately equal within a small tolerance (epsilon).

Also works for complex numbers.

This is useful for comparing computed values where exact equality is not expected due to rounding errors.

  • Uses the machine epsilon for the floating-point type as the tolerance.
  • assert_eq! equivalent for floats.

§Parameters

  • $a: First value.
  • $b: Second value.
  • $msg: Custom failure message.

§Panics

Panics if the absolute difference |a - b| exceeds ::epsilon() for the type T.

§Examples

assert_close!(1.0 + 1e-16, 1.0, "Nearly equal");