|
| 1 | +--- |
| 2 | +Title: 'isunordered()' |
| 3 | +Description: 'Returns true if one or both of the arguments is a NaN value' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | +Tags: |
| 7 | + - 'Arithmetic' |
| 8 | + - 'Functions' |
| 9 | + - 'Numbers' |
| 10 | +CatalogContent: |
| 11 | + - 'learn-c-plus-plus' |
| 12 | + - 'paths/computer-science' |
| 13 | +--- |
| 14 | + |
| 15 | +When comparing two floating-point numbers, the comparison behaves as unordered if one of them is a `NaN` (Not a Number). The function **`isunordered()`** receives two floating-point numbers and checks if either of them is a `NaN`. `isunordered()` returns `true` if one or both of the arguments is `NaN`, and returns `false` only if both arguments are normal floating-point numbers. |
| 16 | + |
| 17 | +## Syntax |
| 18 | + |
| 19 | +```pseudo |
| 20 | +isunordered(a, b) |
| 21 | +``` |
| 22 | +The function `isunordered(a, b)` receives two arguments `a` and `b`, of type `double`, `float`, or `long double` data types and returns `true` if either `a` or `b` is `NaN`. It will return `false` only if both `a` and `b` are normal floating-point numbers. |
| 23 | + |
| 24 | +## Example |
| 25 | + |
| 26 | +```cpp |
| 27 | +#include <iostream> |
| 28 | +#include <cmath> |
| 29 | +using namespace std; |
| 30 | + |
| 31 | +int main() |
| 32 | +{ |
| 33 | + cout << (isunordered(3.0, NAN)?"true":"false") << endl; |
| 34 | + // returns true |
| 35 | + cout << (isunordered(NAN, 3.0)?"true":"false") << endl; |
| 36 | + // returns true |
| 37 | + cout << (isunordered(NAN, 3.0)?"true":"false") << endl; |
| 38 | + // returns true |
| 39 | + cout << (isunordered(3.0, 4.0)?"true":"false") << endl; |
| 40 | + // returns false |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +## Codebyte Example |
| 45 | + |
| 46 | +The following example is runnable and shows what the function `isunordered()` resolves to, when given different kinds of arguments. In the example the helper function `checkNaN()` takes in two double arguments `a` and `b` and passes them to `isunordered()` and outputs the appropriate message depending on the condition `isunordered()` returns. the `main()` function calls `checkNaN()` multiple times with different arguments. For simplicity, `checkNaN()` only takes arguments with `double` data type. |
| 47 | + |
| 48 | +```codebyte/cpp |
| 49 | +#include <iostream> |
| 50 | +#include <cmath> |
| 51 | +using namespace std; |
| 52 | +
|
| 53 | +void checkNaN(double a, double b) { |
| 54 | + if (isunordered(a, b)) |
| 55 | + cout << a << " is NOT comparable with " << b << endl; |
| 56 | + else |
| 57 | + cout << a << " is comparable with " << b << endl; |
| 58 | +} |
| 59 | +
|
| 60 | +
|
| 61 | +int main() |
| 62 | +{ |
| 63 | + double nan = NAN; // a NaN (Not a Number) value |
| 64 | + double pi = 3.14; // a regular double number |
| 65 | + double c = 2.99792458e8; // a regular double number |
| 66 | +
|
| 67 | + checkNaN(nan, nan); |
| 68 | + // outputs: nan is NOT comparable with nan |
| 69 | + checkNaN(nan, pi); |
| 70 | + // outputs: nan is NOT comparable with 3.14 |
| 71 | + checkNaN(c, nan); |
| 72 | + // outputs: 2.99792e+08 is NOT comparable with nan |
| 73 | + checkNaN(c, pi); |
| 74 | + // outputs: 2.99792e+08 is comparable with 3.14 |
| 75 | +} |
| 76 | +``` |
0 commit comments