How to check if a double is NaN in C++

2 Answers

0 votes
#include <iostream>
#include <cmath> // For std::isnan

int main() {
    double d = std::nan(""); // Example NaN value

    if (std::isnan(d)) {
        std::cout << "The value is NaN." << std::endl;
    } else {
        std::cout << "The value is not NaN." << std::endl;
    }
}

 
 
/*
run:
 
The value is NaN.
 
*/
   

 



answered May 11, 2025 by avibootz
0 votes
#include <iostream>
#include <cmath> // For std::isnan

int main() {
    double d = std::nan(""); // Example NaN value

    if (d != d) {
        std::cout << "The value is NaN." << std::endl;
    } else {
        std::cout << "The value is not NaN." << std::endl;
    }
}

 
 
/*
run:
 
The value is NaN.
 
*/

 



answered May 11, 2025 by avibootz
...