FE_DEC_DOWNWARD FE_DEC_TONEAREST FE_DEC_TONEARESTFROMZERO FE_DEC_TOWARDZERO FE_DEC_UPWARD FE_DEC_DYNAMIC
7.6 Floating-point environment <fenv.h>
7.6.5 Rounding and other control modes
{
#pragma STDC FENV_ACCESS ON int set_excepts;
feclearexcept(FE_INVALID | FE_OVERFLOW);
// maybe raise exceptions
set_excepts = fetestexcept(FE_INVALID | FE_OVERFLOW);
if (set_excepts & FE_INVALID) f();
if (set_excepts & FE_OVERFLOW) g();
/* ... */
}
7.6.5 Rounding and other control modes
1 Thefegetroundandfesetroundfunctions provide control of rounding direction modes. The fegetmodeand fesetmode functions manage all the implementation’s dynamic floating-point control modes collectively.
7.6.5.1 Thefegetmodefunction Synopsis
1 #include <fenv.h>
int fegetmode(femode_t *modep);
Description
2 Thefegetmodefunction attempts to store all the dynamic floating-point control modes in the object pointed to bymodep.
Returns
3 Thefegetmodefunction returns zero if the modes were successfully stored. Otherwise, it returns a nonzero value.
7.6.5.2 Thefegetroundfunction Synopsis
1 #include <fenv.h>
int fegetround(void);
Description
2 Thefegetroundfunction gets the current value of the dynamic rounding direction mode.
Returns
3 Thefegetroundfunction returns the value of the rounding direction macro representing the current dynamic rounding direction or a negative value if there is no such rounding direction macro or the current dynamic rounding direction is not determinable.
7.6.5.3 Thefe_dec_getroundfunction Synopsis
1 #include <fenv.h>
#ifdef __STDC_IEC_60559_DFP__
int fe_dec_getround(void);
#endif
Description
2 Thefe_dec_getroundfunction gets the current value of the dynamic rounding direction mode for decimal floating-point operations.
Returns
3 Thefe_dec_getroundfunction returns the value of the rounding direction macro representing the current dynamic rounding direction for decimal floating-point operations, or a negative value if there is no such rounding macro or the current rounding direction is not determinable.
7.6.5.4 Thefesetmodefunction Synopsis
1 #include <fenv.h>
int fesetmode(const femode_t *modep);
Description
2 Thefesetmodefunction attempts to establish the dynamic floating-point modes represented by the object pointed to bymodep. The argumentmodepshall point to an object set by a call tofegetmode, or equalFE_DFL_MODEor a dynamic floating-point mode state macro defined by the implementation.
Returns
The fesetmodefesetmodefunction returns zero if the modes were successfully established. Other-wise, it returns a nonzero value.
7.6.5.5 Thefesetroundfunction Synopsis
1 #include <fenv.h>
int fesetround(int round);
Description
2 Thefesetroundfunction establishes the rounding direction represented by its argumentround. If the argument is not equal to the value of a rounding direction macro, the rounding direction is not changed.
Returns
3 Thefesetroundfunction returns zero if and only if the dynamic rounding direction mode was set to the requested rounding direction.
4 EXAMPLE Save, set, and restore the rounding direction. Report an error and abort if setting the rounding direction fails.
#include <fenv.h>
#include <assert.h>
void f(int round_dir) {
#pragma STDC FENV_ACCESS ON int save_round;
int setround_ok;
save_round = fegetround();
setround_ok = fesetround(round_dir);
assert(setround_ok == 0);
/* ... */
fesetround(save_round);
/* ... */
}
7.6.5.6 Thefe_dec_setroundfunction
Synopsis
1 #include <fenv.h>
#ifdef __STDC_IEC_60559_DFP__
int fe_dec_setround(int round);
#endif
Description
2 Thefe_dec_setroundfunction sets the dynamic rounding direction mode for decimal floating-point operations to be the rounding direction represented by its argumentround. If the argument is not equal to the value of a decimal rounding direction macro, the rounding direction is not changed.
3 IfFLT_RADIXis not 10, the rounding direction altered by thefesetroundfunction is independent of the rounding direction altered by thefe_dec_setround function; otherwise ifFLT_RADIXis 10, whether thefesetroundandfe_dec_setroundfunctions alter the rounding direction of both standard and decimal floating-point operations is implementation- defined.
Returns
4 Thefe_dec_setroundfunction returns a zero value if and only if the argument is equal to a decimal rounding direction macro (that is, if and only if the dynamic rounding direction mode for decimal floating-point operations was set to the requested rounding direction).
7.6.6 Environment
1 The functions in this section manage the floating-point environment — status flags and control modes — as one entity.
7.6.6.1 Thefegetenvfunction Synopsis
1 #include <fenv.h>
int fegetenv(fenv_t *envp);
Description
2 Thefegetenvfunction attempts to store the current dynamic floating-point environment in the object pointed to byenvp.
Returns
3 Thefegetenvfunction returns zero if the environment was successfully stored. Otherwise, it returns a nonzero value.
7.6.6.2 Thefeholdexceptfunction Synopsis
1 #include <fenv.h>
int feholdexcept(fenv_t *envp);
Description
2 Thefeholdexceptfunction saves the current dynamic floating-point environment in the object pointed to byenvp, clears the floating-point status flags, and then installs a non-stop (continue on floating-point exceptions) mode, if available, for all floating-point exceptions.235)
Returns
3 Thefeholdexceptfunction returns zero if and only if non-stop floating-point exception handling was successfully installed.
235)IEC 60559 systems have a default non-stop mode, and typically at least one other mode for trap handling or aborting; if the system provides only the non-stop mode then installing it is trivial. For such systems, thefeholdexceptfunction can be used in conjunction with thefeupdateenvfunction to write routines that hide spurious floating-point exceptions from their callers.
7.6.6.3 Thefesetenvfunction Synopsis
1 #include <fenv.h>
int fesetenv(const fenv_t *envp);
Description
2 Thefesetenvfunction attempts to establish the dynamic floating-point environment represented by the object pointed to byenvp. The argumentenvpshall point to an object set by a call tofegetenvor feholdexcept, or equal a dynamic floating-point environment macro. Note thatfesetenvmerely installs the state of the floating-point status flags represented through its argument, and does not raise these floating-point exceptions.
Returns
3 Thefesetenvfunction returns zero if the environment was successfully established. Otherwise, it returns a nonzero value.
7.6.6.4 Thefeupdateenvfunction Synopsis
1 #include <fenv.h>
int feupdateenv(const fenv_t *envp);
Description
2 Thefeupdateenvfunction attempts to save the currently raised floating-point exceptions in its automatic storage, install the dynamic floating-point environment represented by the object pointed to byenvp, and then raise the saved floating-point exceptions. The argumentenvpshall point to an object set by a call tofeholdexceptorfegetenv, or equal a dynamic floating-point environment macro.
Returns
3 Thefeupdateenvfunction returns zero if all the actions were successfully carried out. Otherwise, it returns a nonzero value.
4 EXAMPLE Hide spurious underflow floating-point exceptions:
#include <fenv.h>
double f(double x) {
#pragma STDC FENV_ACCESS ON double result;
fenv_t save_env;
if (feholdexcept(&save_env))
return /* indication of an environmental problem */; // compute result
if (/* test spurious underflow */) if (feclearexcept(FE_UNDERFLOW))
return /* indication of an environmental problem */; if (feupdateenv(&save_env))
return /* indication of an environmental problem */; return result;
}
7.7 Characteristics of floating types
<float.h>1 The header<float.h>defines several macros that expand to various limits and parameters of the real floating types.
2 The macros, their meanings, and the constraints (or restrictions) on their values are listed in 5.2.4.2.2 and 5.2.4.2.3. A summary is given in Annex E.