In C++, std::isless(x, y) asks "is x less than y?" It is the quiet version of x < y. isless(2, 3) is true, and isless(3, 2) is false.
With a NaN the answer is false and no floating-point exception flag is raised. There is one trap: !isless(x, y) is not the same as isgreaterequal(x, y) when NaN is involved. Example 3 shows it.
std::isless() 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::isless. Start with these lines.
C++
bool a = std::isless(2.0, 3.0); // true
bool b = std::isless(3.0, 2.0); // false
bool c = std::isless(std::nan(""), 1.0); // false (NaN)
Call
What it does
std::isless(2.0, 3.0)
2 is smaller than 3. Returns true.
std::isless(2.0, 2.0)
Equal numbers are not less. Returns false.
std::isless(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 isless(float x, float y);
bool isless(double x, double y);
bool isless(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 y.
false
x is greater 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 smaller, bigger, equal, negative, and NaN.
C++
#include <cmath>
#include <iostream>
struct Pair {
const char* label;
double x;
double y;
};
int main() {
std::cout << std::boolalpha;
Pair pairs[] = {
{"2 vs 3", 2, 3},
{"3 vs 2", 3, 2},
{"3 vs 3", 3, 3},
{"-5 vs -1", -5, -1},
{"NaN vs 1", std::nan(""), 1}
};
for (const Pair& p : pairs) {
std::cout << p.label << ": " << std::isless(p.x, p.y) << '\n';
}
return 0;
}
Output
2 vs 3: true
3 vs 2: false
3 vs 3: false
-5 vs -1: true
NaN vs 1: false
How It Works
Each pair is passed to isless(), and the answer is printed beside its label.
2 < 3 and -5 < -1 are true. The larger first value and the equal pair give false.
The pair with NaN gives false and no error. NaN is not less than anything.
Example 2 — Below Freezing
Check whether each temperature is below 0 degrees. A broken sensor sends NaN.
5 vs 3 isless: false !isless: true isgreaterequal: true
NaN vs 3 isless: false !isless: true isgreaterequal: false
How It Works
The function prints three answers for the same pair, so you can compare them in one row.
For 5 vs 3 the columns agree: !isless and isgreaterequal are both true.
For the NaN pair, isless is false, so !isless becomes true, but isgreaterequal is false. They only match when no NaN is involved.
Frequently Asked Questions
std::isless(x, y) returns true if x is less than 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.
For normal numbers the answer is the same. With NaN, the < operator may raise a floating-point exception flag, but isless() is quiet and returns false.
It returns false. NaN has no order compared to any number, so it is not less than anything.
Include <cmath> and write std::isless(). That always works and picks the C++ overloads. The global name ::isless often works too, but the standard does not promise it, and it may only take a double.