lrintd32(), lrintd64(), lrintd128() and llrintd32(), llrintd64(), llrintd128() — Round the argument 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 int lrintd32(_Decimal32 x); 
long int lrintd64(_Decimal64 x); 
long int lrintd128(_Decimal128 x); 

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

long long int llrintd32(_Decimal32 x); 
long long int llrintd64(_Decimal64 x); 
long long int llrintd128(_Decimal128 x); 

long long int llrint(_Decimal32 x);   /* C++ only */
long long int llrint(_Decimal64 x);   /* C++ only */
long long int llrint(_Decimal128 x);  /* C++ only */
Compile requirment: 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 lrint() and llrint() families of functions round their argument to the nearest integer value according to the current rounding mode. 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.
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 value. If the correct value is positive or negative and too large to represent as a long (lrint() family) or long long (llrint() family), a domain error will occur and an unspecified value is returned.

Example

⁄* CELEBL20

   This example illustrates the lrintd64() and llrintd128() functions.

*⁄

#pragma strings(readonly)

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

static void try_rm(int);
⁄* 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)
{
  long int      l;
  long long int ll;
  _Decimal64    d64  = 500.01DD;
  _Decimal128   d128 = 500.99DL;

  (void)fe_dec_setround(rm);

  l  =  lrintd64( d64 );
  ll = llrintd128(d128);

  printf(" lrintd64( %.2DF) = %ld - rounding mode = %s\n",
         d64 , l, rm_str(rm)
        );
  printf("llrintd128(%.2DDF) = %lld - rounding mode = %s\n",
         d128, ll, rm_str(rm)
        );

  return;
}


int main()
{
  try_rm( FE_DEC_TONEAREST          );
  try_rm( FE_DEC_TOWARDZERO         );
  try_rm( FE_DEC_UPWARD             );
  try_rm( FE_DEC_DOWNWARD           );
  try_rm( FE_DEC_TONEARESTFROMZERO  );
  try_rm(_FE_DEC_TONEARESTTOWARDZERO);
  try_rm(_FE_DEC_AWAYFROMZERO       );
  try_rm(_FE_DEC_PREPAREFORSHORTER  );

  return 0;
}

Related information