In C++, std::fpclassify(x) sorts a decimal number into one of five groups. It does not tell you the value, only the kind of value. You compare the result with the constants FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, and FP_NORMAL.
The other classification functions, isnan(), isinf(), isfinite(), and isnormal(), each answer one yes/no question. fpclassify() answers all of them at once, so it is handy with a switch statement.
std::fpclassify() is declared in <cmath> and is part of the group Classification and comparison. These tools ask what kind of number you have: normal, zero, infinite, or NaN. In C++ they are ordinary overloaded functions in std, not macros.
Syntax
Include <cmath> and call std::fpclassify. Start with these lines.
C++
int kind = std::fpclassify(1.5); // FP_NORMAL
int zero = std::fpclassify(0.0); // FP_ZERO
int inf = std::fpclassify(1.0 / 0.0); // FP_INFINITE
Call
What it does
std::fpclassify(1.5)
An ordinary number. Returns FP_NORMAL.
std::fpclassify(0.0)
Both 0.0 and -0.0 return FP_ZERO.
std::fpclassify(std::nan(""))
Not a number. Returns FP_NAN.
The result is an int, and the numbers behind the FP_ constants are not fixed. Always compare with the names, never with 0, 1, or 2.
Library declarations
C++
int fpclassify(float x);
int fpclassify(double x);
int fpclassify(long double x);
int fpclassify(Integer x); // for integer types
Parameters
Parameter
Description
x
Required. The number to classify. It can be float, double, long double, or an integer type.
Return Value
Return Value
Description
FP_NAN
The value is not a number.
FP_INFINITE
The value is positive or negative infinity.
FP_ZERO
The value is +0.0 or -0.0.
FP_SUBNORMAL
A tiny number below the smallest normal value. It still works but has lost some precision.
FP_NORMAL
An ordinary finite number that is not zero.
C++ Examples
Example 1 — Name the Kind of Each Number
Write a small helper that turns the result of fpclassify() into a word, then test one value of each kind.
1.0 / 3.0 -> fine: 0.333333
tiny * 1e-20 -> lost precision
1.0 / zero -> overflow or divide by zero
0.0 / zero -> not a number
How It Works
report() classifies the result once, and prints a different message for each group. Normal numbers and zero fall into the else branch and are printed.
1.0 / 3.0 is a normal number, 0.333333. 1e-300 * 1e-20 would be 1e-320, which is below the normal range, so it is subnormal.
Dividing by zero gives infinity, and 0.0 / 0.0 has no meaningful answer, so it gives NaN. The classes tell these two apart.
Frequently Asked Questions
std::fpclassify(x) returns an int that tells which kind of floating-point value x is: FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, or FP_NORMAL.
One of the five constants FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, or FP_NORMAL, as an int.
FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, and FP_NORMAL. They are macros defined in <cmath>.
A very small number between zero and the smallest normal number. It can still be used, but it has fewer digits of precision than a normal number.
Include <cmath> and write std::fpclassify(). That always works and picks the C++ overloads. The global name ::fpclassify often works too, but the standard does not promise it, and it may only take a double.