Last active
October 27, 2019 21:50
-
-
Save wolf99/d380446a69d83b1640942937a63361c1 to your computer and use it in GitHub Desktop.
Portable alternative to isinf() 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_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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.