roundd32(), roundd64(), roundd128() — Round to the nearest integer

Standards

Standards / Extensions C or C++ Dependencies
C/C++ DFP both z/OS® V1.8

Format

#define __STDC_WANT_DEC_FP__
#include <math.h>

_Decimal32 roundd32(_Decimal32 x); 
_Decimal64 roundd64(_Decimal64 x); 
_Decimal128 roundd128(_Decimal128 x);

_Decimal32 round(_Decimal32 x);      /* C++ only */
_Decimal64 round(_Decimal64 x);      /* C++ only */
_Decimal128 round(_Decimal128 x);    /* C++ only */

General description

These functions round x to the nearest integer, in decimal floating-point format and rounding halfway cases away from zero, regardless of the current rounding mode.

Notes:
  1. To use IEEE decimal floating-point, the hardware must have the Decimal Floating-Point Facility installed.
  2. These functions work in IEEE decimal floating-point format. See "IEEE Decimal Floating-Point" for more information.

Returned value

These functions return the rounded integer value.

Example

/* CELEBR22

   This example illustrates the round64() function.

*/

#pragma strings(readonly)

#define __STDC_WANT_DEC_FP__
#include <fenv.h>
#include <math.h>
#include <stdio.h>

static void try_rm(int, _Decimal64);

/* 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, _Decimal64 d64)
{
  _Decimal64 r64;

  (void)fe_dec_setround(rm);

  r64 = roundd64(d64);

  printf("roundd64(%+.2DF)  = %+DG - rounding mode = %s\n",
         d64 , r64, rm_str(rm)
        );

  return;
}


int main()
{
  try_rm( FE_DEC_TONEAREST          ,  501.50DD);
  try_rm( FE_DEC_TOWARDZERO         ,  501.50DD);
  try_rm( FE_DEC_UPWARD             , -501.51DD);
  try_rm( FE_DEC_DOWNWARD           , -501.49DD);
  try_rm( FE_DEC_TONEARESTFROMZERO  ,  500.50DD);
  try_rm(_FE_DEC_TONEARESTTOWARDZERO, -501.50DD);
  try_rm(_FE_DEC_AWAYFROMZERO       ,  500.49DD);
  try_rm(_FE_DEC_PREPAREFORSHORTER  ,  501.50DD);

  return 0;
}

Related information