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