C++ std::isgreater() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

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)
CallWhat 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

ParameterDescription
xRequired. The first number to compare. It can be float, double, long double, or an integer type.
yRequired. The second number to compare.

Return Value

Return ValueDescription
truex is greater than y.
falsex 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;
}

How It Works

  1. Each Pair holds a label and two numbers. The loop passes the two numbers to isgreater().
  2. 3 is greater than 2, and -1 is greater than -2, so both are true. The reverse and the equal pair are false.
  3. 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.

C++
#include <cmath>
#include <iostream>

int main() {
    double readings[] = {12.5, std::nan(""), 20.0, 18.0};
    double limit = 15.0;
    int above = 0;

    for (double r : readings) {
        if (std::isgreater(r, limit)) {
            ++above;
        }
    }
    std::cout << "Readings above " << limit << ": " << above << '\n';
    return 0;
}

How It Works

  1. The loop tests each reading against limit with isgreater(r, limit) and counts the ones that pass.
  2. 12.5 is below 15, so it is not counted. 20 and 18 are above it, so the count becomes 2.
  3. The NaN reading gives false quietly, so the missing value is skipped and the answer is 2.

Example 3 — isgreater() Next to the > Operator

For real numbers, both ways give the same result. This example prints them side by side.

C++
#include <cmath>
#include <iostream>

int main() {
    std::cout << std::boolalpha;
    double pairs[][2] = {{5, 3}, {3, 5}, {4, 4}};

    for (auto& p : pairs) {
        std::cout << p[0] << " vs " << p[1]
                  << "  isgreater: " << std::isgreater(p[0], p[1])
                  << "  operator>: " << (p[0] > p[1]) << '\n';
    }
    return 0;
}

How It Works

  1. Each row prints the answer from the function and from the operator for the same two numbers.
  2. The two columns always match for real numbers: 5 > 3 is true, and 3 > 5 and 4 > 4 are false.
  3. 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.

Next: isgreaterequal()

Keep going through the <cmath> functions. Checks x >= y safely, false if either is NaN.

isgreaterequal() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful