Skip to main content

approx_eq

Macro approx_eq 

Source
macro_rules! approx_eq {
    ($lhs:expr, $rhs:expr) => { ... };
    ($lhs:expr, $rhs:expr, rtol <= $rtol:expr) => { ... };
    ($lhs:expr, $rhs:expr, atol <= $atol:expr) => { ... };
    ($lhs:expr, $rhs:expr, rtol <= $rtol:expr, atol <= $atol:expr) => { ... };
    ($lhs:expr, $rhs:expr, atol <= $atol:expr, rtol <= $rtol:expr) => { ... };
}
Expand description

Checks if two values are approximately equal.

Returns true if the values are approximately equal within the specified tolerances, false otherwise. This macro is useful for conditional logic, while assert_approx_eq! should be used in tests.

§Syntax

approx_eq!(left, right)                          // Default tolerances
approx_eq!(left, right, atol <= tolerance)       // Custom absolute tolerance
approx_eq!(left, right, rtol <= tolerance)       // Custom relative tolerance
approx_eq!(left, right, atol <= a, rtol <= r)    // Both tolerances
approx_eq!(left, right, rtol <= r, atol <= a)    // Order doesn't matter

§Default Tolerances

When no tolerances are specified:

  • atol = 0.0
  • rtol = sqrt(f64::EPSILON) ≈ 1.49e-8

§Examples

use lox_test_utils::approx_eq;

// Default tolerances
assert!(approx_eq!(1.0, 1.0 + f64::EPSILON));
assert!(!approx_eq!(1.0, 1.1));

// Custom absolute tolerance
assert!(approx_eq!(1.0, 1.001, atol <= 0.01));

// Custom relative tolerance (1% difference allowed)
assert!(approx_eq!(100.0, 100.5, rtol <= 0.01));

// Both tolerances
assert!(approx_eq!(1.0, 1.001, atol <= 0.01, rtol <= 0.01));

// Works with vectors
use glam::DVec3;
let v1 = DVec3::new(1.0, 2.0, 3.0);
let v2 = DVec3::new(1.0 + f64::EPSILON, 2.0, 3.0);
assert!(approx_eq!(v1, v2));

§See Also