In C++, std::isinf(x) checks whether a decimal number has become infinity. Floating-point math does not stop when a result is too large. The value simply becomes inf, and isinf() lets you notice it.
It returns true for both +inf and -inf. To know which one it is, look at the sign with x > 0 or std::signbit(). NaN is not infinity, so isinf(NaN) is false.
std::isinf() 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::isinf. Start with these lines.
C++
bool a = std::isinf(1e308); // false
bool b = std::isinf(std::exp(710.0)); // true (overflow)
bool c = std::isinf(-std::numeric_limits<double>::infinity()); // true
Call
What it does
std::isinf(1e308)
A very big but finite number. Returns false.
std::isinf(std::exp(710.0))
exp(710) is too big and becomes infinity. Returns true.
std::isinf(std::nan(""))
NaN is not infinity. Returns false.
Write std:: in front, and include <cmath>. The function returns a bool, so use std::boolalpha to print true or false.
The largest double is about 1.8e308. exp(709) is 8.21841e+307, which is just under the limit.
exp(710) would be about 2.2e308, which is above the limit. The library returns infinity instead, and cout prints it as inf.
So isinf() says false for the first value and true for the second. The border between them is the overflow point.
Example 3 — Double a Number Until It Overflows
Start at 1 and double the value each time. Count how many doublings the loop needs to reach infinity.
C++
#include <cmath>
#include <iostream>
int main() {
double x = 1.0;
int count = 0;
while (!std::isinf(x)) {
x *= 2;
++count;
}
std::cout << "Doubled " << count << " times before infinity\n";
return 0;
}
Output
Doubled 1024 times before infinity
How It Works
The loop keeps going while x is not infinity. Each pass multiplies x by 2 and counts one more doubling.
After n doublings x equals 2^n. The biggest power of two a double can store is 2^1023.
The 1024th doubling gives 2^1024, which is too big. The result becomes infinity, the loop ends, and the program prints 1024.
Frequently Asked Questions
std::isinf(x) returns true if x is positive infinity or negative infinity, and false for every other value, including NaN.
true if x is +infinity or -infinity, otherwise false. NaN gives false.
Yes. It checks for any infinity. Use x > 0 or std::signbit(x) afterward if you need to know the sign.
Use std::numeric_limits<double>::infinity() from <limits>. Dividing a number by zero at run time, or overflowing a calculation, also gives infinity.
Include <cmath> and write std::isinf(). That always works and picks the C++ overloads. The global name ::isinf often works too, but the standard does not promise it, and it may only take a double.