Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to...
by shultays on Wednesday May 10, 2023 @02:49AM
(#35884422)
well you shouldn't expect much if you are comparing floats to doubles
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to...
by spacechild1 on Wednesday May 10, 2023 @04:35AM
(#35885097)
True, but it's an easy mistake to make.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to...
by tialaramex on Wednesday May 10, 2023 @08:13AM
(#35886223)
It's an easy mistake to make in C (and C++) because of their lackadaisical approach to type safety.
In Rust if we insist we want to compare an f32 (what C would call float) to an f64 (double), the language says it doesn't have a way to do that, highlighting our mistake. If we just try to compare an f32 with value 0.7 against 0.7, the type inference concludes 0.7 is also an f32 so it's equal, and if we do the same with an f64, this time by inference 0.7 is an f64 and so again it's equal.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to...
by spacechild1 on Thursday May 11, 2023 @06:00AM
(#35900017)
Rust's behavior makes so much more sense! IMO, implicit conversions is one of C++ biggest weaknesses. Yes, it may save some keystrokes, but it can easily lead to subtle (or not so subtle) bugs.
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to...
by kragen on Thursday May 11, 2023 @11:03AM
(#35903205)
agreed
Re:In Erlang/OTP 27, +0.0 will no longer be exactly equal to...
by spacechild1 on Thursday May 11, 2023 @05:42AM
(#35899885)
> almost invariably floating-point calculations end up in some kind of comparison
I'm pretty sure OP meant comparison for equality, not comparison in general (which would be absurd, indeed).
> even equality comparison of floats is useful
That's exactly what I tried to point out. However, these are really special cases and you need to be careful and know what you are doing.
almost invariably floating-point calculations end up in some kind of comparison
even equality comparison of floats is useful; there are an abundance of seminumerical algorithms which use it correctly on the results of computations. a trivial example is computing an attractive fixpoint by iterating a function and comparing each hare result to the previous iteration and to a tortoise. you have to be especially careful about x87 extended precision here
ieee-784 requires bit-exact results for the five fundamental operations
here is an example where your 'safe' generalization is not safe:
this prints 'wtf'