Float compare broke!

Adam D. Ruppe destructionator at gmail.com
Mon Jun 11 05:54:35 PDT 2012


It probably has to do with different rounding
with the constant and the assignment.

http://dlang.org/float.html

Check out the section: Float­ing Point Con­stant Fold­ing

Dif­fer­ent com­piler set­tings, op­ti­miza­tion 
set­tings, and in­lin­ing set­tings can af­fect 
op­por­tu­ni­ties for con­stant fold­ing, there­fore the 
re­sults of float­ing point cal­cu­la­tions may dif­fer 
de­pend­ing on those set­tings.


a == b is probably done by the bits at runtime which match
because it is the same assignment.

But a == i_f might be propagated down there as 80 bit compared
to 32 bit and thus be just slightly different.



When comparing floating point you want to consider a little
wiggle room to be ok due to little rounding error. This function
should help:
http://dlang.org/phobos/std_math.html#approxEqual


More information about the Digitalmars-d-learn mailing list