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
Call
What 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
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 less than or equal to y.
false
x 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;
}
Output
1 vs 4: true
4 vs 4: true
6 vs 4: false
-2 vs -2.5: false
NaN vs 4: false
How It Works
Each pair is checked with islessequal(), and the loop prints the answer.
1 is smaller than 4, and 4 equals 4, so both are true.
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.
10 in [10, 20]: true
15.5 in [10, 20]: true
20 in [10, 20]: true
20.5 in [10, 20]: false
nan in [10, 20]: false
How It Works
inRange() says "at least low and at most high" with two quiet comparisons joined by &&.
The two ends 10 and 20 are inside the range because equal values are allowed. 15.5 is in the middle.
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.