Skip to content

Commit 2916892

Browse files
committed
[term entry] C++ math-functions: isgreaterequal()
1 parent 2a1d955 commit 2916892

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
Title: 'isgreaterequal()'
3+
Description: 'Determines if the floating-point number `x` is greater than or equal to the floating-point number `y`, without setting floating-point exceptions.'
4+
Subjects:
5+
6+
Tags:
7+
- 'Functions'
8+
- 'Comparison'
9+
- 'Math'
10+
CatalogContent:
11+
- 'learn-c-plus-plus'
12+
---
13+
14+
The **`isgreaterequal()`** [function](https://www.codecademy.com/resources/docs/cpp/functions) compares two floating-point values and returns `true` only when the first is greater than or equal to the second. Never raises floating-point exceptions, and always returns `false` if one or both arguments is `NaN`. The function is available through the `<cmath>` header.
15+
16+
## Syntax
17+
18+
```
19+
isgreaterequal(x, y)
20+
```
21+
22+
**Parameters:**
23+
24+
- `x`, `y`: floating-point or integers types.
25+
26+
27+
> **Notes:**
28+
> + The function `isgreaterequal()`is defined with overloads so it works with any mix of arithmetic values.
29+
> + The built-in operator>= for floating-point numbers may raise `FE_INVALID` if one or both of the arguments is NaN. The function `isgreaterequal()` is a "quiet" version of operator>=.
30+
31+
**Return value:**
32+
33+
The `isgreaterequal()` function returns:
34+
35+
- `true` if `x >= y` and neither argument is `NaN`
36+
- `false` otherwise, including when either value is `NaN`
37+
38+
## Example
39+
40+
The following example checks whether one number is greater than another, including a comparison involving `NaN`:
41+
42+
```cpp
43+
#include <iostream>
44+
#include <cmath>
45+
46+
using namespace std;
47+
48+
int main() {
49+
float x = 5.5;
50+
int y = 3;
51+
double z = nan("1");
52+
53+
cout << boolalpha;
54+
cout << x << " isgreaterequal to " << y << ": " << isgreaterequal(x, y) << endl;
55+
cout << y << " isgreaterequal to " << x << ": " << isgreaterequal(y, x) << endl;
56+
cout << x << " isgreaterequal to " << z << ": " << isgreaterequal(x, z) << " (NaN comparison)" << endl;
57+
58+
return 0;
59+
}
60+
```
61+
62+
The output of this code is as follows:
63+
64+
```shell
65+
5.5 isgreaterequal to 3: true
66+
3 isgreaterequal to 5.5: false
67+
5.5 isgreaterequal to nan: false
68+
```
69+
70+
## Codebyte Example
71+
72+
The following example is runnable and outputs whether one number is greater than or equal to another using `isgreaterequal()`:
73+
74+
```codebyte/cpp
75+
#include <iostream>
76+
#include <cmath>
77+
using namespace std;
78+
79+
int main() {
80+
float a = 2.5;
81+
float b = 7.3;
82+
83+
cout << boolalpha;
84+
cout << a << " isgreaterequal to " << b << ": " << isgreaterequal(a, b) << endl;
85+
cout << b << " isgreaterequal to " << a << ": " << isgreaterequal(b, a) << endl;
86+
87+
return 0;
88+
}
89+
```

0 commit comments

Comments
 (0)