Floating-point values compared for exact equality
ID |
rust.correctness.float_equality |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Numeric Operation |
Language |
Rust |
Description
Floating-point arithmetic is approximate, so two values that should be equal often differ in the last bits and this comparison silently yields the wrong answer. Compare the absolute difference against a tolerance, for example (a - b).abs() < f64::EPSILON.
Rationale
Floating-point arithmetic is approximate, so two values that should be equal often differ in the last bits and this comparison silently yields the wrong answer. Compare the absolute difference against a tolerance, for example (a - b).abs() < f64::EPSILON.
The following code illustrates the pattern detected by this rule:
fn balanced(left: f64, right: f64) -> bool {
// FLAGGED: Floating-point values compared for exact equality
left == right
}