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

Started by
6 comments, last by Conner McCloud 18 years, 10 months ago
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 :)
Advertisement
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);
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).
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Quote:Original post by Conner McCloud
bool 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?
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
_isnan(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?
Quote:Original post by iMalc
Quote:Original post by Conner McCloud
bool 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 , so I don't see why it wouldn't work, although "cout &lt;&lt; 0.0/0.0" yields "-1.#IND". So…*shrug*.<br><br>floating: I'm less certain &#111;n 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&lt;double&gt;::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&lt;double&gt;::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