# Infinity and stuff

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

## 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 on other sites
+/-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 on other sites
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!

##### Share on other sites
Quote:
 Original post by Tree PenguinI 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 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;

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 10
• 27
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633405
• Total Posts
3011680
• ### Who's Online (See full list)

There are no registered users currently online

×