The comparison operator (==) is pretty useless when comparing doubles due to floating point errors.
I can use a function:
1 2 3 4 5 6
bool almost_equal(double a, double b)
{
return std::abs(a-b) <= std::numeric_limits<double>::epsilon()
* std::max(std::abs(a), std::abs(b))
* 3.; // 3 ULPs gives us a good "almost equal"
}
but operator ~= seems like it would be intuitive too for an "Almost equal" comparator. booloperator ~= (double a, double b) { /* ... */ }
Of course this doesn't compile (at least in VS2010). Is it possible to overload non-standard operators like this or is this a limitation of the C++ language?
If not, is it possible to re-define the booloperator== (double a, double b) operator?