Very strange problem with comparing floating point numbers

monarch_dodra monarchdodra at gmail.com
Mon Oct 1 05:14:47 PDT 2012


On Monday, 1 October 2012 at 11:36:43 UTC, Maxim Fomin wrote:
> On Sunday, 30 September 2012 at 17:07:19 UTC, monarch_dodra 
> wrote:
>> As a rule of thumb, NEVER use opEqual with floating point 
>> types aniways. You need to use some sort of comparison with 
>> leway for error, such as std.math.approxEqual.
>
> It is possible to compare exactly floating point types by 
> binary comparison, if it provides some benefits.
>
> import std.stdio;
> import std.math;
>
> @property float getFloat()
> {
>    return sqrt(1.1);
> }
>
> void main()
> {
>    writeln(getFloat is getFloat);  // doesn't fail
> }

I think that what you are comparing here is the functions (the 
address), and not the results of the call. Try
writeln(getFloat() is getFloat()); //*May* fail

Also, "is" works like opEqual on built in types, AFAIK, it 
doesn't use any "binary" magic or anything like that.


More information about the Digitalmars-d-learn mailing list