-2

Because of the unstability of floating points getting compared, I want to have at least a warning when using the comparison operator.

Example:

float a,b;
a == b; // Here the warning shall be thrown

How can I realize that?

I thought of overloading like:

inline bool operator==(const float& a, const float& b)
{
    #warning "Usage of equal on floating point variables not allowed."
    return false;
}

I didn't found it clear stated, but it seems that overriding of this operator is forbidden as MSVC throws C2803 and gcc throws "must have an argument of class or enumerated type" (also described in Global overloading of == and != for floating-points). Even if it would be possible, the compiler would throw an error when he comes across the #error statement even if not used.

Community
  • 1
  • 1
Davidius
  • 166
  • 1
  • 12
  • Why are you trying to overload an operator for a built in type? – Ed Heal Nov 15 '16 at 14:34
  • 9
    What do you mean by *Because of the undefined behaviour of floating points getting compared*? There is no undefined behavior in float comparisons. – NathanOliver Nov 15 '16 at 14:35
  • 6
    Comparing floats is not UB. It's not even inherently wrong. It only has a high potential for not working out the way you wanted. – harold Nov 15 '16 at 14:35
  • 1
    Do you mean that since it's hard to compare two floating point values with each other because of rounding, it should be explicitly forbidden? Then *how* would you instead compare them? There are accepted and well-working ways to compare floating point values using an epsilon, do you want to forbid that too? – Some programmer dude Nov 15 '16 at 14:37
  • We have another comparison method does take eps into account. To force the usage of those who state clearly what is going to happen I want the compiler to throw an error. – Davidius Nov 15 '16 at 14:39
  • @harold yes true. HOwever, it is very unlikely that the same thing will happen across different enviroments. – Davidius Nov 15 '16 at 14:40
  • 1
    Possible duplicate of [Overloading comparison for double to allow for numerical error](http://stackoverflow.com/questions/17263482/overloading-comparison-for-double-to-allow-for-numerical-error) – Werner Henze Nov 15 '16 at 14:48
  • Careful of some of the [pitfalls of comparing floats using an epsilon value](http://stackoverflow.com/a/77735/1277769) – SirGuy Nov 15 '16 at 14:50

1 Answers1

1

Your approach with custom operator overload wont work because C++ standard forbids custom operator overloads if all their arguments have builtin types.

If you're using GCC (or clang/MinGW) you can use -Wfloat-equal to enable the warning about those comparsions. It's the closest thing you can get.

Even if it would be possible to completely disable them, it would be a bad idea. Sometimes they are useful and completely safe. Especially if there are no computations.

Consider following code:

float f = 0;

if (a >= -1 && a <= 1)
    f = std::asin(a);

// ...

if (f != 0)
    std::cout << "asin = " << f << '\n';
else
    std::cout << "Out of range.\n";

It's completely safe even though it uses == with float.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • Good example. I changed my question to reflect that. It whould still throw at least warning imo, to make the developer aware that he might do something unstable. – Davidius Nov 15 '16 at 14:56
  • @Davidius I found a flag for GCC to enable this warning. I guess something similar exists for MSVC too. – HolyBlackCat Nov 15 '16 at 15:06
  • oh nice, that's exactly what I was looking for. Hope I can find that in MSVC too. – Davidius Nov 15 '16 at 15:09