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)
Call
What 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.
multiply() calculates the product first and tests the product with isfinite(). It never tests the inputs.
1e100 * 1e100 is 1e200, which fits in a double (the limit is about 1.8e308), so it prints normally.
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;
}
Output
Used 3 values, average 6
How It Works
The loop looks at five numbers. Only the ones that pass isfinite() are added to sum and counted in used.
The infinity and the NaN fail the test, so only 4, 6, and 8 are used. Their sum is 18.
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.