Skip to main content

margin

Function margin 

Source
pub fn margin(factor: f64) -> impl ApproximateEqualityEvaluator
Expand description

Creates an ApproximateEqualityEvaluator that operates by applying the given factor as a margin to determine approximate equality.

Examples found in repository?
examples/scalars.rs (line 22)
13fn main() {
14    {
15        println!();
16        println!("compare two f64 instances (with `margin()`):");
17
18        let expected = 123456.0;
19        let actual = 123456.01;
20
21        // this one passes
22        assert_scalar_ne_approx!(expected, actual, margin(0.0));
23
24        // this one does not
25        let failed = std_panic::catch_unwind(|| {
26            assert_scalar_eq_approx!(expected, actual, margin(0.0));
27        })
28        .is_err();
29        assert!(failed, "expected assert_scalar_eq_approx! to fail");
30    }
31
32    {
33        println!();
34        println!("compare two f64 instances (with `multiplier()`):");
35
36        let expected = 123456.0;
37        let actual = 123456.01;
38
39        // this one passes
40        assert_scalar_ne_approx!(expected, actual, multiplier(0.0));
41
42        // this one does not
43        let failed = std_panic::catch_unwind(|| {
44            assert_scalar_eq_approx!(expected, actual, multiplier(0.0));
45        })
46        .is_err();
47        assert!(failed, "expected assert_scalar_eq_approx! to fail");
48    }
49}
More examples
Hide additional examples
examples/vectors.rs (line 32)
13fn main() {
14    {
15        println!();
16        println!("compare two vectors (README-style, with `multiplier()`):");
17
18        let expected = &[3.0, -40404.0, 1.23456];
19        let actual = Vec::from([3.0, -40410.0, 1.234567]);
20
21        assert_vector_eq_approx!(expected, actual, multiplier(0.00015));
22    }
23
24    {
25        println!();
26        println!("compare two vectors (with `margin()`):");
27
28        let expected = &[1.0, 2.0, 3.0];
29        let actual = Vec::from([1.0, 2.001, 3.0]);
30
31        // this one passes
32        assert_vector_ne_approx!(expected, actual, margin(0.0));
33
34        // this one does not
35        let failed = std_panic::catch_unwind(|| {
36            assert_vector_eq_approx!(expected, actual, margin(0.0));
37        })
38        .is_err();
39        assert!(failed, "expected assert_vector_eq_approx! to fail");
40    }
41
42    {
43        println!();
44        println!("compare two vectors (with `multiplier()`):");
45
46        let expected = &[1.0, 2.0, 3.0];
47        let actual = Vec::from([1.0, 2.001, 3.0]);
48
49        // this one passes
50        assert_vector_ne_approx!(expected, actual, multiplier(0.0));
51
52        // this one does not
53        let failed = std_panic::catch_unwind(|| {
54            assert_vector_eq_approx!(expected, actual, multiplier(0.0));
55        })
56        .is_err();
57        assert!(failed, "expected assert_vector_eq_approx! to fail");
58    }
59}