|
| 1 | +--- |
| 2 | +Title: 'islessgreater()' |
| 3 | +Description: 'Determines whether a floating-point value is less than or greater than another without setting floating-point exceptions.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | +Tags: |
| 7 | + - 'Functions' |
| 8 | + - 'Arithmetic' |
| 9 | +CatalogContent: |
| 10 | + - 'learn-c-plus-plus' |
| 11 | + - 'paths/computer-science' |
| 12 | +--- |
| 13 | + |
| 14 | +The **`islessgreater()`** function determines whether a floating-point value is less than or greater than another floating-point value. Unlike the built-in comparison operators (`<` and `>`), this function does not raise `FE_INVALID` exceptions when the arguments are `NaN`. |
| 15 | + |
| 16 | +## Syntax |
| 17 | + |
| 18 | +```pseudo |
| 19 | +islessgreater(x, y) |
| 20 | +``` |
| 21 | + |
| 22 | +The function takes two parameters: |
| 23 | + |
| 24 | +- `x`: A floating-point or integer value to compare. |
| 25 | +- `y`: A floating-point or integer value to compare. |
| 26 | + |
| 27 | +The return value is `true` if `x` is less than or greater than `y` (i.e., `x < y || x > y`). Otherwise, `false` is returned. |
| 28 | + |
| 29 | +> **Note:** This function is particularly useful when working with floating-point values that might be `NaN`, as it provides a "quiet" comparison that won't trigger floating-point exceptions. |
| 30 | +
|
| 31 | +## Example |
| 32 | + |
| 33 | +In the example below, the `islessgreater()` function compares two floating-point values: |
| 34 | + |
| 35 | +```cpp |
| 36 | +#include <iostream> |
| 37 | +#include <cmath> |
| 38 | +using namespace std; |
| 39 | + |
| 40 | +int main() { |
| 41 | + double x = 5.0; |
| 42 | + double y = 10.0; |
| 43 | + |
| 44 | + cout << "islessgreater(5.0, 10.0): " << islessgreater(x, y) << "\n"; |
| 45 | + cout << "islessgreater(10.0, 5.0): " << islessgreater(y, x) << "\n"; |
| 46 | + cout << "islessgreater(5.0, 5.0): " << islessgreater(x, x) << "\n"; |
| 47 | + |
| 48 | + return 0; |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +This produces the following output: |
| 53 | + |
| 54 | +``` |
| 55 | +islessgreater(5.0, 10.0): 1 |
| 56 | +islessgreater(10.0, 5.0): 1 |
| 57 | +islessgreater(5.0, 5.0): 0 |
| 58 | +``` |
| 59 | + |
| 60 | +## Codebyte Example |
| 61 | + |
| 62 | +The example below demonstrates the `islessgreater()` function with different data types and edge cases, including `NaN` values: |
| 63 | + |
| 64 | +```codebyte/cpp |
| 65 | +#include <iostream> |
| 66 | +#include <cmath> |
| 67 | +using namespace std; |
| 68 | +
|
| 69 | +int main() { |
| 70 | + float f1 = 3.14; |
| 71 | + float f2 = 2.71; |
| 72 | + double d1 = 100.0; |
| 73 | + double d2 = 100.0; |
| 74 | + long double ld1 = 1.5L; |
| 75 | + long double ld2 = 2.5L; |
| 76 | + |
| 77 | + // NaN comparison |
| 78 | + double nan_val = 0.0 / 0.0; |
| 79 | + double normal_val = 5.0; |
| 80 | + |
| 81 | + cout << "islessgreater(3.14, 2.71): " << islessgreater(f1, f2) << "\n"; |
| 82 | + cout << "islessgreater(100.0, 100.0): " << islessgreater(d1, d2) << "\n"; |
| 83 | + cout << "islessgreater(1.5L, 2.5L): " << islessgreater(ld1, ld2) << "\n"; |
| 84 | + cout << "islessgreater(NaN, 5.0): " << islessgreater(nan_val, normal_val) << "\n"; |
| 85 | +} |
| 86 | +``` |
0 commit comments