In C++, std::islessgreater(x, y) asks "are x and y really different?" It is true when x is less than y or greater than y, and quiet with NaN.
It looks like x != y, but they are not the same. With a NaN, x != y is true (NaN is not equal to anything), while islessgreater() is false, because a NaN can be neither less nor greater.
std::islessgreater() is declared in <cmath> and is part of the group Classification and comparison. These tools ask what kind of number you have: normal, zero, infinite, or NaN. In C++ they are ordinary overloaded functions in std, not macros.
Syntax
Include <cmath> and call std::islessgreater. Start with these lines.
C++
bool a = std::islessgreater(1.0, 2.0); // true
bool b = std::islessgreater(2.0, 2.0); // false (equal)
bool c = std::islessgreater(std::nan(""), 2.0); // false (NaN)
Call
What it does
std::islessgreater(1.0, 2.0)
The numbers differ. Returns true.
std::islessgreater(2.0, 2.0)
The numbers are equal. Returns false.
std::islessgreater(std::nan(""), 2.0)
One value is NaN. Returns false, while != would give true.
The result is a bool. Add std::boolalpha before printing to see true or false instead of 1 or 0.
Library declarations
C++
bool islessgreater(float x, float y);
bool islessgreater(double x, double y);
bool islessgreater(long double x, long double y);
// plus overloads for mixed and integer arguments
Parameters
Parameter
Description
x
Required. The first number to compare. It can be float, double, long double, or an integer type.
y
Required. The second number to compare.
Return Value
Return Value
Description
true
x and y are both numbers and are not equal.
false
x equals y, or at least one of them is NaN.
C++ Examples
Example 1 — Compare Several Pairs
Test a smaller, bigger, equal, and a NaN pair.
C++
#include <cmath>
#include <iostream>
struct Pair {
const char* label;
double x;
double y;
};
int main() {
std::cout << std::boolalpha;
Pair pairs[] = {
{"1 vs 2", 1, 2},
{"2 vs 1", 2, 1},
{"2 vs 2", 2, 2},
{"NaN vs 2", std::nan(""), 2}
};
for (const Pair& p : pairs) {
std::cout << p.label << ": " << std::islessgreater(p.x, p.y) << '\n';
}
return 0;
}
Output
1 vs 2: true
2 vs 1: true
2 vs 2: false
NaN vs 2: false
How It Works
The function checks both directions at once, so the order of the two numbers does not matter.
1 vs 2 and 2 vs 1 are both true, as the numbers are different either way.
2 vs 2 is false because the numbers are equal. The NaN pair is false as well, since a NaN is not smaller or bigger.
Example 2 — islessgreater() Next to !=
Print both tests for a pair of equal numbers, a pair of different numbers, and a NaN pair.
3 vs 3 !=: false islessgreater: false
3 vs 4 !=: true islessgreater: true
NaN vs 4 !=: true islessgreater: false
How It Works
Each row prints the result of != and of islessgreater() for the same pair.
For the two real pairs the columns match: equal numbers give false, different numbers give true.
The NaN row is where they split. NaN is not equal to 4, so != says true, while islessgreater() says false.
Example 3 — Count Readings That Really Differ
A sensor should read 5.0. Count the readings that are really different, and ignore the missing one.
C++
#include <cmath>
#include <iostream>
int main() {
double expected = 5.0;
double readings[] = {5.0, 5.2, std::nan(""), 4.9};
int differ = 0;
for (double r : readings) {
if (std::islessgreater(r, expected)) {
++differ;
}
}
std::cout << "Readings that really differ: " << differ << '\n';
return 0;
}
Output
Readings that really differ: 2
How It Works
Every reading is compared with the expected 5.0 using islessgreater().
5.0 is equal, so it is not counted. 5.2 and 4.9 are different, so they are counted.
The missing reading is NaN, so islessgreater() is false and it is skipped. The result is 2. With != it would have been 3.
Frequently Asked Questions
std::islessgreater(x, y) returns true if x < y or x > y. It returns false when the numbers are equal, and false when either one is NaN.
true if x < y or x > y, otherwise false. If either argument is NaN, the result is false.
They match for normal numbers. For NaN, x != y is true, but islessgreater(x, y) is false because NaN is neither less nor greater.
Yes for the answer, but islessgreater() is quiet with NaN and does not raise a floating-point exception flag, and it evaluates both arguments only once.
Include <cmath> and write std::islessgreater(). That always works and picks the C++ overloads. The global name ::islessgreater often works too, but the standard does not promise it, and it may only take a double.