How to check if floating point is NaN in Rust

1 Answer

0 votes
fn main() {
    let num: f32 = 0.0 / 0.0; // This will produce NaN
    
    if num.is_nan() {
        println!("The number is NaN!");
    } else {
        println!("The number is not NaN.");
    }
}


    
/*
run:

The number is NaN!
   
*/

 



answered Aug 3, 2025 by avibootz
...