Module assertables::assert_in_epsilon

source ·
Expand description

Assert a number is within epsilon of another number.

Pseudocode:
| a - b | ≤ ε * min(a, b)

§Example

let a: i8 = 10;
let b: i8 = 20;
let epsilon: i8 = 1;
assert_in_epsilon!(a, b, epsilon);

The macros assert_in_delta and assert_in_epsilon can test approximations:

  • For an approximation, the absolute error (i.e. delta) is the magnitude of the difference between the exact value and the approximation.

  • For an approximation, the relative error (i.e. epsilon) 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.

  • For example, approximating the number 1,000 with an absolute error of 3 is, in most applications, much worse than approximating the number 1,000,000 with an absolute error of 3; in the first case the relative error is 0.003 and in the second it is only 0.000003.

  • Thanks to Ruby minitest for the example and documentation.

§Module macros