0

To check whether two floating-point variables are equal, we cannot use something like a==b. But how about using the islessgreater() function from header file?

From C++11, there are three overloads as below

bool islessgreater (float x      , float y);
bool islessgreater (double x     , double y);
bool islessgreater (long double x, long double y);

EDIT #1 I know there are some workarounds to check equality for two floating-point variables from lots of guys. For example, Floating-point Comparison From Boost How to correctly and standardly compare floats?

What I concern is that whether we could use the standard function islessgreater() in C++11 to check (float a == float b) or not? For example

int main() {

    float a = 1E-10;
    float b = 1.001E-10;

    bool res = !isnan(a) && !isnan(b) && !islessgreater(a, b);

    std::cout << std::boolalpha;

    if (res) std::cout << "a == b" << endl;
    else std::cout << "a != b" << endl;

    return 0;
}
James Liu
  • 11
  • 2
  • 1
    Usually you use an *epsilon* to compare floating point values. Commonly in the form of `abs(x - y) < SELECTED_EPSILON`. If that condition is true, then consider the values equal. – Some programmer dude Aug 30 '19 at 08:43
  • "To check whether two floating-point variables are equal, we cannot use something like a==b". Would you mind providing your reasoning for this? Is it due to the presence of NaN or some "precision-based" reason? – Bathsheba Aug 30 '19 at 08:46
  • `islessgreater(a, b)` is equivalent to `a < b || a > b`, without setting floating point exceptions due to nan, so it's not better than checking `a == b`. Furthermore, *"we cannot use something like a==b"* is a far too-broad statement, and in some contexts, `==` is clearly ok for comparing floating point numbers. – Holt Aug 30 '19 at 08:55
  • See also here: https://stackoverflow.com/questions/11826314/when-to-use-c-float-comparison-functions – Adrian Mole Aug 30 '19 at 08:55
  • The reason for guidelines about not comparing floating point values with `==` is that, due to loss of precision in calculations, values that might be expected to be equal will not compare equal. There is no workaround for that - only use of techniques that have some specified applicability, but are not universal (e.g. `abs(x-y) << std::numeric_limits::epsilon`. – Peter Aug 30 '19 at 08:57

1 Answers1

-1

I don't know why you have mentioned x == y does not work. The following code works perfectly in C++ to compare two floating-point variables:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main() {
    bool result;
    float x = 5.1235;
    float y = 5.1235;
    result = x == y;
    cout << result;
    return 0;
}
  • See https://stackoverflow.com/questions/1839422/strange-output-in-comparison-of-float-with-float-literal checking floats for equality is not always a good idea. – Colin Aug 30 '19 at 08:56
  • Well, please refer to the following links, though your example works. https://www.boost.org/doc/libs/1_71_0/libs/math/doc/html/math_toolkit/float_comparison.html and https://code.google.com/p/googletest/wiki/AdvancedGuide#Floating-Point_Comparison – James Liu Aug 30 '19 at 08:58
  • And [don't include ``](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – Some programmer dude Aug 30 '19 at 09:16
  • Barring the non-portable this answer is correct. Of course you can compare floating point. You just need to know what you're doing. In particular you need to know how numerical errors, if any, accumulate in your calculation. Only then do you know how you should emit some tolerance into any comparison. abs(x - y) < epsilon can be an extremely poor choice to make, sometimes it's abs((x - y) /x) < epsilon, or something else. And the calculation of epsilon is as important as the evaluation of x and y. Using `epsilon` from numeric_limits is a sign you don't know what your're doing. – Bathsheba Aug 30 '19 at 09:27