Overloading non-std operators

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.
bool operator ~= (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 bool operator== (double a, double b) operator?
Last edited on
It is not possible to overload non standard operators and it is not possible to overload an operator with primitive types on both sides.
Too bad. I'll stick with conventional functions.
Topic archived. No new replies allowed.