I didn't know until now that if (a != a) returns true only when a is NaN.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float a = sqrt(-1); // nan
if (a != a) {
cout << "It's NaN!" << endl;
cout << a << endl; // prints 'nan'
}
else {
cout << "It's OK!" << endl;
}
return 0;
}
No comments:
Post a Comment