Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does std::isnan() return false for double variable with NAN value

Tags:

c++

qt

I decided to use NAN(also tried std::numeric_limits::quiet_NaN()) as default value for argument of the function, but when i tried to check it using std::isnan, it returned false. In the same time value was printed to console using qDebug() and i saw nan.

Also i tried to check for NAN using x != x rule. It worked for NAN != NAN, but got false for x != x.

Last try was to define NAN double variable inside of the function and try to check it using both methods, but it lead to the same results.

I cant understand what is wrong.

Example:

double abc = NAN;
qDebug()<< abc << (abc != abc) << std::isnan(abc);

Output:

nan false false
like image 921
Maxim Skvortsov Avatar asked May 21 '18 11:05

Maxim Skvortsov


1 Answers

I didn't know that we are using -ffast-math in our project. That is what leads to the problem. Thank you for your attention and comments. There is no solution if you are using -ffast-math. You just need to look for another way to solve your problem (without NAN)

like image 174
Maxim Skvortsov Avatar answered Nov 14 '22 23:11

Maxim Skvortsov