C++ std::isnan() Function

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

Definition

In C++, std::isnan(x) checks whether a value is NaN, which stands for "not a number". NaN is what you get from calculations with no real answer, such as std::sqrt(-1.0), 0.0 / 0.0, or infinity - infinity.

NaN is strange: it is not equal to anything, not even to itself. So x == NaN never works, and isnan(x) is the right way to test. It is also a good way to print a friendly message instead of the confusing text nan or -nan.

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

C++
bool a = std::isnan(1.5);              // false
bool b = std::isnan(std::sqrt(-1.0));   // true
bool c = std::isnan(std::nan(""));      // true
CallWhat it does
std::isnan(1.5)An ordinary number. Returns false.
std::isnan(std::sqrt(-1.0))The square root of a negative number is NaN. Returns true.
std::isnan(std::nan(""))A NaN made on purpose. Returns true.

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 isnan(float x);
bool isnan(double x);
bool isnan(long double x);
bool isnan(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 NaN.
falsex is any other value, including infinity and zero.

C++ Examples

Example 1 — Calculations That Give NaN

Test a normal number and four ways to produce NaN. All five answers are printed as words.

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

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

    std::cout << "1.0:          " << std::isnan(1.0) << '\n';
    std::cout << "nan(\"\"):      " << std::isnan(std::nan("")) << '\n';
    std::cout << "sqrt(-1):     " << std::isnan(std::sqrt(-1.0)) << '\n';
    std::cout << "inf - inf:    " << std::isnan(inf - inf) << '\n';
    std::cout << "0 * inf:      " << std::isnan(0.0 * inf) << '\n';
    return 0;
}

How It Works

  1. The ordinary number 1.0 is not NaN, so the first line is false.
  2. std::nan("") creates a NaN on purpose. sqrt(-1) has no real answer, so it also gives NaN.
  3. The math rules for infinity say inf - inf and 0 * inf cannot be decided, so both are NaN. All four lines print true.

Example 2 — NaN Is Not Equal to Itself

Compare a NaN with itself in three ways, and see why isnan() is the safe test.

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

int main() {
    std::cout << std::boolalpha;
    double x = std::nan("");

    std::cout << "x == x:    " << (x == x) << '\n';
    std::cout << "x != x:    " << (x != x) << '\n';
    std::cout << "isnan(x):  " << std::isnan(x) << '\n';
    return 0;
}

How It Works

  1. The variable x holds a NaN, so the three lines test the same value in different ways.
  2. The test x == x is false, even though it is the same variable. NaN is not equal to anything, including itself. That is why x != x is true.
  3. The trick x != x works, but std::isnan(x) says the same thing clearly, so it is the one to use.

Example 3 — Print a Message Instead of NaN

Take the square root of several numbers. If the answer is NaN, print a clear message instead.

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

int main() {
    double inputs[] = {16.0, -4.0, 25.0};

    for (double x : inputs) {
        double r = std::sqrt(x);
        if (std::isnan(r)) {
            std::cout << "sqrt(" << x << ") is not a real number\n";
        } else {
            std::cout << "sqrt(" << x << ") = " << r << '\n';
        }
    }
    return 0;
}

How It Works

  1. The loop calculates the square root first and checks the answer with isnan() before printing.
  2. sqrt(16) and sqrt(25) are real numbers (4 and 5), so the else branch prints them.
  3. sqrt(-4) has no real answer, so it returns NaN. The if branch runs and the user sees a clear message instead of -nan.

Frequently Asked Questions

std::isnan(x) returns true if x is NaN (not a number), and false for every other value, including infinity.
true if x is NaN, otherwise false. Infinity and zero return false.
NaN is not equal to any value, including another NaN. Every comparison with it gives false, except !=. Use std::isnan(x) instead.
No. Infinity is a valid special value, so isnan(inf) is false. Use std::isinf() for infinity, or std::isfinite() to rule out both.
Include <cmath> and write std::isnan(). That always works and picks the C++ overloads. The global name ::isnan often works too, but the standard does not promise it, and it may only take a double.

Next: isnormal()

Keep going through the <cmath> functions. Checks that a number is normal: not zero, subnormal, inf, or NaN.

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

7 people found this page helpful