I have a piece of Javascript code and i am trying to convert it into C code. I have a problem to convert following line to C:
var q = (!isNaN(dLat/dPhi)) ? dLat/dPhi : Math.cos(lat1);
Can you tell me equivalent of isNan in C? Thanks.
I have a piece of Javascript code and i am trying to convert it into C code. I have a problem to convert following line to C:
var q = (!isNaN(dLat/dPhi)) ? dLat/dPhi : Math.cos(lat1);
Can you tell me equivalent of isNan in C? Thanks.
C has an isnan
macro in math.h
(this is a C99 addition).
The macro return an non-zero int
only if the argument is a NaN value.
Don't feel foolish, but it's:
isnan(x)
You can also use fpclassify
and check if the return result is FP_NAN
.
isnan
was first introduced in 3BSD but didn't go into the cross-platform spec until C99. fpclasify
turned up in C99. They're in math.h
.
Microsoft supply _isnan
in MSVC, in float.h
.
On first sight, it looks like the original author just wanted to check for dPhi
being 0, so you might want to substitute that. However, if you want to translate the code verbatim, simply use the isnan
macro from math.h, like this:
#include <math.h>
double q = (!isnan(dLat/dPhi)) ? dLat/dPhi : cos(lat1);
Assuming your C implementation is modern (i.e. anything but MSVC) you should have isnan
available in math.h
. Otherwise, if (x!=x)
is a perfectly good way to check for NAN if you don't care about it modifying the floating point exception flags (which almost nobody uses or cares about).