Theano/libgpuarray

How to properly compare to NAN in C?

Closed this issue · 2 comments

This doesn't work expected, at least for CUDA:

>>> import numpy
>>> import pygpu
>>> ctx = pygpu.init('cuda')
>>> pygpu.set_default_context(ctx)
>>>
>>> def isnan(a):
...     oper = 'res = (a == NAN) ? 1 : 0'
...     return pygpu.elemwise.elemwise1(a, op='', oper=oper)
>>>
>>> a = pygpu.array([1, numpy.nan, numpy.inf])
>>> a
gpuarray.array([  1.,  nan,  inf])
>>> isnan(a)  # wrong result
gpuarray.array([ 0.,  0.,  0.])
>>>
>>> def isinf(a):
...     oper = 'res = (a == INFINITY) ? 1 : 0'
...     return pygpu.elemwise.elemwise1(a, op='', oper=oper)
>>> isinf(a)  # this works
gpuarray.array([ 0.,  0.,  1.])
nouiz commented

This is a C language "properties". All comparison to NAN give false maybe except another nan (I forgot about that case).

The way to do it is like we do in Theano:

https://github.com/Theano/Theano/blob/master/theano/scalar/basic.py#L1369

isnan(a)

For consistency on windows, we do abs(isnan(a))

Ah, thanks @nouiz, I'll update my code.