corrupt float

Started by
7 comments, last by xdotdt 18 years, 9 months ago
How can I detect whether a float variable is corrupt? When I debug some codes, sometimes it happens that a float variable becomes like 1.#QNAN0, or similar forms. How can I know this in code, not in debugging-mode by doing like below.

if( corrupt( float_var ) )
{
    printf( "corrupt" );
}
And someone knows what that means? I mean what 1.#QNAN0 or similar one means? [Edited by - xdotdt on July 11, 2005 1:08:04 AM]
kju:b
Advertisement
All NaN values have the property that any comparison operations (, >=, !=,==) are false: so it is not equal to itself.

bool IsNum(float f) { return (f == f); }
NaN means "Not a Number"

for the rest look there
http://www.psc.edu/general/software/packages/ieee/ieee.html

greets Tgar
thanks everybody!
have a nice day.
kju:b
try this:

#include <float.h>bool corrupt( float x ){  return !_finite(x);}
thanks glSmurf.
I found a similar function.

_CRTIMP int __cdecl _finite(double);
_CRTIMP int __cdecl _isnan(double);

Are _finite() and _isnan() different?
If so, how are they different?

Plus, is there a version for float?
kju:b
Quote:Original post by xdotdt
thanks glSmurf.
I found a similar function.

_CRTIMP int __cdecl _finite(double);
_CRTIMP int __cdecl _isnan(double);

Are _finite() and _isnan() different?
If so, how are they different?

They are different in how they treat infinity.

finite returns true for normal numbers, and false for Inf (Infnity) and NaN (Not a Number). isnan returns true for NaN, and false for Inf and normal numbers. It's just a matter of whether Inf is "treated" as a number or a NaN, so to speak. As there are three "states"; number, Inf and NaN, a single true/false function is not enough to identity the different "states".
Quote:Original post by xdotdt
is there a version for float?


they work just as good with floats
thanks everybody who replied.
kju:b

This topic is closed to new replies.

Advertisement