C++ islessequal()
The islessequal() function compares two floating-point values and returns true if the first value is less than or equal to the second. It performs a quiet comparison, meaning it does not raise floating-point exceptions and always returns false if either value is NaN.
This function is provided by the <cmath> header and is safer than using the <= operator when working with floating-point edge cases.
Syntax
islessequal(x, y)
Parameters:
x: The first numeric value to compare.y: The second numeric value to compare. Both parameters can be floating-point or integer types due to overloads.
Return value:
- Returns
trueifxis less than or equal toyand neither value isNaN. - Returns
falseotherwise, including when either argument isNaN.
Example 1: Comparing two numeric values
In this example, islessequal() is used to compare two regular numeric values:
#include <iostream>#include <cmath>using namespace std;int main() {double a = 4.5;double b = 7.2;cout << boolalpha;cout << islessequal(a, b) << endl;cout << islessequal(b, a) << endl;return 0;}
The output of this code is:
truefalse
Example 2: Comparison involving NaN
In this example, islessequal() safely handles a comparison involving NaN:
#include <iostream>#include <cmath>using namespace std;int main() {double x = 5.0;double y = NAN;cout << boolalpha;cout << islessequal(x, y) << endl;cout << islessequal(y, x) << endl;return 0;}
The output of this code is:
falsefalse
Codebyte Example
In this example, islessequal() is used inside a helper function to check ordering between different numeric inputs:
Frequently Asked Questions
1. What is the equal function in C++?
C++ does not have a standalone equal() function for basic comparisons; equality is typically checked using the == operator, or with helper functions like std::equal() for ranges.
2. What is the math library function in C++?
C++ math functions such as sqrt(), pow(), sin(), and islessequal() are provided by the <cmath> header.
3. Should I use equals() or == for string compare?
In C++, strings should be compared using == when working with std::string. The equals() method does not exist in standard C++.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve 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