Skip to content

Instantly share code, notes, and snippets.

@wolf99
Last active October 27, 2019 21:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolf99/41ca7ea28898c670889b2c6c496e7f35 to your computer and use it in GitHub Desktop.
Save wolf99/41ca7ea28898c670889b2c6c496e7f35 to your computer and use it in GitHub Desktop.
Portable alternative to isnan() in math.h
int my_isnan(double x)
{
volatile double tmp = x; /* Prevent optimization by compiler. */
return tmp != x;
}
@wolf99
Copy link
Author

wolf99 commented Feb 2, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment