Very strange problem with comparing floating point numbers

Maxim Fomin maxim at maxim-fomin.ru
Mon Oct 1 04:37:02 PDT 2012


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
}



More information about the Digitalmars-d-learn mailing list