Gists C++

Gists C++

Useful Code Snippets

Gists C++

Catch NA

Catch NA

Catch NA in C++ release mode.


#include <iostream>
#include <cmath>
#include <cstdlib>
void CheckForNaN(double value, const char* func, const char* file, int line) {
    if (std::isnan(value)) {
        std::cerr << "Encountered NaN value in function " << func
                  << " at " << file << ":" << line << std::endl;
        abort(); // Forces program to crash
    }
}
#define CHECK_FOR_NAN(value) CheckForNaN(value, __func__, __FILE__, __LINE__)
int main() {
    double x = std::nan(""); // Example NaN value
    CHECK_FOR_NAN(x); // This will cause the program to crash if x is NaN
    return 0;
}