Equality == comparisons with floating point numbers

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Fri Dec 6 05:47:02 PST 2013


A dangerous topic for everyone :-)

I've been working with some unittests involving comparing the output of 
different but theoretically equivalent versions of the same calculation.  To my 
surprise, calculations which I assumed would produce identical output, were 
failing equality tests.

It seemed unlikely this would be due to any kind of different rounding error, 
but I decided to check by writing out the whole floating-point numbers formatted 
with %.80f.  This confirmed my suspicion that the numbers were indeed identical. 
  You can read the detailed story here:
https://github.com/D-Programming-Language/phobos/pull/1740

It seems like I can probably use isIdentical for the unittests, but I am more 
concerned about the equality operator.  I completely understand that equality 
comparisons between FP are dangerous in general as tiny rounding errors may 
induce a difference, but == in D seems to see difference in circumstances where 
(so far as I can see) it really shouldn't happen.

Can anybody offer an explanation, a prognosis for improving things, and possible 
coping strategies in the meantime (other than the ones I already know, 
isIdentical and approxEqual)?


More information about the Digitalmars-d-learn mailing list