Sign in to follow this  
Floating

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

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 this post


Link to post
Share on other sites
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

Share this post


Link to post
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 this post


Link to post
Share on other sites
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?

Share this post


Link to post
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 this post


Link to post
Share on other sites
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 [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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this