C islessgreater() Macro

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

Definition

In C, islessgreater() is a <math.h> comparison macro. It answers a yes/no question: x and y are ordered and not equal (x is less than or greater than y). A true answer is a non-zero value (often printed as 1). A false answer is 0.

Unlike a raw x != (ordered) y compare, islessgreater() is designed so that NaN does not raise an invalid floating-point exception. If either value is NaN, the answer is false (except for special cases covered by related macros such as isunordered).

Pass two real floating values. Include <math.h>. Print the result with %d after casting to int if you want a clear 0 or 1 on the screen.

Syntax

Compare two floating-point values. Start with this call.

C
islessgreater(x, y);
CallWhat it does
islessgreater(5.0, 3.0)Returns non-zero because 5.0 and 3.0 are different ordered numbers.
islessgreater(2.0, 9.0)Returns 0 for equal numbers, and also 0 when NaN makes the pair unordered.
islessgreater(NAN, 1.0)With NaN involved, the result is 0 (false) without raising an invalid exception from this macro.

Include <math.h>. On GCC, link with -lm. Prefer this macro over x != (ordered) y when either side might be NaN and you do not want an invalid exception.

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

Parameters

ParameterDescription
xRequired. Left-hand floating value (float, double, or long double).
yRequired. Right-hand floating value to compare against.

Return Value

Return ValueDescription
Non-zero (true)When x and y are ordered and not equal (x is less than or greater than y).
0 (false)When the relation is not true, including many NaN cases.

C Examples

Example 1 — Basic Ordered Inequality

Print 1 when two ordered values differ, otherwise print 0.

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

int main(void) {
    printf("%d\n", (int)islessgreater(5.0, 3.0));
    printf("%d\n", (int)islessgreater(4.0, 4.0));
    printf("%d\n", (int)islessgreater(1.0, 9.0));
    return 0;
}

How It Works

  1. Include math.h. That provides islessgreater.
  2. Different values. 5.0 and 3.0 differ, so print 1.
  3. Equal values. Equal ordered numbers are not less-or-greater, so print 0.
  4. Another difference. 1.0 and 9.0 also print 1.

Example 2 — NaN Makes the Pair Unordered

When NaN appears, islessgreater returns 0.

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

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

How It Works

  1. Unordered means no ranking. NaN cannot be placed above or below a normal number.
  2. Macro returns false. Both calls print 0.
  3. Different from != alone. This macro also requires the values to be ordered.
  4. No invalid raise. The quiet NaN path is the reason these macros exist.

Example 3 — Detect a Real Change

Treat two readings as changed only when they are ordered and unequal.

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

int main(void) {
    double old_v = 10.0;
    double new_v = 10.5;

    if (islessgreater(old_v, new_v)) {
        printf("changed\n");
    } else {
        printf("same\n");
    }

    return 0;
}

How It Works

  1. Keep an old value. The previous reading is 10.0.
  2. Read a new value. The new reading is 10.5.
  3. Ask islessgreater. The values are ordered and different.
  4. Report change. The program prints changed.

Frequently Asked Questions

islessgreater() is a math.h comparison macro. It checks whether x and y are ordered and not equal (x is less than or greater than y).
x and y are required floating values. They are the two numbers you want to compare.
A non-zero int-like result for true, or 0 for false. Beginners often print it as 0 or 1.
Include <math.h>. On GCC, link with -lm.
The macro is quieter with NaN: it does not raise an invalid floating-point exception the way a plain relational operator can. Use related macros like isunordered / isgreater / isless when you need the other relation.

Next: isnan()

Test whether a floating value is Not-a-Number (NaN).

isnan() 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