I'm looking for a way to check for NaN values in Cython code. At the moment, I'm using:
if value != value:
# value is NaN
else:
# value is not NaN
Is there a better way to do this? Is it possible to use a function like Numpy's isnan
?
I'm looking for a way to check for NaN values in Cython code. At the moment, I'm using:
if value != value:
# value is NaN
else:
# value is not NaN
Is there a better way to do this? Is it possible to use a function like Numpy's isnan
?
Taken from http://groups.google.com/group/cython-users/msg/1315dd0606389416, you could do this:
Then you can just use
isnan(value)
.In newer versions of Cython, it is even easier:
If you want to make sure that your code also works on Windows you should better use
because on Windows, as far as I know, isnan is called _isnan and is defined in float.h
See also here for example: https://github.com/astropy/astropy/pull/186
If you don't want to introduce numpy you could also insert these precompiler directives into the .c file cython generates: