NaN means “Not a Number.” It is a special floating-point value for a mathematically undefined or otherwise invalid numeric result. For example, taking the square root of −42 or dividing 0 by 0 produces a NaN value. section 2.3.3 section 5.4.4
NaN can spread through a calculation: almost all operations involving it return NaN. It also defeats an ordinary equality check. A NaN value does not equal any value, including itself—even when two values have identical bit patterns. In Rust, an assertion that a NaN equals itself therefore fails. section 2.3.3 section 5.4.4
fn main() {
let x = (-42.0_f32).sqrt();
assert_eq!(x, x);
}
Run this short Rust example to see why checking a result against itself does not establish that it is a valid number. section 2.3.3
Check a suspect result with `is_nan()`. Use `is_finite()` when infinity should also be rejected. An assertion can then stop the program near the calculation that went wrong rather than letting an invalid result pass unnoticed. Infinity is a separate special value: dividing `1.0` by `0.0` demonstrates a failed `is_finite()` check, not a NaN. section 2.3.3
At the bit level, an `f32` whose exponent bits are all ones represents infinity, negative infinity, or NaN. Many distinct `f32` bit patterns represent NaN—approximately 4.2 million—so identical bits are not required for two values to share this behavior. section 5.4.4








