C++ std::islessequal() Function

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

Definition

In C++, std::islessequal(x, y) asks "is x less than or equal to y?" It is the quiet version of x <= y. islessequal(2, 2) is true, but isless(2, 2) is false.

If a value is NaN, you get false without any floating-point exception. Use it for limit checks, such as "is the price within budget?", when some prices may be missing.

std::islessequal() 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::islessequal. Start with these lines.

C++
bool a = std::islessequal(2.0, 3.0);   // true
bool b = std::islessequal(3.0, 3.0);   // true (equal is allowed)
bool c = std::islessequal(4.0, 3.0);   // false
CallWhat it does
std::islessequal(2.0, 3.0)2 is smaller. Returns true.
std::islessequal(3.0, 3.0)Equal values count. Returns true.
std::islessequal(std::nan(""), 3.0)One value is NaN. 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 islessequal(float x, float y);
bool islessequal(double x, double y);
bool islessequal(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 less than or equal to y.
falsex is greater than y, or one of them is NaN.

C++ Examples

Example 1 — Compare Several Pairs

Test smaller, equal, bigger, and NaN pairs. The equal pair differs from isless().

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

struct Pair {
    const char* label;
    double x;
    double y;
};

int main() {
    std::cout << std::boolalpha;
    Pair pairs[] = {
        {"1 vs 4", 1, 4},
        {"4 vs 4", 4, 4},
        {"6 vs 4", 6, 4},
        {"-2 vs -2.5", -2, -2.5},
        {"NaN vs 4", std::nan(""), 4}
    };

    for (const Pair& p : pairs) {
        std::cout << p.label << ": " << std::islessequal(p.x, p.y) << '\n';
    }
    return 0;
}

How It Works

  1. Each pair is checked with islessequal(), and the loop prints the answer.
  2. 1 is smaller than 4, and 4 equals 4, so both are true.
  3. 6 vs 4 is bigger, so it is false. -2 is bigger than -2.5, so that is false too. The NaN row is always false.

Example 2 — Items Within Budget

A price is acceptable if it is at most 100. One price is missing (NaN), and it must be rejected.

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

int main() {
    std::cout << std::boolalpha;
    double prices[] = {99.99, 100.0, 100.01, std::nan("")};

    for (double p : prices) {
        std::cout << "price " << p << " ok: "
                  << std::islessequal(p, 100.0) << '\n';
    }
    return 0;
}

How It Works

  1. islessequal(p, 100.0) is true when the price is 100 or lower.
  2. 99.99 is under the limit, and 100 is exactly on it, so both are allowed. 100.01 is one cent over, so it is rejected.
  3. The missing price is NaN, so the answer is false and the item is rejected. On some systems the NaN prints as -nan.

Example 3 — Check if a Value Is in a Range

Combine isgreaterequal() and islessequal() to test that a value is between 10 and 20, with both ends allowed.

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

bool inRange(double x, double low, double high) {
    return std::isgreaterequal(x, low) && std::islessequal(x, high);
}

int main() {
    std::cout << std::boolalpha;
    double values[] = {10.0, 15.5, 20.0, 20.5, std::nan("")};

    for (double v : values) {
        std::cout << v << " in [10, 20]: " << inRange(v, 10.0, 20.0) << '\n';
    }
    return 0;
}

How It Works

  1. inRange() says "at least low and at most high" with two quiet comparisons joined by &&.
  2. The two ends 10 and 20 are inside the range because equal values are allowed. 15.5 is in the middle.
  3. 20.5 is above the top, so it fails. NaN fails the first test, so the whole answer is false. On some systems NaN prints as -nan.

Frequently Asked Questions

std::islessequal(x, y) returns true if x is less than or equal to y. It works like x <= y, but is quiet with NaN and returns false if either value is NaN.
true if x <= y, otherwise false. If either argument is NaN, the result is false.
isless() is false for equal numbers, and islessequal() is true for equal numbers. Both are false when a NaN is involved.
Use std::isgreaterequal(x, low) && std::islessequal(x, high). Both calls are quiet with NaN, so a NaN value is simply reported as outside the range.
Include <cmath> and write std::islessequal(). That always works and picks the C++ overloads. The global name ::islessequal often works too, but the standard does not promise it, and it may only take a double.

Next: islessgreater()

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

islessgreater() 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.

14 people found this page helpful