From 707ee3d390e8cce2799e08a5cb42011528304726 Mon Sep 17 00:00:00 2001 From: abey Date: Sat, 6 Dec 2025 15:49:44 +1030 Subject: [PATCH 1/2] [Term Entry] C++ Math-functions: isunordered() #8022 --- .../terms/isunordered/isunordered.md | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 content/cpp/concepts/math-functions/terms/isunordered/isunordered.md diff --git a/content/cpp/concepts/math-functions/terms/isunordered/isunordered.md b/content/cpp/concepts/math-functions/terms/isunordered/isunordered.md new file mode 100644 index 00000000000..fdbd68cf7fa --- /dev/null +++ b/content/cpp/concepts/math-functions/terms/isunordered/isunordered.md @@ -0,0 +1,76 @@ +--- +Title: 'isunordered()' +Description: 'Returns true if one or both of the arguments is a NaN value' +Subjects: + - 'Computer Science' +Tags: + - 'Arithmetic' + - 'Functions' + - 'Numbers' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +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. + +## Syntax + +```pseudo +isunordered(a, b) +``` +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. + +## Example + +```cpp +#include +#include +using namespace std; + +int main() +{ + cout << (isunordered(3.0, NAN)?"true":"false") << endl; + // returns true + cout << (isunordered(NAN, 3.0)?"true":"false") << endl; + // returns true + cout << (isunordered(NAN, 3.0)?"true":"false") << endl; + // returns true + cout << (isunordered(3.0, 4.0)?"true":"false") << endl; + // returns false +} +``` + +## Codebyte Example + +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. + +```codebyte/cpp +#include +#include +using namespace std; + +void checkNaN(double a, double b) { + if (isunordered(a, b)) + cout << a << " is NOT comparable with " << b << endl; + else + cout << a << " is comparable with " << b << endl; +} + + +int main() +{ + double nan = NAN; // a NaN (Not a Number) value + double pi = 3.14; // a regular double number + double c = 2.99792458e8; // a regular double number + + checkNaN(nan, nan); + // outputs: nan is NOT comparable with nan + checkNaN(nan, pi); + // outputs: nan is NOT comparable with 3.14 + checkNaN(c, nan); + // outputs: 2.99792e+08 is NOT comparable with nan + checkNaN(c, pi); + // outputs: 2.99792e+08 is comparable with 3.14 +} +``` \ No newline at end of file From 19d91371fa1baed46ebbc0a734ec637e79bfe3cf Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 8 Dec 2025 15:55:32 +0530 Subject: [PATCH 2/2] 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. --- .../terms/isunordered/isunordered.md | 72 +++++++++++-------- 1 file changed, 42 insertions(+), 30 deletions(-) diff --git a/content/cpp/concepts/math-functions/terms/isunordered/isunordered.md b/content/cpp/concepts/math-functions/terms/isunordered/isunordered.md index fdbd68cf7fa..84c6df6dc88 100644 --- a/content/cpp/concepts/math-functions/terms/isunordered/isunordered.md +++ b/content/cpp/concepts/math-functions/terms/isunordered/isunordered.md @@ -1,8 +1,9 @@ --- Title: 'isunordered()' -Description: 'Returns true if one or both of the arguments is a NaN value' +Description: 'Checks whether either of two floating-point values is NaN, returning true if the comparison between them is unordered.' Subjects: - 'Computer Science' + - 'Game Development' Tags: - 'Arithmetic' - 'Functions' @@ -12,17 +13,30 @@ CatalogContent: - 'paths/computer-science' --- -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. +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. ## Syntax ```pseudo isunordered(a, b) ``` -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. + +**Parameters:** + +- `a`: A floating-point value. +- `b`: A floating-point value. + +Both arguments may be `float`, `double`, `long double`, or mixed arithmetic types (due to overloads). + +**Return value:** + +- Returns `true` if either `a` or `b` is `NaN`, which makes the comparison unordered (no meaningful `<`, `>`, or `==` relationship exists). +- Returns `false` otherwise. ## Example +In this example several value pairs are tested with `isunordered()` to demonstrate when comparisons involving NaN are unordered: + ```cpp #include #include @@ -30,20 +44,23 @@ using namespace std; int main() { - cout << (isunordered(3.0, NAN)?"true":"false") << endl; - // returns true - cout << (isunordered(NAN, 3.0)?"true":"false") << endl; - // returns true - cout << (isunordered(NAN, 3.0)?"true":"false") << endl; - // returns true - cout << (isunordered(3.0, 4.0)?"true":"false") << endl; - // returns false + cout << (isunordered(3.0, NAN)?"true":"false") << endl; + cout << (isunordered(NAN, 3.0)?"true":"false") << endl; + cout << (isunordered(3.0, 4.0)?"true":"false") << endl; } ``` +The output of this code is: + +```shell +true +true +false +``` + ## Codebyte Example -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. +In this example a helper function prints whether two doubles can be compared or if their comparison is unordered because of `NaN` involvement: ```codebyte/cpp #include @@ -51,26 +68,21 @@ The following example is runnable and shows what the function `isunordered()` re using namespace std; void checkNaN(double a, double b) { - if (isunordered(a, b)) - cout << a << " is NOT comparable with " << b << endl; - else - cout << a << " is comparable with " << b << endl; + if (isunordered(a, b)) + cout << a << " is NOT comparable with " << b << endl; + else + cout << a << " is comparable with " << b << endl; } - int main() { - double nan = NAN; // a NaN (Not a Number) value - double pi = 3.14; // a regular double number - double c = 2.99792458e8; // a regular double number - - checkNaN(nan, nan); - // outputs: nan is NOT comparable with nan - checkNaN(nan, pi); - // outputs: nan is NOT comparable with 3.14 - checkNaN(c, nan); - // outputs: 2.99792e+08 is NOT comparable with nan - checkNaN(c, pi); - // outputs: 2.99792e+08 is comparable with 3.14 + double nan = NAN; // a NaN (Not a Number) value + double pi = 3.14; // a regular double number + double c = 2.99792458e8; // a regular double number + + checkNaN(nan, nan); + checkNaN(nan, pi); + checkNaN(c, nan); + checkNaN(c, pi); } -``` \ No newline at end of file +```