Why are float equality comparsions allowed?

That's why it has Ord, which floats are not.

But f32 is an IEEE 754 single-precision floating point. That defines equality, and thus Rust offers it.

There's really no good alternative. It's impossible to represent arbitrary real numbers in computers, and especially if you only have 32 bits.

Saying you can't use == doesn't really fix anything, because making people use x <= y && x >= y isn't better. Not to mention that the other comparisons are equally fallible -- one wouldn't expect 0.1 + 0.2 > 0.3 to be true, but it is -- so are you planning on removing all the comparisons? That doesn't seem right either.

There's no canned "just do this and you'll be fine" solution. IEEE 754 is about as good as it gets, in finite space, and has the huge advantage of lots of literature for people who do want to make sure they're doing it correctly.

(And, before someone inevitably brings them up, no, posits don't solve anything here.)

12 Likes