Search code examples
c++ccudanumerical

Testing for infinity in CUDA


In a CUDA program, I recently switched from testing for inifinity using

return x==INFINITY || x==-INFINITY;

where INFINITY is from math.h, to

return !isfinite(x);

and was quite surprised to get different results. gnu.org suggests that they actually should behave similarly. Am I missing something? Is it not allowed to use INFINITY in a CUDA kernel?

Edit: I just discovered isinf and noticed that checking using

return isinf(x);

gives the same result as the INFINITY check. Why isn't isfinite(x)==!isinf(x)?


Solution

  • isfinite(a) is the same as !isnan(a) && !isinf(a). If x is NaN, then both isfinite(x) and isinf(x) are false.