C++ std::isinf() Function

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

Definition

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

Library declarations
C++
bool isinf(float x);
bool isinf(double x);
bool isinf(long double x);
bool isinf(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 false.

Return Value

Return ValueDescription
truex is positive infinity or negative infinity.
falsex is any other value, including NaN.

C++ Examples

Example 1 — Test Several Values

Check a big number, both infinities, NaN, and a division by zero.

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

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

    std::cout << "1e308:      " << std::isinf(1e308) << '\n';
    std::cout << "+infinity:  " << std::isinf(inf) << '\n';
    std::cout << "-infinity:  " << std::isinf(-inf) << '\n';
    std::cout << "NaN:        " << std::isinf(std::nan("")) << '\n';
    std::cout << "1.0 / zero: " << std::isinf(1.0 / zero) << '\n';
    return 0;
}

How It Works

  1. The first line is a very large number, but it is still inside the range of a double, so the answer is false.
  2. Both +infinity and -infinity give true. isinf() does not care about the sign.
  3. NaN is a different special value, so it gives false. Dividing by a volatile zero gives infinity at run time, so the last line is true.

Example 2 — Where exp() Overflows

The function std::exp() grows very fast. Find the place where the answer no longer fits in a double.

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

int main() {
    std::cout << std::boolalpha;

    double a = std::exp(709.0);
    double b = std::exp(710.0);

    std::cout << "exp(709) = " << a << ", infinite: " << std::isinf(a) << '\n';
    std::cout << "exp(710) = " << b << ", infinite: " << std::isinf(b) << '\n';
    return 0;
}

How It Works

  1. The largest double is about 1.8e308. exp(709) is 8.21841e+307, which is just under the limit.
  2. exp(710) would be about 2.2e308, which is above the limit. The library returns infinity instead, and cout prints it as inf.
  3. 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;
}

How It Works

  1. The loop keeps going while x is not infinity. Each pass multiplies x by 2 and counts one more doubling.
  2. After n doublings x equals 2^n. The biggest power of two a double can store is 2^1023.
  3. 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.

Next: isless()

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

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

12 people found this page helpful