C++ std::isless() Function

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

Definition

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

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 y.
falsex 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;
}

How It Works

  1. Each pair is passed to isless(), and the answer is printed beside its label.
  2. 2 < 3 and -5 < -1 are true. The larger first value and the equal pair give false.
  3. 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.

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

int main() {
    std::cout << std::boolalpha;
    double temps[] = {-3.5, 0.0, 4.2, std::nan("")};

    for (double t : temps) {
        std::cout << "temp " << t << " below freezing: "
                  << std::isless(t, 0.0) << '\n';
    }
    return 0;
}

How It Works

  1. isless(t, 0.0) is true only when the temperature is strictly under 0.
  2. -3.5 is below zero, so it prints true. Exactly 0 is not below zero, and 4.2 is above it, so both print false.
  3. The broken sensor (NaN) also gives false: there is no proof it is freezing. Depending on the system, the NaN may print as -nan.

Example 3 — Why "Not Less" Is Not "Greater or Equal"

Compare isless(), its negation, and isgreaterequal() for a normal pair and for a pair with NaN.

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

void show(const char* label, double x, double y) {
    std::cout << label << "  isless: " << std::isless(x, y)
              << "  !isless: " << !std::isless(x, y)
              << "  isgreaterequal: " << std::isgreaterequal(x, y) << '\n';
}

int main() {
    std::cout << std::boolalpha;
    show("5 vs 3  ", 5.0, 3.0);
    show("NaN vs 3", std::nan(""), 3.0);
    return 0;
}

How It Works

  1. The function prints three answers for the same pair, so you can compare them in one row.
  2. For 5 vs 3 the columns agree: !isless and isgreaterequal are both true.
  3. 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.

Next: islessequal()

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

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

13 people found this page helpful