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
Call
What 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().
x == x: false
x != x: true
x < 1: false
x > 1: false
isnan: true
How It Works
The value in x comes from std::nan(""), so it is not a number.
The tests ==, <, and > are all false, as NaN has no order. Only != gives true.
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;
}
Output
Real readings: 3
Average: 22.3333
How It Works
The array uses std::nan("") as a "no reading" marker, which is better than a fake number like -999.
The loop skips each NaN with !std::isnan(r) and adds the other three: 20.5 + 22.5 + 24 = 67.
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.