Floating-point epsilon is a small tolerance for deciding whether a computed value is close enough to its expected mathematical result. Exact equality can fail because floating-point values are represented with limited precision. section 2.3.3
Compare with a tolerance
Instead of asserting that 0.1 + 0.1 exactly equals 0.2, calculate the absolute difference and compare it with f32::EPSILON: let result: f32 = 0.1 + 0.1; let desired: f32 = 0.2; let absolute_difference = (desired - result).abs(); assert!(absolute_difference <= f32::EPSILON); Rust provides f32::EPSILON and f64::EPSILON for the two floating-point types. section 2.3.3
The suitable tolerance depends on the floating-point type and calculation. Inspecting bit patterns shows that equivalent-looking f32 and f64 operations can produce different results; epsilon comparisons address this precision problem rather than making values mathematically exact. section 2.3.3
