Last active
October 27, 2019 21:56
-
-
Save wolf99/41ca7ea28898c670889b2c6c496e7f35 to your computer and use it in GitHub Desktop.
Portable alternative to isnan() in math.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int my_isnan(double x) | |
{ | |
volatile double tmp = x; /* Prevent optimization by compiler. */ | |
return tmp != x; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NaN is the only "number" that is not equal to any other number, including itself. A simple test like
return (x != x);
would be optimised out by most compilers, thus the need for the addition of a volatile variable. Compilers do not always implement this aspect of doubles, so testing of this function's operation is required.