I am using Visual C++ to load binary data into float
, like this:
double dValue;
memcpy(&dValue, lpData, sizeof(dValue));
For normal cases, this will work properly. However, for some rare cases, when binary data are corrupt, the dValue
will be invalid and any further operations on it will cause “Float-point invalid operation” exception.
I check the dValue
in debugger and it displays as -1.#QNAN00000000000
.
To prevent the exception, I need to verify the dValue
after it is loaded from the binary data. I try to use:
if (_finite(dValue))
{
… do some tasks…
}
But still the invalid dValue
will cause _finite
function to raise a Float-point invalid operation
exception. Although _finite(dValue)
will return 0 properly, the exception will be allocated in memory. Even if I deallocate it. Too many allocate/deallocate will still exhaust the system resources.
Therefore, is there a way to detect the validity of a double value without raising any exceptions?