fenv_t - Linux
Overview
The fenv_t command is used for the manipulation of floating-point environment information in a thread-safe manner. It allows users to access and modify various aspects of the floating-point environment, including rounding modes, precision modes, and exception handling.
Syntax
fenv_t fenv_access(fenv_t *envp, int access)
fenv_t fenv_clear()
int fenv_copysign(fenv_t *envp, const fenv_t *env)
int fenv_hardcopy(fenv_t *envp, const fenv_t *env)
int fenv_merge(fenv_t *envp, const fenv_t *env)
int fenv_compat(fenv_t *envp, int save_context)
int fenv_is_in_range(int capability)
int fenv_peek(fenv_t *envp)
int fenv_popcnt(fenv_t *envp, int offset)
fenv_t fenv_restore(const fenv_t *env, int restore_context)
int fenv_rotatel(fenv_t *envp, int offset)
int fenv_rotr(fenv_t *envp, int offset)
int fenv_run()
int fenv_set(fenv_t *env, int flag)
int fenv_set_p(fenv_t *env, int flag, int newval)
int fenv_setround(int rounding_mode)
int fenv_shiftl(fenv_t *envp, int offset)
int fenv_shiftr(fenv_t *envp, int offset)
int fenv_update(const fenv_t *env)
int fegetexceptflag(fenv_t *env, int flag)
int fesetexceptflag(const fenv_t *env, int flag, int newval)
int feupdateenv(const fenv_t *env)
Options/Flags
- FEROUND: Rounding mode. Can be set to FE_TONEAREST, FE_DOWNWARD, FE_UPWARD, or FE_TOWARDZERO.
- FE_TOWARDZERO: Round towards zero.
- FE_DOWNWARD: Round towards negative infinity.
- FE_UPWARD: Round towards positive infinity.
- FE_TONEAREST: Round to the nearest number.
- FE_ACCESSCALL: Determine if exception is raised on access.
- FE_CHOP: Determine if exception is raised when result would be inexact.
- FE_INVALID: Determine if exception is raised on invalid result.
- FE_OVERFLOW: Determine if exception is raised on overflow.
- FE_UNDERFLOW: Determine if exception is raised on underflow.
- FE_DIVBYZERO: Determine if exception is raised on division by zero.
- FE_PRECISION: Floating-point precision mode. Can be set to FE_DEC_SINGLE, FE_DEC_DOUBLE, FE_DEC_EXTENDED, or FE_OTHER.
- FE_OTHER: Floating-point precision mode not covered by other constants.
- FE_DEC_EXTENDED: 80-bit extended precision.
- FE_DEC_DOUBLE: 64-bit double precision.
- FE_DEC_SINGLE: 32-bit single precision.
- FE_INSIG: Determine if exception is raised on invalid signal.
- FE_SAVE: Save the current floating-point environment.
- FE_ALL_EXCEPT: Mask for all exceptions.
- FE_ALL_EXCEPT_INEXACT: Mask for all exceptions except for inexact.
Examples
1. Get the current floating-point environment:
fenv_t env;
fenv_t current_env = fenv_access(&env, FE_READ);
2. Set the rounding mode to round to the nearest number:
fenv_t env;
fenv_access(&env, FE_WRITE);
fenv_setround(FE_TONEAREST);
3. Print the floating-point environment:
fenv_t env;
fenv_access(&env, FE_READ);
printf("Current floating-point environment:\n");
printf("Rounding mode: %d\n", env.__control & FE_TONEAREST);
printf("Precision mode: %d\n", env.__control & FE_DEC_DOUBLE);
Common Issues
- Incorrect rounding mode: Ensure the rounding mode is set correctly for your application’s needs.
- Unexpected exceptions: Check the exception flags in the floating-point environment to identify the cause of any unexpected exceptions.
- Thread safety: fenv_t is thread-safe, but be aware of potential data races when accessing it from multiple threads.
Integration
The fenv_t command can be combined with other C standard library functions, such as setjmp and longjmp, to control the floating-point environment during error handling.
Related Commands
- fegetround: Get the current rounding mode.
- fesetround: Set the current rounding mode.
- fegetenv: Get the current floating-point environment.
- feupdateenv: Update the current floating-point environment.
- fetestexcept: Test a floating-point exception.