C++ std::fpclassify() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

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
CallWhat 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

ParameterDescription
xRequired. The number to classify. It can be float, double, long double, or an integer type.

Return Value

Return ValueDescription
FP_NANThe value is not a number.
FP_INFINITEThe value is positive or negative infinity.
FP_ZEROThe value is +0.0 or -0.0.
FP_SUBNORMALA tiny number below the smallest normal value. It still works but has lost some precision.
FP_NORMALAn 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.

C++
#include <cmath>
#include <iostream>
#include <limits>

const char* kind(double x) {
    switch (std::fpclassify(x)) {
        case FP_NAN:       return "NaN";
        case FP_INFINITE:  return "infinite";
        case FP_ZERO:      return "zero";
        case FP_SUBNORMAL: return "subnormal";
        case FP_NORMAL:    return "normal";
    }
    return "unknown";
}

int main() {
    struct Item { const char* label; double value; };
    Item items[] = {
        {"1.5", 1.5},
        {"-0.0", -0.0},
        {"infinity", std::numeric_limits<double>::infinity()},
        {"NaN", std::nan("")},
        {"denorm_min", std::numeric_limits<double>::denorm_min()}
    };

    for (const Item& it : items) {
        std::cout << it.label << " -> " << kind(it.value) << '\n';
    }
    return 0;
}

How It Works

  1. The switch in kind() has one case for each of the five FP_ constants, and returns a word for it.
  2. 1.5 is an ordinary number, so it is normal. Negative zero is still zero, because fpclassify() ignores the sign.
  3. The smallest positive double, denorm_min(), is below the normal range, so it is subnormal. Infinity and NaN have their own groups.

Example 2 — Count Values by Kind

Go through an array of readings and count how many fall in each group. This is a quick data-quality check.

C++
#include <cmath>
#include <iostream>
#include <limits>

int main() {
    double inf = std::numeric_limits<double>::infinity();
    double values[] = {2.5, 0.0, std::nan(""), 1e-310, inf, -7.0, std::nan("")};
    int normal = 0, zero = 0, subnormal = 0, infinite = 0, nan = 0;

    for (double v : values) {
        int c = std::fpclassify(v);
        if (c == FP_NORMAL) ++normal;
        else if (c == FP_ZERO) ++zero;
        else if (c == FP_SUBNORMAL) ++subnormal;
        else if (c == FP_INFINITE) ++infinite;
        else if (c == FP_NAN) ++nan;
    }
    std::cout << "normal: " << normal << '\n';
    std::cout << "zero: " << zero << '\n';
    std::cout << "subnormal: " << subnormal << '\n';
    std::cout << "infinite: " << infinite << '\n';
    std::cout << "NaN: " << nan << '\n';
    return 0;
}

How It Works

  1. The array has seven values. Each one is passed to fpclassify() once, and the result is compared with the FP_ constants.
  2. The values 2.5 and -7.0 are normal. 0.0 is zero, and 1e-310 is smaller than the smallest normal double (about 2.2e-308), so it is subnormal.
  3. There is one infinity and two NaNs. The five counters add up to 7, which is the size of the array.

Example 3 — Check the Result of a Calculation

After a risky calculation, use fpclassify() to find out what went wrong. The variables are volatile so the compiler cannot work out the answers early.

C++
#include <cmath>
#include <iostream>

void report(const char* label, double r) {
    int c = std::fpclassify(r);
    std::cout << label << " -> ";
    if (c == FP_NAN)            std::cout << "not a number";
    else if (c == FP_INFINITE)  std::cout << "overflow or divide by zero";
    else if (c == FP_SUBNORMAL) std::cout << "lost precision";
    else                        std::cout << "fine: " << r;
    std::cout << '\n';
}

int main() {
    volatile double zero = 0.0;
    volatile double tiny = 1e-300;

    report("1.0 / 3.0", 1.0 / 3.0);
    report("tiny * 1e-20", tiny * 1e-20);
    report("1.0 / zero", 1.0 / zero);
    report("0.0 / zero", 0.0 / zero);
    return 0;
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Next: frexp()

Keep going through the <cmath> functions. Split a number into a mantissa and a power of two.

frexp() tutorial →

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.

14 people found this page helpful