RE: Detecting math errors

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Floating point errors are detected using _statusfp(), to clear the error use
_clearfp. If you wish for a C exception to be thrown you can enable this
using _controlfp. For example:

void ShowFPError()
{
DWORD dwStatusFP = _statusfp();

// Show error flags
printf("Calculation error flags: ");
if (dwStatusFP & _EM_INVALID)
printf(" _EM_INVALID");
if (dwStatusFP & _EM_DENORMAL)
printf(" _EM_DENORMAL");
if (dwStatusFP & _EM_ZERODIVIDE)
printf(" _EM_ZERODIVIDE");
if (dwStatusFP & _EM_OVERFLOW)
printf(" _EM_OVERFLOW");
if (dwStatusFP & _EM_UNDERFLOW)
printf(" _EM_UNDERFLOW");
if (dwStatusFP & _EM_INEXACT)
printf(" _EM_INEXACT");
if (dwStatusFP == 0)
printf("None");
printf("\r\n");
}

int _tmain(int argc, _TCHAR* argv[])
{
volatile double answer = 0;

// Do the calculation
ShowFPError();
answer = cos(2.0);
printf("cos(2.0)=%f\r\n", answer);
ShowFPError();

return 0;
}

The code will produce the following results:

Calculation error flags: None
cos(2.0)=-0.416147
Calculation error flags: _EM_INEXACT

CJ

"Ulrich Eckhardt" wrote:

Hi!

The C standard says that acos(2.0) must signal failure by setting 'errno' to
EDOM. Since CE fails to adhere to the C standard here by not
providing 'errno', I wonder how it actually signals failures in this and
other similar functions. Does it actually signal that or does it silently
return garbage? How does it differ for the various versions of CE?

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

.