You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Updated the description of the isunordered function for clarity and added detailed parameter and return value sections. Enhanced examples to demonstrate usage with NaN values.
Description: 'Returns true if one or both of the arguments is a NaN value'
3
+
Description: 'Checks whether either of two floating-point values is NaN, returning true if the comparison between them is unordered.'
4
4
Subjects:
5
5
- 'Computer Science'
6
+
- 'Game Development'
6
7
Tags:
7
8
- 'Arithmetic'
8
9
- 'Functions'
@@ -12,65 +13,76 @@ CatalogContent:
12
13
- 'paths/computer-science'
13
14
---
14
15
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
+
The **`isunordered()`**function returns `true`if either of the two floating-point values is `NaN`, meaning the comparison is unordered because no meaningful relational comparison (such as `<`, `>`, or `==`) can be made.
16
17
17
18
## Syntax
18
19
19
20
```pseudo
20
21
isunordered(a, b)
21
22
```
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
+
**Parameters:**
25
+
26
+
-`a`: A floating-point value.
27
+
-`b`: A floating-point value.
28
+
29
+
Both arguments may be `float`, `double`, `long double`, or mixed arithmetic types (due to overloads).
30
+
31
+
**Return value:**
32
+
33
+
- Returns `true` if either `a` or `b` is `NaN`, which makes the comparison unordered (no meaningful `<`, `>`, or `==` relationship exists).
34
+
- Returns `false` otherwise.
23
35
24
36
## Example
25
37
38
+
In this example several value pairs are tested with `isunordered()` to demonstrate when comparisons involving NaN are unordered:
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.
63
+
In this example a helper function prints whether two doubles can be compared or if their comparison is unordered because of `NaN` involvement:
47
64
48
65
```codebyte/cpp
49
66
#include <iostream>
50
67
#include <cmath>
51
68
using namespace std;
52
69
53
70
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;
71
+
if (isunordered(a, b))
72
+
cout << a << " is NOT comparable with " << b << endl;
73
+
else
74
+
cout << a << " is comparable with " << b << endl;
58
75
}
59
76
60
-
61
77
int main()
62
78
{
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
79
+
double nan = NAN; // a NaN (Not a Number) value
80
+
double pi = 3.14; // a regular double number
81
+
double c = 2.99792458e8; // a regular double number
0 commit comments