Skip to content

Commit 0938397

Browse files
[Term Entry] C++ Math-functions: islessequal()
1 parent fb43261 commit 0938397

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
Title: 'islessequal()'
3+
Description: 'Checks whether the first floating-point value is less than or equal to the second, without raising floating-point exceptions.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Game Development'
7+
Tags:
8+
- 'Arithmetic'
9+
- 'Functions'
10+
- 'Math'
11+
- 'Numbers'
12+
CatalogContent:
13+
- 'learn-c-plus-plus'
14+
- 'paths/computer-science'
15+
---
16+
17+
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`.
18+
19+
This function is provided by the `<cmath>` header and is safer than using the `<=` operator when working with floating-point edge cases.
20+
21+
## Syntax
22+
23+
```pseudo
24+
islessequal(x, y)
25+
```
26+
27+
**Parameters:**
28+
29+
- `x`: The first numeric value to compare.
30+
- `y`: The second numeric value to compare.
31+
Both parameters can be floating-point or integer types due to overloads.
32+
33+
**Return value:**
34+
35+
- Returns `true` if `x` is less than or equal to `y` and neither value is `NaN`.
36+
- Returns `false` otherwise, including when either argument is `NaN`.
37+
38+
## Example 1: Comparing two numeric values
39+
40+
In this example, `islessequal()` is used to compare two regular numeric values:
41+
42+
```cpp
43+
#include <iostream>
44+
#include <cmath>
45+
using namespace std;
46+
47+
int main() {
48+
double a = 4.5;
49+
double b = 7.2;
50+
51+
cout << boolalpha;
52+
cout << islessequal(a, b) << endl;
53+
cout << islessequal(b, a) << endl;
54+
55+
return 0;
56+
}
57+
```
58+
59+
The output of this code is:
60+
61+
```shell
62+
true
63+
false
64+
```
65+
66+
## Example 2: Comparison involving `NaN`
67+
68+
In this example, `islessequal()` safely handles a comparison involving `NaN`:
69+
70+
```cpp
71+
#include <iostream>
72+
#include <cmath>
73+
using namespace std;
74+
75+
int main() {
76+
double x = 5.0;
77+
double y = NAN;
78+
79+
cout << boolalpha;
80+
cout << islessequal(x, y) << endl;
81+
cout << islessequal(y, x) << endl;
82+
83+
return 0;
84+
}
85+
```
86+
87+
The output of this code is:
88+
89+
```shell
90+
false
91+
false
92+
```
93+
94+
## Codebyte Example
95+
96+
In this example, `islessequal()` is used inside a helper function to check ordering between different numeric inputs:
97+
98+
```codebyte/cpp
99+
#include <iostream>
100+
#include <cmath>
101+
using namespace std;
102+
103+
void checkOrder(double a, double b) {
104+
if (islessequal(a, b))
105+
cout << a << " is less than or equal to " << b << endl;
106+
else
107+
cout << a << " is greater than " << b << endl;
108+
}
109+
110+
int main() {
111+
checkOrder(3.0, 3.0);
112+
checkOrder(2.5, 4.1);
113+
checkOrder(6.0, 1.0);
114+
}
115+
```
116+
117+
## Frequently Asked Questions
118+
119+
### 1. What is the equal function in C++?
120+
121+
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.
122+
123+
### 2. What is the math library function in C++?
124+
125+
C++ math functions such as `sqrt()`, `pow()`, `sin()`, and `islessequal()` are provided by the `<cmath>` header.
126+
127+
### 3. Should I use `equals()` or `==` for string compare?
128+
129+
In C++, strings should be compared using `==` when working with `std::string`. The `equals()` method does not exist in standard C++.

0 commit comments

Comments
 (0)