Infinity and stuff
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.
+/-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.
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.
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!
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.
Alternatively, you could change the conditions to support inf and nan:
ft = ( ft >= 0.0 ) ? ( ( ft <= 1.0 ) ? ft : 1.0 ) : 0.0;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement