Equality == comparisons with floating point numbers

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Sun Dec 8 01:55:41 PST 2013


On 08/12/13 09:44, Abdulhaq wrote:
> It's generally held that checking FP numbers for exact equality isn't practical
> and it's better to go for equality within a certain tolerance - any reason why
> you're not happy with that :-)?

Well, 2 reasons -- one was that in the pull request there was a reviewer request 
to check for exact equality.  The other is that quite obviously these quantities 
_are_ really equal and can be predictably and reliably checked as such if the 
quantities concerned are writefln'd to screen before the assert is called.

To give context, you're talking about a comparison of

      (a ^^ 2.0) * 1.0 + 0.0  == a ^^ 2.0

(or, alternatively, the same but using isIdentical).

I'm curious to confirm why placing writefln statements before the isIdentical 
check should change its behaviour (I assume flushing the FPU cache?) and whether 
this can be reliably done without writing output to screen.

But yes, you're right, if I get to the end of the day without finding a way to 
make exact equality comparison work, I will just give up and go back to my 
original solution of approxEqual.  I'm just hoping to come out of this having 
learned some new tricks, rather than falling back to the workarounds I already 
know :-)


More information about the Digitalmars-d-learn mailing list