I wrote this code that allows me to check when two doubles are almost equal :
bool are_equal(double x, double y, double abs_tol=1e-15, double rel_tol=1e-15){
double diff(std::abs(x-y));
x = std::abs(x);
y = std::abs(y);
x = (x>y)?x:y;
return (diff<rel_tol*x) || (diff<abs_tol); /*has been updated*/
}
I'd like to know if it's a safe test and what are the "smart" values for abs_tol
and rel_tol
. I'd like to fix thoses values so that it works for very small and very big double.
edit Here is the link I took my inspiration from...