Skip to content
Tutorials Center
My learning

Topics

C Reference ยท ctype.h, math.h and others

C isnan(), isinf(), isfinite() and fpclassify()

Reference for the math.h classification macros: isnan, isinf, isfinite, isnormal, signbit and fpclassify, with NaN pitfalls and safe checking patterns.

The classification macros in <math.h> tell you what kind of value a floating-point number holds. A double can be a normal number, a subnormal (very tiny) number, zero, infinity, or NaN ("not a number"). Ordinary comparisons cannot detect NaN reliably, so you use isnan() and friends.

These are type-generic macros: they accept float, double and long double and return an int. A nonzero result means "yes". fpclassify() returns exactly one of five category constants, which makes it handy in a switch.

C
#include <math.h>

if (isnan(x)) { /* x is not a number */ }
if (!isfinite(x)) { /* NaN or +-infinity */ }

Try it

C Classifying special values

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C NaN comparisons and a safe average

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
int fpclassify(real-floating x);
int isfinite(real-floating x);
int isinf(real-floating x);
int isnan(real-floating x);
int isnormal(real-floating x);
int signbit(real-floating x);
Name Description
x A float, double or long double value. The macro evaluates it once.

Returns: fpclassify() returns one of the constants below. The other macros return nonzero if the property holds and 0 otherwise. Do not compare the result with 1; test it for truth.

Constant Meaning
FP_NAN Not a number.
FP_INFINITE Positive or negative infinity.
FP_ZERO Positive or negative zero.
FP_SUBNORMAL Nonzero but smaller than the smallest normal value; reduced precision.
FP_NORMAL An ordinary finite nonzero number.

Errors: none. These macros never set errno and never raise floating-point exceptions, even for NaN.

Usage notes

  • NaN is never equal to anything, including itself. x == NAN is always false, and x != x is true only for NaN. Always use isnan().
  • Every ordered comparison with NaN is false. x < y, x > y, x <= y and x >= y all fail, so a sort or a "find the maximum" loop can silently misbehave when a NaN is present. isunordered(x, y) tests for that case directly.
  • How NaN appears: 0.0 / 0.0, INFINITY - INFINITY, sqrt(-1.0), log(-1.0), strtod("nan", NULL) and the NAN macro. Once NaN enters an expression it usually spreads to the result.
  • Avoid printing NaN in tests. printf("%f", x) prints nan or -nan depending on the sign bit, which differs between machines, and NaN payloads are not portable. Print the result of isnan() instead.
  • isfinite() is the one-step guard for "a real, usable number": it rejects NaN and both infinities.
  • isinf() does not promise a sign. Combine it with a comparison: isinf(x) && x < 0 for negative infinity.
  • signbit() works on zero and NaN too, so it is the only way to tell -0.0 from 0.0.
  • -ffast-math and -ffinite-math-only break these checks: the compiler may assume NaN and infinity never occur and delete your isnan() tests.
  • Subnormal numbers (FP_SUBNORMAL) are valid but slow on some CPUs and carry fewer significant digits.
  • Standard version: all six macros arrived in C99. C23 adds issubnormal() and iszero(), which the runner's C library does not provide yet, so use fpclassify() for those tests.