Handling NaN in a Photon?

I have some floats stored in EEPROM - however when the code is run for the first time these may contain NaN.
I have this code to do a bounds check for sensible ranges:


but it doesn’t seem to detect if P_offset_hi is NaN.

Anyone know how to detect a NaN?

Hi @twospoons

Have you tried adding #include <math.h> and using the isnan(x) function?


Ah, is that where its hiding? I saw isnan() in the C docs . I’ll try that thanks.

Well that didn’t work. isnan() isn’t recognised by the compiler.

Well, I can’t confirm that, for me this test code compiles just fine

1 Like

I found the problem. I have #include “softap_http.h” in my code as well, and that causes this error:

test.ino:6:16: error: 'isnan' was not declared in this scope
 if (isnan(x)){

Take it out and it works fine. How do I fix this?

You could follow the suggestion given in the error note

/usr/local/gcc-arm-embedded/arm-none-eabi/include/c++/5.3.1/cmath:641:5: note:   'std::isnan'
     isnan(_Tp __x)

and prefix the namespace std::

  if (std::isnan(x)) {
1 Like

Thank you. I would never have found that. Where on earth was was that hiding?

Did you hit the SHOW RAW button?

No, but I just looked at it now - and I wouldn’t have seen the fix. So thanks again.