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.
#include <math.h> if (isnan(x)) { /* x is not a number */ } if (!isfinite(x)) { /* NaN or +-infinity */ }
Try it
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.
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
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 == NANis always false, andx != xis true only for NaN. Always useisnan(). - Every ordered comparison with NaN is false.
x < y,x > y,x <= yandx >= yall 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 theNANmacro. Once NaN enters an expression it usually spreads to the result. - Avoid printing NaN in tests.
printf("%f", x)printsnanor-nandepending on the sign bit, which differs between machines, and NaN payloads are not portable. Print the result ofisnan()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 < 0for negative infinity.signbit()works on zero and NaN too, so it is the only way to tell-0.0from0.0.-ffast-mathand-ffinite-math-onlybreak these checks: the compiler may assume NaN and infinity never occur and delete yourisnan()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()andiszero(), which the runner's C library does not provide yet, so usefpclassify()for those tests.