How to check if a float is not a number (NAN)?
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 :)
Quote:Original post by Floating
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 :)
bool isNan(float x) { if(x == x) return false; return true; //or return x != x;}
CM
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);
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);
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).
Also, there already exists a function for testings for NaN. It's called isnan, and it's found in math.h (or cmath).
Quote:Original post by Conner McCloudThat'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?bool isNan(float x) { if(x == x) return false; return true; //or return x != x;}
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?
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?
Quote:Original post by iMalcQuote:Original post by Conner McCloudThat'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?bool isNan(float x) { if(x == x) return false; return true; //or return x != x;}
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 , so I don't see why it wouldn't work, although "cout << 0.0/0.0" yields "-1.#IND". So…*shrug*.<br><br>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. <br><pre><br>bool isInf(float x)<br>{<br> return fabs(x) == numeric_limits<double>::infinity();<br>}</pre><br><br>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. <br><br>CM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement