Home »
C++ programming language
isunordered() Function with Example in C++
C++ isunordered() function: Here, we are going to learn about the isunordered() function with example of cmath header in C++ programming language?
Submitted by IncludeHelp, on May 18, 2020
C++ isunordered() function
isunordered() function is a library function of cmath header, it is used to check whether the given values are unordered (if one or both values are Not-A-Number (NaN)), then they are unordered values). It accepts two values (float, double or long double) and returns 1 if the given values are unordered; 0, otherwise.
Syntax
Syntax of isunordered() function:
In C99, it has been implemented as a macro,
macro isunordered(x, y)
Syntax
In C++11, it has been implemented as a function,
bool isunordered (float x, float y);
bool isunordered (double x, double y);
bool isunordered (long double x, long double y);
Parameter(s)
- x, y – represent the values to be checked as unordered.
Return value
The returns type of this function is bool, it returns 1 if one or both arguments are NaN; 0, otherwise.
Sample Input and Output
Input:
float x = sqrt(-1.0f);
float y = 10.0f;
Function call:
isunordered(x, y);
Output:
1
Input:
float x = 1.0f;
float y = 10.0f;
Function call:
isunordered(x, y);
Output:
0
Example
C++ code to demonstrate the example of isunordered() function:
// C++ code to demonstrate the example of
// isunordered() function
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << "isunordered(-5.0f, -2.0f): " << isunordered(-5.0f, -2.0f) << endl;
cout << "isunordered(sqrt(-1.0f), sqrt(-2.0f)): " << isunordered(sqrt(-1.0f), sqrt(-2.0f)) << endl;
cout << "isunordered(10.0f, sqrt(-1.0f)): " << isunordered(10.0f, sqrt(-1.0f)) << endl;
cout << "isunordered(sqrt(-1.0f), 1.0f): " << isunordered(sqrt(-1.0f), 1.0f) << endl;
float x = 10.0f;
float y = 5.0f;
// checking using the condition
if (isunordered(x, y)) {
cout << x << "," << y << " are unordered." << endl;
}
else {
cout << x << "," << y << " are not unordered." << endl;
}
x = 10.0f;
y = sqrt(-1.0f);
if (isunordered(x, y)) {
cout << x << "," << y << " are unordered." << endl;
}
else {
cout << x << "," << y << " are unordered." << endl;
}
return 0;
}
Output
isunordered(-5.0f, -2.0f): 0
isunordered(sqrt(-1.0f), sqrt(-2.0f)): 1
isunordered(10.0f, sqrt(-1.0f)): 1
isunordered(sqrt(-1.0f), 1.0f): 1
10,5 are not unordered.
10,-nan are unordered.
Reference: C++ isunordered() function