expression
Compare finite values using absolute and relative tolerances
A fixed tolerance alone can be unsuitable across scales. This example restricts inputs to finite magnitudes at most 1e100 and fixed, positive tolerances so intermediate arithmetic cannot overflow. Absolute tolerance covers values near zero, relative tolerance scales with magnitude. Choose tolerances from the application requirements, not machine epsilon alone. Expected result (NOT RUN): near zero: true large: true NaN: false Common mistake: Treating f64::EPSILON as a universal absolute error tolerance. Static Rust example; not a verified ByteMind executable assessment.
Formal Syntax & Structure
rust syntaxrust
abs(a-b) <= max(abs_tol, rel_tol * max(abs(a),abs(b)))Working Example
Example Snippetrust
fn close(a: f64, b: f64) -> bool { if !a.is_finite() || !b.is_finite() || a.abs().max(b.abs()) > 1e100 { return false; } (a-b).abs() <= 1e-9_f64.max(1e-6 * a.abs().max(b.abs()))}fn main() { println!("near zero: {}", close(0.0, 1e-10)); println!("large: {}", close(1e6, 1e6+0.5)); println!("NaN: {}", close(f64::NAN, 1.0));}