In C, fpclassify() is a classification helper from <math.h>. You pass one floating-point value. It returns a category code that says what kind of number you have — for example a normal number, zero, infinity, NaN, or a tiny subnormal value.
Think of it as asking: “What is this float?” instead of doing many separate tests. The common category macros are FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, and FP_NORMAL. It is usually implemented as a macro (or macro-like function) that works with float, double, or long double.
Syntax
Pass one floating-point value. Start with this call.
C
fpclassify(3.14);
Call
What it does
fpclassify(3.14)
Returns FP_NORMAL for an ordinary finite non-zero value.
fpclassify(0.0)
Returns FP_ZERO.
fpclassify(INFINITY)
Returns FP_INFINITE.
Include <math.h>. On GCC, link with -lm when you also use other math functions. Compare the result with FP_* macros in a switch or if.
Library declarations
C
int fpclassify(real-floating x);
Parameters
Parameter
Description
x
Required. Any real floating type (float, double, or long double).
Return Value
Return Value
Description
FP_NAN
x is Not-a-Number.
FP_INFINITE
x is +infinity or -infinity.
FP_ZERO
x is +0 or -0.
FP_SUBNORMAL
x is a tiny non-zero value below the normal range.
FP_NORMAL
x is a normal finite non-zero number.
C Examples
Example 1 — Label Ordinary Values
Print a short name for each category.
C
#include <stdio.h>
#include <math.h>
static const char *kind(double x) {
switch (fpclassify(x)) {
case FP_NAN: return "nan";
case FP_INFINITE: return "inf";
case FP_ZERO: return "zero";
case FP_SUBNORMAL: return "subnormal";
case FP_NORMAL: return "normal";
default: return "other";
}
}
int main(void) {
printf("%s\n", kind(3.14));
printf("%s\n", kind(0.0));
printf("%s\n", kind(INFINITY));
return 0;
}
Output
normal
zero
inf
How It Works
Include math.h. Brings in fpclassify and the FP_* macros.
Switch on result. Map each category to a short string.
Normal number.3.14 is FP_NORMAL.
Zero and infinity.0.0 is FP_ZERO; INFINITY is FP_INFINITE.
Example 2 — Catch NaN with FP_NAN
Invalid floating results often become NaN.
C
#include <stdio.h>
#include <math.h>
int main(void) {
double bad = 0.0 / 0.0;
if (fpclassify(bad) == FP_NAN) {
printf("got NaN\n");
} else {
printf("not NaN\n");
}
return 0;
}
Output
got NaN
How It Works
Make a NaN.0.0 / 0.0 is undefined and yields NaN.
Classify.fpclassify(bad) returns FP_NAN.
Branch. Handle the error path before more math.
Related helpers.isnan() is a shorter yes/no check for the same idea.
Example 3 — Guard Before Using a Value
Only compute further when the value is a normal number.
C
#include <stdio.h>
#include <math.h>
int main(void) {
double x = 4.0;
if (fpclassify(x) == FP_NORMAL) {
printf("sqrt = %.1f\n", sqrt(x));
} else {
printf("skip\n");
}
return 0;
}
Output
sqrt = 2.0
How It Works
Check first. Confirm x is FP_NORMAL.
Then compute. Call sqrt only on a safe value.
Other cases. Zero, NaN, or infinity can take a different path.
Why one call.fpclassify covers many cases in a single test.
Frequently Asked Questions
It returns a category for a floating-point value such as FP_NORMAL, FP_ZERO, FP_INFINITE, FP_NAN, or FP_SUBNORMAL.
x is required. It can be float, double, or long double.
An int category code. Compare it with the FP_* macros from math.h.
Include <math.h>.
It is provided as a classification macro/function in math.h. Use it like a function call; the library may implement it as a macro.