Namespaces
Variants
Views
Actions

round, lround, llround

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)(C99)(C99)
Exponential functions
(C99)
(C99)
(C99)
(C99)
Power functions
(C99)
(C99)
Trigonometric and hyperbolic functions
(C99)
(C99)
(C99)
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Nearest integer floating point operations
roundlroundllround
(C99)(C99)(C99)
(C99)
(C99)(C99)(C99)
Floating point manipulation functions
(C99)(C99)
(C99)
(C99)
Classification
(C99)
(C99)
(C99)
Macro constants
 
Defined in header <math.h>
float       roundf( float arg );
(since C99)
double      round( double arg );
(since C99)
long double roundl( long double arg );
(since C99)
long      lroundf( float arg );
(since C99)
long      lround( double arg );
(since C99)
long      lroundl( long double arg );
(since C99)
long long llroundf( float arg );
(since C99)
long long llround( double arg );
(since C99)
long long llroundl( long double arg );
(since C99)

Computes nearest integer to arg. Number is rounded away from zero in halfway cases

Contents

[edit] Parameters

arg - floating point value

[edit] Return value

Nearest integer to arg.

[edit] Example

#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <fenv.h>
#include <limits.h>
#include <string.h>
 
#pragma STDC FENV_ACCESS ON
 
void show_fe_exceptions(void)
{
    printf("current exceptions raised: ");
    if(fetestexcept(FE_DIVBYZERO))     printf(" FE_DIVBYZERO");
    if(fetestexcept(FE_INEXACT))       printf(" FE_INEXACT");
    if(fetestexcept(FE_INVALID))       printf(" FE_INVALID");
    if(fetestexcept(FE_OVERFLOW))      printf(" FE_OVERFLOW");
    if(fetestexcept(FE_UNDERFLOW))     printf(" FE_UNDERFLOW");
    if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
    printf("\n");
}
 
int main(void)
{
    printf("round(+2.5)      = %+.1f\n", round(+2.5));   /* rounded away from zero */
    printf("round(-2.5)      = %+.1f\n", round(-2.5));
    printf("round(+1.5)      = %+.1f\n", round(+1.5));
    printf("round(-1.5)      = %+.1f\n", round(-1.5));
    printf("round(+0.0)      = %+.1f\n", round(+0.0));
    printf("round(-0.0)      = %+.1f\n", round(-0.0));
    printf("round(+INFINITY) = %+f\n",   round(+INFINITY));
    printf("round(-INFINITY) = %+f\n",   round(-INFINITY));
    printf("round(NAN)       = %f\n",    round(NAN));
    printf("\n");
 
    /* no "inexact" floating-point exception for finite non-integer arguments */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("round(2.5)       = %.1f\n", round(2.5));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
    printf("\n");
 
    printf("lround(+2.5) = %+ld\n", lround(+2.5));
    printf("lround(-2.5) = %+ld\n", lround(-2.5));
    printf("lround(+1.5) = %+ld\n", lround(+1.5));
    printf("lround(-1.5) = %+ld\n", lround(-1.5));
    printf("\n");
 
    /* rounded value is outside the range of the return type */
    /* neither domain nor range error occurs                 */
    printf("LONG_MAX             = %+ld\n", LONG_MAX);
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("lround(LONG_MAX+1.5) = %+ld\n", lround(LONG_MAX+1.5));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
 
    return 0;
}

Possible output:

round(+2.5)      = +3.0
round(-2.5)      = -3.0
round(+1.5)      = +2.0
round(-1.5)      = -2.0
round(+0.0)      = +0.0
round(-0.0)      = -0.0
round(+INFINITY) = +inf
round(-INFINITY) = -inf
round(NAN)       = nan
 
round(2.5)       = 3.0
Success
current exceptions raised:  none
 
lround(+2.5) = +3
lround(-2.5) = -3
lround(+1.5) = +2
lround(-1.5) = -2
 
LONG_MAX             = +9223372036854775807
lround(LONG_MAX+1.5) = -9223372036854775808
Success
current exceptions raised:  FE_INVALID

[edit] See also

nearest integer not greater than the given value
(function) [edit]
nearest integer not less than the given value
(function) [edit]
(C99)
nearest integer not greater in magnitude than the given value
(function) [edit]