floating point comparison basics

ed sillymongrel at gmail.com
Tue Dec 3 15:16:22 PST 2013


OK, I've had a look into the code in std.math finally understand 
it, I think. I was confused about maxRelativeError and 
maxAbsoluteError then I found this also:

http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
(...If you want to count numbers near zero but of opposite sign 
as being equal then you need to add a maxAbsoluteError check 
also. ...)


So my understanding is this:

1. If comparing to 0.0 then "fabs(lhs) < maxAbsoluteError" is OK
2. If comparing two floating point values X, Y: 
"fabs((lhs-rhs)/rhs) < maxRelativeError" is OK.

Reading elsewhere on the web (bad idea I know) I get the 
impression the ordering of lhs and rhs matters. Some code, as in 
the link above, is written as:

if(fabs(rhs) > fabs(lhs)) {
     relErr = fabs((lhs-rhs)/rhs);
} else {
     fabs((lhs-rhs)/lhs);
}
if(relErr < maxRelErr) { return true;}
else {return false;}

Why does Phobos not check lhs < rhs? Does this imply that in some 
cases:

approxEqual(X, Y) != approxEqual(Y, X) ??

Thanks,
Ed



More information about the Digitalmars-d-learn mailing list