In floating-point numbers, the NaN
value is weirdly special. It's never equal to any NaN
, even itself. It's possible for x != x
to be true because of NaN
.
Because of such weird values, Rust has two levels of equality: PartialEq
and Eq
, and two levels of ordering PartialOrd
and Ord
. Floating point numbers (f32
, f64
) implement only PartialEq
and PartialOrd
.
Floats can't be sorted using the regular .sort()
method that requires Ord
. There is a total_cmp
helper method that does give ordering to NaN
values, which makes sorting work:
floats.sort_by;
Similarly, floats can't be keys in a HashMap
due to lack of Eq
. That's for the best, because exact comparison of floats is tricky anyway.