Jump to content
  • Advertisement
Sign in to follow this  
Floating

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

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

If you intended to correct an error in the post then please contact us.

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

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
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 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!