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.

0

Share this post

Link to post

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.