Module assert_approx_eq

Source
Expand description

Assert a number is approximately equal to another.

Pseudocode:
| a - b | ≤ 1e-6

§Example

use assertables::*;

let a: f32 = 1.0000001;
let b: f32 = 1.0000011;
assert_approx_eq!(a, b);

§Comparisons

This crate provides macro groups that test approximations and nearness:

  • assert_approx_eq and assert_approx_ne test the approximate equality within 1e-6. The macro name and the approximate value are chosen to be similar to the longtime popular rust crate assert_approx_eq.

  • assert_in_delta tests the absolute error (i.e. delta). This is the magnitude of the difference between the exact value and the approximation.

  • assert_in_epsilon tests the relative error (i.e. epsilon). This is the absolute error divided by the magnitude of the exact value. This can be used to compare approximations of numbers of wildly differing size.

Examples:

  • Approximating the number 100 and 103 has an absolute error (approx) of 3 and a relative error (epsilon) of 0.03.

  • Approximating the number 1,000,000 and 1,000,003 has an absolute error (approx) of 3, and a relative error (epsilon) of 0.000003.

  • For many kinds of applications, the relative error is more important than the absolute error.

§Thanks

  • Thanks to Ashley Williams for creating and maintaining the assert_approx_eq crate.

  • Thanks to Ryan Davis and Ruby minitest for creating and maintaining assert_in_approx and assert_in_epsilon code.

§Module macros