unit-testingrust

assert_eq! with floating point numbers and delta


Is there a preferred way to do an assert with two floating point numbers and a delta in Rust?

For example...

let a = 3.0;
let b = 2.9999999999;
assert_eq!(a, b, 0.0001); // Imaginary syntax where a ~= b, within 0.0001

Solution

  • No. At the moment, you have to check the difference by yourself or use the float-cmp crate.

    Also check out the f32 constants.