Functions | ||||
(C99) | ||||
(C99) | ||||
(C99) | ||||
(C99)(C99) | ||||
(C99)(C99) | ||||
(C99) | ||||
(C99) | ||||
(C99) | ||||
Macro constants | ||||
FE_ALL_EXCEPTFE_DIVBYZEROFE_INEXACTFE_INVALIDFE_OVERFLOWFE_UNDERFLOW (C99) | ||||
(C99) |
Defined in header <fenv.h> | ||
#define FE_DIVBYZERO /*implementation defined power of 2*/ | (since C99) | |
#define FE_INEXACT /*implementation defined power of 2*/ | (since C99) | |
#define FE_INVALID /*implementation defined power of 2*/ | (since C99) | |
#define FE_OVERFLOW /*implementation defined power of 2*/ | (since C99) | |
#define FE_UNDERFLOW /*implementation defined power of 2*/ | (since C99) | |
#define FE_ALL_EXCEPT FE_DIVBYZERO | FE_INEXACT | \ FE_INVALID | FE_OVERFLOW | \ | (since C99) | |
All these macro constants (exceptFE_ALL_EXCEPT) expand to integer constant expressions that are distinct powers of 2, which uniquely identify all supported floating-point exceptions. Each macro is only defined if it is supported.
The macro constantFE_ALL_EXCEPT, which expands to the bitwise OR of all otherFE_*
, is always defined and is zero if floating-point exceptions are not supported by the implementation.
Constant | Explanation |
FE_DIVBYZERO | pole error occurred in an earlier floating-point operation |
FE_INEXACT | inexact result: rounding was necessary to store the result of an earlier floating-point operation |
FE_INVALID | domain error occurred in an earlier floating-point operation |
FE_OVERFLOW | the result of an earlier floating-point operation was too large to be representable |
FE_UNDERFLOW | the result of an earlier floating-point operation was subnormal with a loss of precision |
FE_ALL_EXCEPT | bitwise OR of all supported floating-point exceptions |
The implementation may define additional macro constants in<fenv.h>
to identify additional floating-point exceptions. All such constants begin withFE_
followed by at least one uppercase letter.
Seemath_errhandling for further details.
#include <stdio.h>#include <math.h>#include <float.h>#include <fenv.h> #pragma STDC FENV_ACCESS ONvoid show_fe_exceptions(void){printf("exceptions raised:");if(fetestexcept(FE_DIVBYZERO))printf(" FE_DIVBYZERO");if(fetestexcept(FE_INEXACT))printf(" FE_INEXACT");if(fetestexcept(FE_INVALID))printf(" FE_INVALID");if(fetestexcept(FE_OVERFLOW))printf(" FE_OVERFLOW");if(fetestexcept(FE_UNDERFLOW))printf(" FE_UNDERFLOW");feclearexcept(FE_ALL_EXCEPT);printf("\n");} int main(void){printf("MATH_ERREXCEPT is %s\n", math_errhandling& MATH_ERREXCEPT?"set":"not set"); printf("0.0/0.0 = %f\n",0.0/0.0); show_fe_exceptions(); printf("1.0/0.0 = %f\n",1.0/0.0); show_fe_exceptions(); printf("1.0/10.0 = %f\n",1.0/10.0); show_fe_exceptions(); printf("sqrt(-1) = %f\n",sqrt(-1)); show_fe_exceptions(); printf("DBL_MAX*2.0 = %f\n",DBL_MAX*2.0); show_fe_exceptions(); printf("nextafter(DBL_MIN/pow(2.0,52),0.0) = %.1f\n",nextafter(DBL_MIN/pow(2.0,52),0.0)); show_fe_exceptions();}
Possible output:
MATH_ERREXCEPT is set0.0/0.0 = nanexceptions raised: FE_INVALID1.0/0.0 = infexceptions raised: FE_DIVBYZERO1.0/10.0 = 0.100000exceptions raised: FE_INEXACTsqrt(-1) = -nanexceptions raised: FE_INVALIDDBL_MAX*2.0 = infexceptions raised: FE_INEXACT FE_OVERFLOWnextafter(DBL_MIN/pow(2.0,52),0.0) = 0.0exceptions raised: FE_INEXACT FE_UNDERFLOW
(C99)(C99)(C99) | defines the error handling mechanism used by the common mathematical functions (macro constant)[edit] |
C++ documentation forfloating-point exception macros |