Standards / Extensions | C or C++ | Dependencies |
---|---|---|
C/C++ DFP | both | z/OS® V1.8 |
#define __STDC_WANT_DEC_FP__
#include <math.h>
long long int llroundd32(_Decimal32 x);
long long int llroundd64(_Decimal64 x);
long long int llroundd128(_Decimal128 x);
long long int llround(_Decimal32 x); /* C++ only */
long long int llround(_Decimal64 x); /* C++ only */
long long int llround(_Decimal128 x); /* C++ only */
If successful, they return the rounded integer. If the correct value is positive or negative and too large to represent as a long long, a domain error will occur and an unspecified value is returned.
⁄* CELEBL21
This example illustrates the llroundd32() function.
*⁄
#pragma strings(readonly)
#define __STDC_WANT_DEC_FP__
#include <fenv.h>
#include <math.h>
#include <stdio.h>
static void try_rm(int, _Decimal32);
⁄* pass back printable rounding mode *⁄
static
char *rm_str(int rm)
{
char *s = "undetermined";
switch (rm)
{
case FE_DEC_TONEAREST :
s = "FE_DEC_TONEAREST" ; break;
case FE_DEC_TOWARDZERO :
s = "FE_DEC_TOWARDZERO" ; break;
case FE_DEC_UPWARD :
s = "FE_DEC_UPWARD" ; break;
case FE_DEC_DOWNWARD :
s = "FE_DEC_DOWNWARD" ; break;
case FE_DEC_TONEARESTFROMZERO :
s = "FE_DEC_TONEARESTFROMZERO" ; break;
case _FE_DEC_TONEARESTTOWARDZERO :
s = "_FE_DEC_TONEARESTTOWARDZERO" ; break;
case _FE_DEC_AWAYFROMZERO :
s = "_FE_DEC_AWAYFROMZERO" ; break;
case _FE_DEC_PREPAREFORSHORTER :
s = "_FE_DEC_PREPAREFORSHORTER" ; break;
}
return s;
}
⁄* Try out one passed-in number with rounding mode *⁄
static void try_rm(int rm, _Decimal32 d32)
{
long long int ll;
(void)fe_dec_setround(rm);
ll = llroundd32(d32);
printf("llroundd32(%+.2HF) = %+lld - rounding mode = %s\n",
d32 , ll, rm_str(rm)
);
return;
}
int main()
{
try_rm( FE_DEC_TONEAREST , 501.50DF);
try_rm( FE_DEC_TOWARDZERO , 501.50DF);
try_rm( FE_DEC_UPWARD , -501.51DF);
try_rm( FE_DEC_DOWNWARD , -501.49DF);
try_rm( FE_DEC_TONEARESTFROMZERO , 500.50DF);
try_rm(_FE_DEC_TONEARESTTOWARDZERO, -501.50DF);
try_rm(_FE_DEC_AWAYFROMZERO , 500.49DF);
try_rm(_FE_DEC_PREPAREFORSHORTER , 501.50DF);
return 0;
}