Jump to content
  • Advertisement
Sign in to follow this  
Tree Penguin

Infinity and stuff

This topic is 4775 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

Hi, i got a problem and i can not find anything explaining a solution (strange i must admit, i am probably just searching for the wrong terms): I clamp a value between 0.0f and 1.0f: ft=(ft>1.0f)?1.0f:((ft<0.0f)?0.0f:ft); but still values like -1.#inf (or something like it, anyway, infinity and such), values that are not displayed as normal floating point numbers, pass the test. Is there a way to check for these special values? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
+/-Infinity will result from dividing a non-zero integer by zero, and nan's (Not A Number) can be generated a number of ways e.g. dividing zero by zero, or taking the sqrt of a negative value.
Things such as these are probably things you should be dealing with before the operation that produces a nan is performed. For example if you are averaging some values, make sure that the count is at least 1, before dividing by it. Not purely to avoid the divide by zero, but because you may wish to omit other code in that case, return a failure code, throw an exception, produce an error message, or whatever.

That said, nan's behave is an odd way when it comes to comparison operators and you could simply have your program look for that odd behaviour. e.g. a nan not being equal to itself. Or _fpclass may be what you're looking for.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tree Penguin
I am too lazy to look for the code causing the value, but ft!=ft appears to work fine so i'll stick with that, thanks a lot!


Yes, this is the appropriate test.

Share this post


Link to post
Share on other sites
Alternatively, you could change the conditions to support inf and nan:
    ft = ( ft >= 0.0 ) ? ( ( ft <= 1.0 ) ? ft : 1.0 ) : 0.0; 

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!