F64 primitive is_nan() implementation question

Can somebody explain the implementation for is_nan() for the f64 primitive type?
It looks like this:

pub fn is_nan(self) -> bool {
self != self
}

More precisely I'm interested in what does self != self mean...

You cand find the function described here:
https://doc.rust-lang.org/std/primitive.f64.html
And the implementation here:
https://doc.rust-lang.org/src/core/num/f64.rs.html#160-162

I was working through an example in chapter 17 of the Rust book, dealing with OOP Rust and I couldn't figure out why

assert_eq!(f64_value, f64::NAN);

would not work and I had to use

assert_eq!(f64_value.is_nan(), true);

Thanks,
Alex

NaN is the only value of an f64 that doesn't equal itself.

1 Like

Thanks @sfackler! You pointed me in the right direction: IEEE754 - floating point representation.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.