In C++, std::isgreater(x, y) asks "is x greater than y?" For normal numbers it gives the same answer as x > y. isgreater(3, 2) is true and isgreater(2, 3) is false.
The difference shows up with NaN. The plain > operator may raise a floating-point "invalid" flag when a NaN is involved. The six comparison functions, isgreater(), isgreaterequal(), isless(), islessequal(), islessgreater(), and isunordered(), are "quiet": they just return false (or the right answer) with no side effects.
std::isgreater() 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::isgreater. Start with these lines.
C++
bool a = std::isgreater(3.0, 2.0); // true
bool b = std::isgreater(2.0, 3.0); // false
bool c = std::isgreater(std::nan(""), 1.0); // false (NaN)
Call
What it does
std::isgreater(3.0, 2.0)
3 is bigger than 2. Returns true.
std::isgreater(2.0, 2.0)
Equal numbers are not greater. Returns false.
std::isgreater(std::nan(""), 1.0)
One value is NaN, so there is no order. Returns false.
The result is a bool. Add std::boolalpha before printing to see true or false instead of 1 or 0.
Library declarations
C++
bool isgreater(float x, float y);
bool isgreater(double x, double y);
bool isgreater(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 is greater than y.
false
x is less than, or equal to y, or one of them is NaN.
C++ Examples
Example 1 — Compare Several Pairs
Test pairs where the first value is bigger, smaller, equal, negative, and NaN. Mixed int and double values are also allowed.
C++
#include <cmath>
#include <iostream>
struct Pair {
const char* label;
double x;
double y;
};
int main() {
std::cout << std::boolalpha;
Pair pairs[] = {
{"3 vs 2", 3, 2},
{"2 vs 3", 2, 3},
{"2 vs 2", 2, 2},
{"-1 vs -2", -1, -2},
{"NaN vs 1", std::nan(""), 1}
};
for (const Pair& p : pairs) {
std::cout << p.label << ": " << std::isgreater(p.x, p.y) << '\n';
}
return 0;
}
Output
3 vs 2: true
2 vs 3: false
2 vs 2: false
-1 vs -2: true
NaN vs 1: false
How It Works
Each Pair holds a label and two numbers. The loop passes the two numbers to isgreater().
3 is greater than 2, and -1 is greater than -2, so both are true. The reverse and the equal pair are false.
The last pair has a NaN, which has no order, so the answer is false and no error is raised.
Example 2 — Count Readings Above a Limit
Four sensor readings are checked against a limit of 15. One reading is missing (NaN), and it must not count.
5 vs 3 isgreater: true operator>: true
3 vs 5 isgreater: false operator>: false
4 vs 4 isgreater: false operator>: false
How It Works
Each row prints the answer from the function and from the operator for the same two numbers.
The two columns always match for real numbers: 5 > 3 is true, and 3 > 5 and 4 > 4 are false.
So you can use either one. Pick isgreater() when NaN might be in your data and you want a quiet, predictable test.
Frequently Asked Questions
std::isgreater(x, y) returns true if x is greater than y. It works like x > y, but never raises an invalid-operation exception, and it returns false if either value is NaN.
true if x > y, otherwise false. If either argument is NaN, the result is false.
For normal numbers the answer is the same. With NaN, the > operator may raise a floating-point exception flag, while isgreater() stays quiet and returns false.
It returns false. A NaN has no order compared to any number, so it is not greater than anything.
Include <cmath> and write std::isgreater(). That always works and picks the C++ overloads. The global name ::isgreater often works too, but the standard does not promise it, and it may only take a double.