Representing Infinity


Can anybody tell me or provide code that shows the representation of Infinity in CUDA. I tried to use following code but there is error message that host function can not be called from device .


double inf()
if (std::numeric_limits::has_infinity)
return std::numeric_limits::infinity();
return std::numeric_limits::max();


Something like this should work:

__device__ double infty(void)


	const unsigned long long ieee754inf =  0x7ff0000000000000;

	return __longlong_as_double(ieee754inf);


And just in case you need the float value it is: 0x7f800000;

If you want negative infinity then in both cases change the 7 to a f.

You can figure these values our for yourself if you know that infinity is defined as having an exponent of all 1s and a mantissa of all 0s. The sign bit is still used for +/- infinity.

Single-precision infinity: __int_as_float(0x7f800000)
Double-precision infinity: __hiloint2double(0x7ff00000, 0x00000000)
Double-precision infinity: __longlong_as_double(0x7ff0000000000000ULL) // only for CUDA_ARCH >= 130