lroundd32(), lroundd64(), lroundd128() — Round a floating-point number to its 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 int lroundd32(_Decimal32 x); 
long int lroundd64(_Decimal64 x); 
long int lroundd128(_Decimal128 x);

long int lround(_Decimal32 x);     /* C++ only */
long int lround(_Decimal64 x);     /* C++ only */
long int lround(_Decimal128 x);    /* C++ only */

General description

The lround functions round x to the nearest integer value, 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

The lround functions return the rounded integer value of x.

If the rounded value is outside the range of the return type, the numeric result is unspecified. A range error may occur if the magnitude of x is too large.

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