In rust, is there a version of f32
/ f64
that implements Eq
?
The only reason I can see for f32
/ f64
not implementing Eq
is that NaN != NaN
.
Potential ways such a type could behave:
- The type could just make
NaN == NaN
for that type which would be really useful because I often assume thata == a
is always true. - Another approach would be to disallow
NaN
entirely in that type so that there is noNaN
that could be unequal to itself.
Ideally there would be a way to use that type just by using a suffix (similar to 2.3_f32
) but I don't think that is possible.
Eq
. Comparing floating point numbers for equality is just not that useful in the first place:println!("{}", 0.1 + 0.2 == 0.3);
printsfalse
because of rounding errors and the decimal to binary conversions. Given that thePartialEq
is already not that useful, it's not surprising no one would have bothered making a floating point type work withEq
. – ParableEq
with the specific behavior you desire. – Amphisbaena