Skip to content

Commit 19d9137

Browse files
Refine isunordered function documentation
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.
1 parent 04dc3f2 commit 19d9137

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed
Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
---
22
Title: 'isunordered()'
3-
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.'
44
Subjects:
55
- 'Computer Science'
6+
- 'Game Development'
67
Tags:
78
- 'Arithmetic'
89
- 'Functions'
@@ -12,65 +13,76 @@ CatalogContent:
1213
- 'paths/computer-science'
1314
---
1415

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.
1617

1718
## Syntax
1819

1920
```pseudo
2021
isunordered(a, b)
2122
```
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.
2335

2436
## Example
2537

38+
In this example several value pairs are tested with `isunordered()` to demonstrate when comparisons involving NaN are unordered:
39+
2640
```cpp
2741
#include <iostream>
2842
#include <cmath>
2943
using namespace std;
3044

3145
int main()
3246
{
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
47+
cout << (isunordered(3.0, NAN)?"true":"false") << endl;
48+
cout << (isunordered(NAN, 3.0)?"true":"false") << endl;
49+
cout << (isunordered(3.0, 4.0)?"true":"false") << endl;
4150
}
4251
```
4352

53+
The output of this code is:
54+
55+
```shell
56+
true
57+
true
58+
false
59+
```
60+
4461
## Codebyte Example
4562

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.
63+
In this example a helper function prints whether two doubles can be compared or if their comparison is unordered because of `NaN` involvement:
4764

4865
```codebyte/cpp
4966
#include <iostream>
5067
#include <cmath>
5168
using namespace std;
5269
5370
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;
5875
}
5976
60-
6177
int main()
6278
{
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
82+
83+
checkNaN(nan, nan);
84+
checkNaN(nan, pi);
85+
checkNaN(c, nan);
86+
checkNaN(c, pi);
7587
}
76-
```
88+
```

0 commit comments

Comments
 (0)