C++ isless()

Anonymous contributor's avatar
Anonymous contributor
Published Jan 30, 2026
Contribute to Docs

The C++ isless() function returns true if the first argument is strictly less than the second argument, and false otherwise. It performs a quiet floating-point comparison, never raises exceptions, and returns false if either value is NaN. The function is defined in the <cmath> header.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours

Syntax

bool isless(x, y);

Parameters:

  • x: The first value for comparison.
  • y: The second value for comparison.

Both x and y are either floating-point values (double, float, or long double) or integers, which are implicitly converted to a floating-point type.

Return value:

The isless() function returns true if x < y and both values are valid numbers. Returns false if the comparison is false or if either argument is NaN.

Example

The following example uses the isless() function to compare and determine if the first argument is strictly less than the second argument:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
bool result;
int x, y;
x = 5;
y = 7;
result = isless(x, y);
cout << x << " is less than " << y << ": " << result << endl;
x = 9;
y = 3;
result = isless(x, y);
cout << x << " is less than " << y << ": " << result << endl;
double z = nan("1");
result = isless(x, z);
cout << x << " is less than NaN: " << result << endl;
return 0;
}

This produces the following output:

5 is less than 7: 1
9 is less than 3: 0
9 is less than NaN: 0

Codebyte Example

This example uses isless() inside conditional logic to safely compare values, including NaNs:

Code
Output

Use isless() in situations where direct comparison operators might behave unpredictably:

  • When comparisons may involve NaN, since isless() guarantees a defined boolean result.
  • When writing safe comparison logic that avoids undefined behavior.
  • When building math-heavy code such as sorting, filtering, or numerical checks that require robust comparisons.

All contributors

Contribute to Docs

Learn C++ on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours