Read and set the IEEE floating-point rounding mode.
Standard C Library (libc.a)
#include <float.h>
fprnd_t fp_read_rnd()
fprnd_t fp_swap_rnd( RoundMode)
fprnd_t RoundMode;
The fp_read_rnd subroutine returns the current rounding mode. The fp_swap_rnd subroutine changes the rounding mode to the RoundMode parameter and returns the value of the rounding mode before the change.
Floating-point rounding occurs when the infinitely precise result of a floating-point operation cannot be represented exactly in the destination floating-point format (such as double-precision format).
The IEEE Standard for Binary Floating-Point Arithmetic allows floating-point numbers to be rounded in four different ways: round toward zero, round to nearest, round toward +INF, and round toward -INF. Once a rounding mode is selected it affects all subsequent floating-point operations until another rounding mode is selected.
The encodings of the rounding modes are those defined in the ANSI C Standard. The float.h file contains definitions for the rounding modes. Below is the float.h definition, the ANSI C Standard value, and a description of each rounding mode.
float.h Definition | ANSI Value | Description |
---|---|---|
FP_RND_RZ | 0 | Round toward 0 |
FP_RND_RN | 1 | Round to nearest |
FP_RND_RP | 2 | Round toward +INF |
FP_RND_RM | 3 | Round toward -INF |
The fp_swap_rnd subroutine can be used to swap rounding modes by saving the return value from fp_swap_rnd(RoundMode). This can be useful in functions that need to force a specific rounding mode for use during the function but wish to restore the caller's rounding mode on exit. Below is a code fragment that accomplishes this action:
save_mode = fp_swap_rnd (new_mode); ....desired code using new_mode (void) fp_swap_rnd(save_mode); /*restore caller's mode*/
RoundMode | Specifies one of the following modes: FP_RND_RZ, FP_RND_RN, FP_RND_RP, or FP_RND_RM. |
The floor, ceil, nearest, trunc, rint, itrunc, uitrunc, fmod, or fabs (floor, floorf, floorl, nearest, trunc, itrunc, or uitrunc Subroutine) subroutine, fp_any_enable, fp_is_enabled, fp_enable_all, fp_enable,fp_disable_all, or fp_disable (fp_any_enable, fp_is_enabled, fp_enable_all, fp_enable, fp_disable_all, or fp_disable Subroutine) subroutine, fp_clr_flag, fp_read_flag, fp_set_flag, or fp_swap_flag (fp_clr_flag, fp_set_flag, fp_read_flag, or fp_swap_flag Subroutine) subroutine.
Subroutines Overview in AIX 5L Version 5.2 General Programming Concepts: Writing and Debugging Programs.