double num = 0.0/0.0; // create NaN value if (std::isnan(num)) { std::cout << "The value is NaN." << std::endl; }
#include... double num = acos(-1.5); // compute arccos of a value out of range if (std::isnan(num)) { std::cout << "The value is NaN." << std::endl; }
#includeThis code creates a vector of double values, including a NaN value. It then uses the std::find_if algorithm with std::isnan as the predicate to find the position of the NaN value in the vector. The std::isnan function is part of the C++ standard library, which is included as part of the standard C++ distribution. Therefore, it does not require any additional package or library to be installed.#include ... std::vector values {1.0, 2.0, 3.0, 0.0/0.0, 4.0, 5.0}; auto num_it = std::find_if(values.begin(), values.end(), std::isnan); if (num_it != values.end()) { std::cout << "Found a NaN value at position " << std::distance(values.begin(), num_it) << std::endl; }