llroundd32(), llroundd64(), llroundd128() — 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>

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 */
Compile requirement: Use of this function requires the long long data type. See z/OS XL C/C++ Language Reference for information on how to make long long available.

General description

The llround() family of functions round x to the nearest integer, 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

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.

Example

⁄* 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;
}

Related information