C isunordered() Macro

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

Definition

In C, isunordered() is a <math.h> comparison macro. It tells you if two floating values cannot be ordered. That almost always means at least one of them is NaN. True is non-zero; false is 0.

When a pair is unordered, relations such as greater, less, or equal do not hold in the usual way. The quiet compare macros return false in those cases and avoid raising an invalid exception.

Pass two real floating arguments. Include <math.h>. Print the result as 0 or 1. Use this check before you trust a ranking between two readings.

Syntax

Ask whether two values cannot be ordered. Start with this call.

C
isunordered(x, y);
CallWhat it does
isunordered(NAN, 1.0)Returns non-zero because NaN makes the pair unordered.
isunordered(2.0, 3.0)Returns 0 because both values are ordered numbers.
isunordered(NAN, NAN)Returns non-zero. Two NaNs are still unordered.

Include <math.h>. On GCC, link with -lm. If isunordered is true, macros such as isgreater and isless will return false.

Library declarations
C
#define isunordered(x, y) /* implementation-defined macro */

Parameters

ParameterDescription
xRequired. First floating value.
yRequired. Second floating value.

Return Value

Return ValueDescription
Non-zero (true)When the arguments cannot be ordered (usually because of NaN).
0 (false)When both values are ordered numbers.

C Examples

Example 1 — Basic Unordered Check

Print 1 when NaN makes a pair unordered, otherwise print 0.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    printf("%d\n", (int)isunordered(NAN, 1.0));
    printf("%d\n", (int)isunordered(2.0, 3.0));
    printf("%d\n", (int)isunordered(4.0, 4.0));
    return 0;
}

How It Works

  1. Include math.h. That provides isunordered and NAN.
  2. NaN pair. The first line prints 1.
  3. Different normals. Ordered numbers print 0.
  4. Equal normals. Equal ordered values are still ordered, so print 0.

Example 2 — Gate a Compare Behind a Check

Only rank two values when they are ordered.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double a = 5.0;
    double b = NAN;

    if (isunordered(a, b)) {
        printf("cannot rank\n");
    } else if (isgreater(a, b)) {
        printf("a wins\n");
    } else {
        printf("b wins or tie\n");
    }

    return 0;
}

How It Works

  1. Prepare two values. One is normal and one is NaN.
  2. Ask isunordered first. The pair cannot be ranked.
  3. Skip greater/less. You avoid pretending NaN has an order.
  4. Print a clear message. The program reports cannot rank.

Example 3 — Two NaNs Are Still Unordered

Even NaN against NaN fails ordering.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    printf("%d\n", (int)isunordered(NAN, NAN));
    printf("%d\n", (int)islessgreater(NAN, NAN));
    return 0;
}

How It Works

  1. Compare two NaNs. They cannot be ordered.
  2. isunordered is true. The first line prints 1.
  3. islessgreater is false. The second line prints 0.
  4. Use both ideas. Unordered explains why inequality macros also fail.

Frequently Asked Questions

isunordered() returns non-zero when two floating values cannot be ordered, usually because at least one is NaN.
x and y are required floating values. They are the pair you want to test.
A non-zero value when the pair is unordered, otherwise 0.
Include <math.h>. On GCC, link with -lm.
If isunordered is true, isgreater and friends return false and avoid raising an invalid exception on that NaN compare path.

Next: ldexp()

Scale a floating value by a power of two.

ldexp() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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