C++ std::isfinite() Function

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

Definition

In C++, std::isfinite(x) answers one question: is this a real number I can use? It returns true for ordinary numbers, for zero, and for tiny subnormal numbers. It returns false for infinity and for NaN.

It is the quickest way to check a result after a calculation that could overflow or divide by zero. One check with isfinite() covers both infinity and NaN, so you do not need to call isinf() and isnan() separately.

std::isfinite() 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::isfinite. Start with these lines.

C++
bool a = std::isfinite(3.14);     // true
bool b = std::isfinite(0.0);      // true
bool c = std::isfinite(1.0 / 0.0); // false (infinity)
CallWhat it does
std::isfinite(3.14)An ordinary number. Returns true.
std::isfinite(0.0)Zero counts as finite. Returns true.
std::isfinite(std::nan(""))NaN is not finite. Returns false.

Write std:: in front, and include <cmath>. The function returns a bool, so use std::boolalpha to print true or false.

Library declarations
C++
bool isfinite(float x);
bool isfinite(double x);
bool isfinite(long double x);
bool isfinite(Integer x);   // for integer types

Parameters

ParameterDescription
xRequired. The number to test. It can be float, double, long double, or an integer type. Integers always give true.

Return Value

Return ValueDescription
truex is zero, subnormal, or a normal number.
falsex is positive infinity, negative infinity, or NaN.

C++ Examples

Example 1 — Test Several Values

Try ordinary numbers, a huge number, zero, infinity, and NaN, and print the answers as words.

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

int main() {
    std::cout << std::boolalpha;
    double inf = std::numeric_limits<double>::infinity();

    std::cout << "3.14:      " << std::isfinite(3.14) << '\n';
    std::cout << "0.0:       " << std::isfinite(0.0) << '\n';
    std::cout << "-1e308:    " << std::isfinite(-1e308) << '\n';
    std::cout << "infinity:  " << std::isfinite(inf) << '\n';
    std::cout << "-infinity: " << std::isfinite(-inf) << '\n';
    std::cout << "NaN:       " << std::isfinite(std::nan("")) << '\n';
    return 0;
}

How It Works

  1. std::boolalpha makes cout print true and false instead of 1 and 0.
  2. 3.14, 0.0, and -1e308 are all real numbers, so each one gives true. A very large value is still finite.
  3. Positive infinity, negative infinity, and NaN are not finite, so those three lines print false.

Example 2 — Detect an Overflow After Multiplying

Multiply two numbers and check the product. 1e200 * 1e200 is too big for a double.

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

void multiply(double a, double b) {
    double product = a * b;
    if (std::isfinite(product)) {
        std::cout << a << " * " << b << " = " << product << '\n';
    } else {
        std::cout << a << " * " << b << " overflowed\n";
    }
}

int main() {
    multiply(1e100, 1e100);
    multiply(1e200, 1e200);
    return 0;
}

How It Works

  1. multiply() calculates the product first and tests the product with isfinite(). It never tests the inputs.
  2. 1e100 * 1e100 is 1e200, which fits in a double (the limit is about 1.8e308), so it prints normally.
  3. 1e200 * 1e200 would be 1e400. That is too big, so the result becomes infinity, isfinite() returns false, and the second branch runs.

Example 3 — Average Only the Good Values

A list contains infinity and NaN by mistake. Skip them with isfinite() and average the rest.

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

int main() {
    double inf = std::numeric_limits<double>::infinity();
    double data[] = {4.0, inf, 6.0, std::nan(""), 8.0};
    double sum = 0.0;
    int used = 0;

    for (double v : data) {
        if (std::isfinite(v)) {
            sum += v;
            ++used;
        }
    }
    std::cout << "Used " << used << " values, average " << sum / used << '\n';
    return 0;
}

How It Works

  1. The loop looks at five numbers. Only the ones that pass isfinite() are added to sum and counted in used.
  2. The infinity and the NaN fail the test, so only 4, 6, and 8 are used. Their sum is 18.
  3. The average is 18 / 3 = 6. Without the check, one bad value would turn the whole sum into infinity or NaN.

Frequently Asked Questions

std::isfinite(x) returns true if x is a real, finite number. It returns false if x is infinity or NaN.
true if x is zero, subnormal, or normal. false if x is positive or negative infinity, or NaN.
Yes. Zero, negative zero, subnormal numbers, and normal numbers are all finite. Only infinity and NaN return false.
isfinite() is the shortest check when you only want to know if a result is usable. Use isnan() or isinf() when you need to know which problem you have.
Include <cmath> and write std::isfinite(). That always works and picks the C++ overloads. The global name ::isfinite often works too, but the standard does not promise it, and it may only take a double.

Next: isgreater()

Keep going through the <cmath> functions. Checks x > y safely, false if either is NaN.

isgreater() 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.

9 people found this page helpful