Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Approximately compare floating point numbers

  • TODO

  • approx_eq

  • float-cmp

  • Where ULP stands for "units of least precision", or "units in the last place".

use float_cmp::approx_eq;

fn main() {
    let x = 1.0;
    let y = 2.0;
    let z = 1.0;
    println!("{}", x < y);
    println!("{}", x == z);
    println!();

    let x = 0.1 + 0.2;
    let y = 0.3;
    println!("{}", x);
    println!("{}", y);
    println!("{}", x == y);
    println!("{}", approx_eq!(f32, x, y, ulps = 17));

    let x = 0.00001;
    let y = 0.000010000001;
    println!("{}", x == y);
    println!("{}", approx_eq!(f32, x, y, ulps = 20));
}
true
true

0.3
0.3
true
true
false
true