isunordered() function in C++

In this post, we will see isunordered() function in C++. This is an inbuilt function and defined in <cmath> header file. Let’s see how we can use this function in a C++ program.

Syntax

The isunordered() function has the following syntax:

bool isunordered(float a, float b)

bool isunordered(double a, double b)

The isunordered() function takes two values as input and tests if they are unordered. It returns 1 if either or both of the two values is NAN otherwise it returns 0.

Example Program

See the below example.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    float a = 12.0;
    float b = sqrt(-12.0);
    
    cout << "a is " << a << endl;
    cout << "b is " << b << endl;
    
    if(isunordered(a, b) == 1)
    {
        cout << "a and b are unordered\n";
    }
    else
    {
        cout << "a and b are not unordered\n";
    }
    return 0;
}

Output:

a is 12
b is -nan
a and b are unordered

Let’s see another example in C++.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double a = 12.0;
    double b = -12.0;
    
    cout << "a is " << a << endl;
    cout << "b is " << b << endl;
    
    if(isunordered(a, b) == 1)
    {
        cout << "a and b are unordered\n";
    }
    else
    {
        cout << "a and b are not unordered\n";
    }
    return 0;
}

Output:

a is 12
b is -12
a and b are not unordered

As is evident from the above examples, we can use isunordered() function to find out whether two values are meaningfully comparable or not. In the first example, we have two values, one of which is -nan. Hence the function returns 1. In the second example, none of the passed values is nan and hence the isunordered() function returns 0.

Hope it was helpful.

Also read: isnormal() function in C++

Leave a Reply

Your email address will not be published.