float equality

so so at so.so
Mon Feb 21 13:17:41 PST 2011


>> 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.

Why not? A logical flaw on my part or the statement being somewhat harsh?
Because i don't think it is the former, i will give an example for the  
latter.
I am a self-taught programmer and i too made big mistakes when using FP,  
probably i still do since it is a strange beast to deal with even if you  
know all about it.
For this reason it is somewhat understandable for people like me failing  
this kind of traps but can we say same thing for others?
Do they have this excuse? Not knowing the fundamental thing about FP and  
use it?

> 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

One thing i fail to understand here is, yes i agree "float == float"  
probably a "code smell" and we can find a workaround for this.
But i have to ask, where does it stop? I mean i can just here find you  
pages and pages of illogical results created with crafted examples using  
not even floating points but integrals.
If you want to make it easier like the case in hand, ignore a fundamental  
rule and you get tons of examples.

All i am trying to say is that if we are not in a position to solve this  
huge problem with a syntax change, lets just keep it the way it is instead  
of making yet another inconsistency.


More information about the Digitalmars-d mailing list