zng_layout::unit

Function about_eq

source
pub fn about_eq(a: f32, b: f32, epsilon: f32) -> bool
Expand description

f32 equality used in floating-point units.

  • NaN values are equal.
  • INFINITY values are equal.
  • NEG_INFINITY values are equal.
  • Finite values are equal if the difference is less than epsilon.

Note that this definition of equality is symmetric and reflexive, but it is not transitive, difference less then epsilon can accumulate over a chain of comparisons breaking the transitive property:

let e = 0.001;
let a = 0.0;
let b = a + e - 0.0001;
let c = b + e - 0.0001;

assert!(
    about_eq(a, b, e) &&
    about_eq(b, c, e) &&
    !about_eq(a, c, e)
)

See also about_eq_hash.