float equality

bearophile bearophileHUGS at lycos.com
Mon Feb 21 11:33:05 PST 2011


so:

> I still think "==" should mean the exact equality test and must be  
> consistent in language.

Everyone in this thread has misunderstood what I have tried to say, so I will try to explain again, see the bottom of this post.
My idea was to turn the "==" operator among FP values into a syntax error (plus other added ideas).


> Making something like almostEqual default is far more catastrophic than  
> its current form.

This problem doesn't exists in my idea.


> It doesn't solve the existing problem and create a basis for new form of  
> problems.

The problems it creates are smaller.


> If one doesn't know what floating point is and insists on using it, it is  
> his own responsibility to face the consequences.

I don't buy this argument.


> If only interval arithmetic would solve all the problems, i wouldn't  
> hesitate dumping everything about FP.
> But no, it comes with its shortcomings.

I agree that interval arithmetic has its downsides. I have not proposed to replace normal floating point values with intervals. I have suggested to add a Phobos module for interval arithmetic because it's a useful thing to have.

---------------------

The little proposal I was thinking about is:

1) Turn the "==" operator into a compile-time syntax error if one or both operands are floating point values.
if (x == y) { ==> syntax error

2) The semantic of the "==" is now done by the "is" operator.
So if you want exactly this C code:
if (x == y) {
you use:
if (x is y) {

3) Then another built-in function or semantics is added, that is similar to:
some_syntax(x, y, an_approximation_level)
It takes tree arguments: x, y and a number of bits.

Turning the == into a syntax error is done to remember programmers that == among FP values is tricky, and the "is" operator is added because there are some situations where you want an exact comparison and you know what you are doing.

This was my idea. I know it doesn't improve the current situation a lot... so I don't expect this to be accepted. But I like to be understood :-)

Bye,
bearophile


More information about the Digitalmars-d mailing list