C++ std::nan() Function

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

Definition

In C++, std::nan(tag) creates a NaN value on purpose. NaN means "not a number". It is often used as a marker for a missing value, such as a sensor that did not send a reading.

The parameter is a text string called the tag. It can add a small piece of extra information to the NaN, but how it is used depends on the system, so most programs pass an empty string, std::nan(""). The float and long double versions are std::nanf() and std::nanl().

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

C++
double a = std::nan("");     // a double NaN
float  b = std::nanf("");    // a float NaN
long double c = std::nanl("");   // a long double NaN
CallWhat it does
std::nan("")Returns a double NaN. The usual form with an empty tag.
std::nanf("")Returns a float NaN.
std::nanl("")Returns a long double NaN.

Unlike most <cmath> functions, the type is in the name: nan, nanf, nanl. There are no overloads. You can also use std::numeric_limits<double>::quiet_NaN().

Library declarations
C++
double      nan(const char* tagp);
float       nanf(const char* tagp);
long double nanl(const char* tagp);

Parameters

ParameterDescription
tagpRequired. A C string that describes the NaN. Pass an empty string "" for a plain NaN. Other values are implementation defined.

Return Value

Return ValueDescription
Quiet NaNA quiet NaN of the type that matches the function: double, float, or long double.
0 on failureIf the library cannot make a NaN, the standard says it may return 0. This does not happen on common systems.

C++ Examples

Example 1 — Create a NaN in Each Type

Make a NaN with nan(), nanf(), and nanl(), and check each one with isnan().

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

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

    double d = std::nan("");
    float f = std::nanf("");
    long double l = std::nanl("");

    std::cout << "double NaN:      " << std::isnan(d) << '\n';
    std::cout << "float NaN:       " << std::isnan(f) << '\n';
    std::cout << "long double NaN: " << std::isnan(l) << '\n';
    return 0;
}

How It Works

  1. The three functions have the same job and differ only in the type they return. Each variable gets the matching type.
  2. The empty string "" asks for a plain NaN. There is no number to pass, because NaN is not a number.
  3. isnan() has overloads for all three types, so each check prints true.

Example 2 — How a NaN Behaves in Comparisons

Create one NaN, and see that every comparison with it is false, except !=.

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 << "x < 1:  " << (x < 1.0) << '\n';
    std::cout << "x > 1:  " << (x > 1.0) << '\n';
    std::cout << "isnan:  " << std::isnan(x) << '\n';
    return 0;
}

How It Works

  1. The value in x comes from std::nan(""), so it is not a number.
  2. The tests ==, <, and > are all false, as NaN has no order. Only != gives true.
  3. The only reliable way to detect it is std::isnan(x), which prints true.

Example 3 — Use NaN as a Missing Value

Five temperature readings, two of them missing. Create the missing ones with nan() and average only the real ones.

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

int main() {
    double readings[] = {20.5, std::nan(""), 22.5, std::nan(""), 24.0};
    double sum = 0.0;
    int count = 0;

    for (double r : readings) {
        if (!std::isnan(r)) {
            sum += r;
            ++count;
        }
    }
    std::cout << "Real readings: " << count << '\n';
    std::cout << "Average: " << sum / count << '\n';
    return 0;
}

How It Works

  1. The array uses std::nan("") as a "no reading" marker, which is better than a fake number like -999.
  2. The loop skips each NaN with !std::isnan(r) and adds the other three: 20.5 + 22.5 + 24 = 67.
  3. The average is 67 / 3, which prints as 22.3333 with the default six digits.

Frequently Asked Questions

std::nan(tagp) returns a quiet NaN (not a number) as a double. The tag string is usually empty, as in std::nan("").
A quiet NaN. std::nan() returns a double, std::nanf() returns a float, and std::nanl() returns a long double.
Both give a quiet NaN. quiet_NaN() is the modern C++ way and needs <limits>. nan("") comes from <cmath> and also accepts a tag string.
It is a string that can carry extra information in the NaN. What it means depends on the system, so beginners should pass an empty string.
Include <cmath> and write std::nan(). That always works and picks the C++ overloads. The global name ::nan often works too, but the standard does not promise it, and it may only take a double.

Next: nearbyint()

Keep going through the <cmath> functions. Round using the current rounding mode.

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

13 people found this page helpful