0

This simple program compiles without any issue and equality assertion is evaluated as true.

fn main() {
    let celsius_temp: f64 = 23.0;
    let fahrenheit_temp: f64 = celsius_to_fahrenheit(celsius_temp);

    assert_eq!(fahrenheit_temp, 73.4);
    println!("Test passed!");
}

fn celsius_to_fahrenheit(celsius_temp: f64) -> f64 {
    (celsius_temp * 1.8) + 32.0
}

However, if I use instead of f64 a data type of f32, Rust compiler panics and assertion is false. It gives me a following message: thread 'main' panicked at 'assertion failed: (left == right) left: 73.399994, right: 73.4'

Why does it work with f64 and not with f32? I am not using here large numbers.

Lazar Nikolic
  • 4,261
  • 1
  • 22
  • 46

0 Answers0