llround(), llroundf(), llroundl() — Round to the nearest integer

Standards

Standards / Extensions C or C++ Dependencies

C99
Single UNIX Specification, Version 3
C++ TR1 C99

both  z/OS V1R7

Format

#define _ISOC99_SOURCE
#include <math.h>

long long int llround(double x);
long long int llroundf(float x);
long long int llroundl(long double x);
C++ TR1 C99:
#define _TR1_C99
#include <math.h>

long long int llround(float x); 
long long int llround(long double x);
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.
Note: The following table shows the viable formats for these functions. See IEEE binary floating-point for more information about IEEE Binary Floating-Point.
Function Hex IEEE
llround X X
llroundf X X
llroundl X X

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

/*
 * This program illustrates the use of llround() function
 *
 */
#define _ISOC99_SOURCE
#include <math.h>
#include <_Ieee754.h>  /* fpc functions */
#include <stdio.h>


void main() {

  _FP_fpcreg_t   save_rmode;
  long long int  rnd2nearest;
  double         number;

  printf("Illustrates the llround() function\n\n");

  save_rmode.rmode = _RMODE_RZ;
  __fpc_sm(save_rmode.rmode);  /* set rounding mode to round to zero */

  number=501.1;
  rnd2nearest = llround(number);
  printf ("llround(%.1f) = %lli\n",number, rnd2nearest);

  number=1.5;
  rnd2nearest = llround(number);
  printf ("llround(%.1f) = %lli\n",number, rnd2nearest);

  number=-2.5;
  rnd2nearest = llround(number);
  printf ("llround(%.1f) = %lli\n",number, rnd2nearest);
}

Output

Illustrates the llround() function

llround(501.1) = 501
llround(1.5) = 2
llround(-2.5) = -3

Related information