nan question

Hi all

I am working on a thermostat type project - It is working fine but I want to add a few safety aspects now. I notice that if I disconnect one of the thermistors then the the arduino sees that its too cold and will keep on heating.

If I disconnect the thermistor and look at the value of the reading, it returns ‘nan’ - this is not a problem but is there a way to say ‘if (reading == nan)… do something’

I will post the code but I do apologize, its a mess!

graphicstest_WORKING_CUT_8.ino (8.78 KB)

Try the following

if (isnan(someFloat)) {
}

Great, thanks - will try that when I finish work.

So

if (isnan(someFloat)) {
}

someFloat would be my temperature variable? and that if statement would execute if the variable is nan?

Thanks again

Quite obviously. ;)

if (isnan(reading)) {
}

Yeah, I guess that was pretty indisputable :) blonde moment!

On another note - if I want to compare a few variables, how could I check them all with one if statement?

Would it be something like

if (isnan(reading1) || (reading2) || (reading3)){ }

Thanks again, looking forward to trying it out!

dtokez:
if (isnan(reading1) || (reading2) || (reading3)){
}

Code tags man. Use them.

if (isnan(reading1) || isnan(reading2) || isnan(reading 3))

The OR operator doesn’t let you shove more things into a function. You have to call the function on each one.

You can also use the if (reading == reading) trick. This will actually return false if reading is a NaN, since a NaN can never compare equal to anything - even another NaN.

According to the IEEE standard, NaN values have the odd property
that comparisons involving them are always false.
That is, for a float f, f != f will be true only if f is NaN.

Note that, as some comments below have pointed out,
 not all compilers respect this when optimizing code.

For any compiler which claims to use IEEE floating point,
this trick should work. But I can't guarantee that it will work in practice.
Check with your compiler, if in doubt.

from http://stackoverflow.com/questions/570669/checking-if-a-double-or-float-is-nan-in-c