<cmath> (math.h)

function
<cmath> <ctgmath>

lround

long int lround  (double x);
long int lroundf (float x);
long int lroundl (long double x);
long int lround (double x);
long int lround (float x);
long int lround (long double x);
long int lround (T x);           // additional overloads for integral types
Round to nearest and cast to long integer
Returns the integer value that is nearest in value to x, with halfway cases rounded away from zero.

The rounded value is returned as a value of type long int. See llround for an equivalent function that returns a long long int instead.

Header <tgmath.h> provides a type-generic macro version of this function.
Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double before calculations (defined for T being any integral type).

Parameters

x
Value to round.

Return Value

The value of x rounded to the nearest integral, casted to a value of type long int.
If the rounded value is outside the range of the return type, the value returned is unspecified, and a domain error or an overflow range error may occur (or none, depending on implementation).

If a domain error occurs:
- And math_errhandling has MATH_ERRNO set: the global variable errno is set to EDOM.
- And math_errhandling has MATH_ERREXCEPT set: FE_INVALID is raised.

If an overflow range error occurs:
- And math_errhandling has MATH_ERRNO set: the global variable errno is set to ERANGE.
- And math_errhandling has MATH_ERREXCEPT set: FE_OVERFLOW is raised.

Example

1
2
3
4
5
6
7
8
9
10
11
12
/* lround example */
#include <stdio.h>      /* printf */
#include <math.h>       /* lround */

int main ()
{
  printf ( "lround (2.3) = %ld\n", lround(2.3) );
  printf ( "lround (3.8) = %ld\n", lround(3.8) );
  printf ( "lround (-2.3) = %ld\n", lround(-2.3) );
  printf ( "lround (-3.8) = %ld\n", lround(-3.8) );
  return 0;
}


Possible output:

Rounding using to-nearest rounding:
lround (2.3) = 2
lround (3.8) = 4
lround (-2.3) = -2
lround (-3.8) = -4

See also