C++ std::isunordered() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

In C++, std::isunordered(x, y) asks "can these two numbers be compared at all?" Two numbers are "unordered" when at least one of them is NaN, because NaN is not smaller, bigger, or equal to anything.

It is the opposite of the other comparison functions. If isunordered(x, y) is false, then exactly one of x < y, x > y, or x == y is true. Infinity is a normal ordered value, so isunordered(inf, 1) is false.

std::isunordered() is declared in <cmath> and is part of the group Classification and comparison. These tools ask what kind of number you have: normal, zero, infinite, or NaN. In C++ they are ordinary overloaded functions in std, not macros.

Syntax

Include <cmath> and call std::isunordered. Start with these lines.

C++
bool a = std::isunordered(1.0, 2.0);        // false
bool b = std::isunordered(std::nan(""), 2.0); // true
bool c = std::isunordered(2.0, std::nan(""));  // true
CallWhat it does
std::isunordered(1.0, 2.0)Both are real numbers. Returns false.
std::isunordered(std::nan(""), 2.0)The first is NaN. Returns true.
std::isunordered(2.0, std::nan(""))The second is NaN. Returns true.

The result is a bool. Add std::boolalpha before printing to see true or false instead of 1 or 0.

Library declarations
C++
bool isunordered(float x, float y);
bool isunordered(double x, double y);
bool isunordered(long double x, long double y);
// plus overloads for mixed and integer arguments

Parameters

ParameterDescription
xRequired. The first number to compare. It can be float, double, long double, or an integer type.
yRequired. The second number to compare.

Return Value

Return ValueDescription
trueAt least one of x and y is NaN.
falseBoth x and y are real numbers (infinity included).

C++ Examples

Example 1 — Compare Several Pairs

Test normal pairs, pairs with a NaN in either place, a pair of two NaNs, and a pair with infinity.

C++
#include <cmath>
#include <iostream>
#include <limits>

struct Pair {
    const char* label;
    double x;
    double y;
};

int main() {
    std::cout << std::boolalpha;
    double inf = std::numeric_limits<double>::infinity();
    Pair pairs[] = {
        {"1 vs 2", 1, 2},
        {"NaN vs 2", std::nan(""), 2},
        {"2 vs NaN", 2, std::nan("")},
        {"NaN vs NaN", std::nan(""), std::nan("")},
        {"inf vs 1", inf, 1}
    };

    for (const Pair& p : pairs) {
        std::cout << p.label << ": " << std::isunordered(p.x, p.y) << '\n';
    }
    return 0;
}

How It Works

  1. The loop prints true when the pair cannot be put in order, and false when it can.
  2. The three pairs with a NaN, in either position or both, are true. A NaN in any place makes the pair unordered.
  3. 1 vs 2 is a normal pair. inf vs 1 is also a normal pair, because infinity is bigger than every number, so both are false.

Example 2 — Compare Only When It Makes Sense

Check isunordered() first. Only compare the two numbers when both are real.

C++
#include <cmath>
#include <iostream>

struct Pair {
    const char* label;
    double x;
    double y;
};

int main() {
    Pair pairs[] = {
        {"3 vs 7", 3, 7},
        {"NaN vs 7", std::nan(""), 7},
        {"7 vs 3", 7, 3},
        {"4 vs 4", 4, 4}
    };

    for (const Pair& p : pairs) {
        std::cout << p.label << ": ";
        if (std::isunordered(p.x, p.y)) std::cout << "cannot compare";
        else if (p.x < p.y)             std::cout << "first is smaller";
        else if (p.x > p.y)             std::cout << "first is bigger";
        else                            std::cout << "equal";
        std::cout << '\n';
    }
    return 0;
}

How It Works

  1. The if chain asks the unordered question first. If a NaN is present, nothing else is tested.
  2. When both numbers are real, exactly one of the three remaining answers must be true: smaller, bigger, or equal.
  3. 3 vs 7 is smaller, 7 vs 3 is bigger, and 4 vs 4 is equal. The NaN pair is reported as "cannot compare" instead of a wrong answer.

Example 3 — isunordered(x, x) as a NaN Test

When both arguments are the same value, the pair is unordered only when the value is NaN. Use it to count missing values.

C++
#include <cmath>
#include <iostream>

int main() {
    double data[] = {1.5, std::nan(""), 2.5, std::nan(""), 4.0};
    int missing = 0;

    for (double v : data) {
        if (std::isunordered(v, v)) {
            ++missing;
        }
    }
    std::cout << "Missing values: " << missing << " of 5\n";
    return 0;
}

How It Works

  1. Each value is compared with itself. For a real number, a value is always "in order" with itself, so isunordered(v, v) is false.
  2. For a NaN, the pair is unordered, so the result is true. That happens twice in this array.
  3. The program prints 2 of 5. This is the same as std::isnan(v), which reads more clearly and is the better choice in real code.

Frequently Asked Questions

std::isunordered(x, y) returns true if x or y is NaN, because then the two values cannot be compared. It returns false when both are real numbers.
true if at least one of x and y is NaN, otherwise false.
When at least one of the two arguments is NaN. In that case x < y, x > y, and x == y are all false.
Yes, it gives the same answer in one call. The C++ standard provides it so that comparison code can deal with NaN in a quiet and clear way.
Include <cmath> and write std::isunordered(). That always works and picks the C++ overloads. The global name ::isunordered often works too, but the standard does not promise it, and it may only take a double.

Next: ldexp()

Keep going through the <cmath> functions. Multiply a number by a power of two.

ldexp() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful