Detect NaN and remove it?

I am currently writing a program that needs to be able to divide by zero and not die.

I have tried to check if NaN is equal to itself and just remove the NaN, but it passes the test.
boolean isnan(float n) { return n!=n; }

I tried debugging with
Serial.begin(9600); Serial.print(1/0);
and it gave me
4294967295, which definitely isn’t NaN.

Does anyone have any solutions?

Just always check for 0 before you divide and handle it accordingly.


I tried to do that, but it still crashed. Do you have any example code?
Also, I realized that 4294967295 is actually 0xFFFFFFFF in hex. Why is it not showing up as NaN?

You have to check the number before using it, example:
// Do the division 1/a
// Do something else


Off topic, but is that SmileBasic in your avatar? :slight_smile:

1 Like

I just found out that a different error was messing with my code. (didn’t put comma in one of my char* arrays) I just tried the simple solution and it works!

(Off topic) I love SmileBASIC, although I’m a bit burnt out on it, so I’m taking a break and messing around with Arduboy programming.