nearbyintd32(), nearbyintd64(), nearbyintd128() — 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>

_Decimal32  nearbyintd32(_Decimal32 x); 
_Decimal64  nearbyintd64(_Decimal64 x); 
_Decimal128 nearbyintd128(_Decimal128 x);
_Decimal32  nearbyint(_Decimal32 x);     /* C++ only */
_Decimal64  nearbyint(_Decimal64 x);     /* C++ only */
_Decimal128 nearbyint(_Decimal128 x);    /* C++ only */

General description

The nearbyint() family of functions round x to an integer value, in decimal floating-point format, using the current rounding mode without raising the inexact decimal floating-point exception.
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 causes an overflow, a range error occurs and the value, respectively, of the macro: ±HUGE_VAL_D32, ±HUGE_VAL_D64, or ±HUGE_VAL_D128 (with the same sign as x) is returned.

Example

⁄* CELEBN06

   This example illustrates the nearbyintd64() function.

*⁄

#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)
{
  _Decimal64 r64;
  _Decimal64 d64 = 500.99DD;

  (void)fe_dec_setround(rm);

  r64 = nearbyintd64(d64);

  printf("nearbyintd64(%.2DF) = %DG - rounding mode = %s\n",
         d64, r64, 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