这里写代码片
link
ou would need a POSIX system, or a C99 compiler that supported Annex F of the C99 Standard. You can test if Annex F is supported by checking if the macro STDC_IEC_559 is defined. The relevant functions would be found in
// crt_statusfp.c
// Build by using: cl /W4 /Ox /nologo crt_statusfp.c
// This program creates various floating-point errors and
// then uses _statusfp to display messages that indicate these problems.
#include <stdio.h>
#include <float.h>
#include <fenv.h>
#pragma fenv_access(on)
double test( void )
{
double a = 1e-40;
float b;
double c;
printf("Status = 0x%.8x - clear\n", fetestexcept(FE_ALL_EXCEPT));
// Assignment into b is inexact & underflows:
b = (float)(a + 1e-40);
printf("Status = 0x%.8x - inexact, underflow\n", fetestexcept(FE_ALL_EXCEPT));
// c is denormal:
c = b / 0.0;
printf("Status = 0x%.8x - inexact, underflow, denormal\n",
fetestexcept(FE_ALL_EXCEPT));
float d = 1.0 + 2.0;
printf("Status = 0x%.8x \n", fetestexcept(FE_ALL_EXCEPT));
// Clear floating point status:
feclearexcept(FE_ALL_EXCEPT);
printf("Status = 0x%.8x - clear\n", fetestexcept(FE_ALL_EXCEPT));
d = 1.0 + 2.0;
printf("Status = 0x%.8x \n", fetestexcept(FE_ALL_EXCEPT));
feclearexcept(FE_ALL_EXCEPT);
printf("Status = 0x%.8x \n", fetestexcept(FE_ALL_EXCEPT));
return c;
}
int main(void)
{
return (int)test();
}