HiveBrain v1.2.0
Get Started
← Back to all entries
patternrustModerate

assert_eq! with floating point numbers and delta

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
numberswithassert_eqfloatingandpointdelta

Problem

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.

Context

Stack Overflow Q#30856285, score: 20

Revisions (0)

No revisions yet.