fe_dec_setround() — Set the current rounding mode
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
C/C++ DFP | both | z/OS® V1.8 |
Format
#define __STDC_WANT_DEC_FP__
#include <fenv.h>
int fe_dec_setround(int round);
General description
The fe_dec_setround function establishes the rounding mode for decimal floating-point operations represented by its argument round. If the argument is not equal to the value of a valid rounding mode, the rounding mode is not changed.
The following rouding modes are defined for decimal floating-point, and are located in fenv.h:
- FE_DEC_DOWNWARD
- rounds towards minus infinity
- FE_DEC_TONEAREST
- rounds to nearest
- FE_DEC_TOWARDZERO
- rounds toward zero
- FE_DEC_UPWARD
- rounds toward plus infinity
- FE_DEC_TONEARESTFROMZERO
- rounds to nearest, ties away from zero
- _FE_DEC_AWAYFROMZERO
- rounds away from zero
- _FE_DEC_TONEARESTTOWARDZERO
- rounds to nearest, ties toward zero
- _FE_DEC_PREPAREFORSHORTER
- rounds to prepare for shorter precision
Notes:
- To use IEEE decimal floating-point, the hardware must have the Decimal Floating-Point Facility installed.
- This function works in IEEE decimal floating-point format. See "IEEE Decimal Floating-Point" for more information.
Returned value
If successful, returns a zero value.
If the argument is not equal to a valid rounding mode, returns -1. The following errnos are defined:
- Error Code
- Definition
- EINVAL
- The rounding mode specified is not a valid Decimal Floating Point rounding mode.
- EMVSERR
- The function was unable to set the specified rounding mode due to an internal error.
Example
/* CELEBF77
sample program that issues fe_dec_getround()/setround()
This program calls fe_dec_getround() to get the DFP rounding mode.
Then it will compare the returned value with FE_DEC_TONEAREST
rounding mode. If not the same, it will call fe_dec_setround() to
set the rounding mode to the desired value FE_DEC_TONEAREST.
*/
#define __STDC_WANT_DEC_FP__
#include <fenv.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]){
int r;
if ((r = fe_dec_getround()) == -1){
perror("fe_dec_getround");
exit (-1);
}
printf("The Decimal floating point rounding mode is %d\n", r);
if (r != FE_DEC_TONEAREST){
printf("The DFP rounding mode is not FE_DEC_TONEAREST.\n");
if (fe_dec_setround(FE_DEC_TONEAREST) == -1){
perror("fe_dec_setround");
exit (-1);
}
}
printf("The DFP rounding mode has been set to FE_DEC_TONEAREST.\n");
return 0;
}