# How to check if a float is not a number (NAN)?

This topic is 4986 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I have a little assembly routine that tells me if a float is infinite. Does such a routine exist to check whether a float is not a number? EDIT: I am programming under Windows XP Thanks :)

##### Share on other sites
Quote:
 Original post by FloatingHello,I have a little assembly routine that tells me if a float is infinite. Does such a routine exist to check whether a float is not a number?EDIT: I am programming under Windows XPThanks :)

bool isNan(float x) {    if(x == x)        return false;    return true;    //or return x != x;}

CM

##### Share on other sites
Thanks for the quick reply :)

Just another little question: how come following code doesn't work?

if (number==std::numeric_limits<float>::infinity())
Beep(1000,1000);
if (number==std::numeric_limits<float>::quiet_NaN())
Beep(1000,1000);

##### Share on other sites
NaN will never equal anything else; even other NaNs.

Also, there already exists a function for testings for NaN. It's called isnan, and it's found in math.h (or cmath).

##### Share on other sites
Quote:
 Original post by Conner McCloudbool isNan(float x) { if(x == x) return false; return true; //or return x != x;}
That's odd, that definately doesn't work for the nan produced by dividing zero by zero, which is the easiest way to produce a nan that I can think of. Are you sure that should work?

##### Share on other sites
_isnan(x)
?

##### Share on other sites
Thank you for the replies.

And how can I use std::numeric_limits<float>::infinity() ?
Is it only meant to generate an infinite value or can I use it to check if my number is infinite too?

##### Share on other sites
Quote:
Original post by iMalc
Quote:
 Original post by Conner McCloudbool isNan(float x) { if(x == x) return false; return true; //or return x != x;}
That's odd, that definately doesn't work for the nan produced by dividing zero by zero, which is the easiest way to produce a nan that I can think of. Are you sure that should work?

1/0 is infinity, not NaN. *edit: doh, zero/zero. Not sure on that point, let me check. *edit2: For me at least, isNaN(0.0/0.0) yields true. The format of that is fff8000000000000, which is a NaN according to the IEEE standard [S=1, E=2047, F>0], so I don't see why it wouldn't work, although "cout << 0.0/0.0" yields "-1.#IND". So...*shrug*.

floating: I'm less certain on this point, but I believe infinity is allowed to equal itself. A quick test program confirms that that appears to be the behavior. So you can just compare to numeric_limits<double>::infinity(), provided you're careful of sign, since you can have both a positive and a negative infinity, and they don't equal each other.
bool isInf(float x){   return fabs(x) == numeric_limits<double>::infinity();}

In .NET2003, I can't get isnan to work with either math.h or cmath. The IDE does see _isnan, but it is declared in float.h.

CM

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634134
• Total Posts
3015751
×