Skip to content

Instantly share code, notes, and snippets.

@wolf99
Last active October 27, 2019 21:50
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/d380446a69d83b1640942937a63361c1 to your computer and use it in GitHub Desktop.
Save wolf99/d380446a69d83b1640942937a63361c1 to your computer and use it in GitHub Desktop.
Portable alternative to isinf() in math.h
int my_inf(double x)
{
volatile double tmp = x; /* Prevent optimization by compiler. */
if ((tmp == x) && ((tmp - x) != 0.0))
return x < 0.0 ? -1 : 1;
return 0;
}
@wolf99
Copy link
Author

wolf99 commented Feb 2, 2017

The sum of infinity and a finite variable is still infinity. Positive and negative infinities can be differentiated by comparing them to zero. A simple test like return (x - x != 0.0); 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