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

Compare floating point numbers by rounding

  • round
macro_rules! round {
    ($number: expr, $precision: expr) => {
        (10_u32.pow($precision) as f64 * $number).round() / 10_u32.pow($precision) as f64
    };
}

fn main() {
    let x: f64 = 0.1 + 0.2;
    let y = 0.3;
    println!("{x}");
    println!("{y}");
    println!("{}", x == y);
    println!();

    println!("{}", (100.0 * x).round() / 100.0);
    println!("{}", ((100.0 * x).round() / 100.0) == y);

    println!("{}", round64(x, 2));
    println!("{}", round64(x, 2) == y);

    println!("{}", round!(x, 2) == y);
}

fn round64(number: f64, precision: u32) -> f64 {
    (10_u32.pow(precision) as f64 * number).round() / 10_u32.pow(precision) as f64
}
0.30000000000000004
0.3
false

0.3
true
0.3
true