fp_read_rnd or fp_swap_rnd Subroutine

Purpose

Read and set the IEEE floating-point rounding mode.

Library

Standard C Library (libc.a)

Syntax

#include <float.h>

fprnd_t fp_read_rnd()
fprnd_t fp_swap_rnd( RoundMode)
fprnd_t RoundMode;

Description

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.

Note: The default floating-point rounding mode is round to nearest. All C main programs begin with the rounding mode set to round to nearest.

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*/

Parameters

Item Description
RoundMode Specifies one of the following modes: FP_RND_RZ, FP_RND_RN, FP_RND_RP, or FP_RND_RM.