rint, lrint, llrint

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C99)
(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
(C99)(C99)(C99)
(C99)
(C99)
rintlrintllrint
(C99)(C99)(C99)
Floating point manipulation functions
(C99)(C99)
(C99)
(C99)
Classification
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)
Macro constants
 
Defined in header <math.h>
float rintf( float arg );
(since C99)
double rint( double arg );
(since C99)
long double rintl( long double arg );
(since C99)
long lrintf( float arg );
(since C99)
long lrint( double arg );
(since C99)
long lrintl( long double arg );
(since C99)
long long llrintf( float arg );
(since C99)
long long llrint( double arg );
(since C99)
long long llrintl( long double arg );
(since C99)

Rounds the floating-point argument arg to an integer value in floating-point format, using the current rounding mode. If the result differs from arg (i.e., arg was not an integer value already), the floating-point exception FE_INEXACT is raised.

Contents

[edit] Parameters

arg - floating point value

[edit] Return value

The integer result of rounding arg

[edit] Notes

The only difference between nearbyint and rint is that rint may raise the FE_INEXACT floating-point exception, while nearbyint never raises it.

[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");
}
 
void show_fe_rounding_method(void)
{
    printf("current rounding method:    ");
    switch (fegetround()) {
           case FE_TONEAREST:  printf ("FE_TONEAREST");  break;
           case FE_DOWNWARD:   printf ("FE_DOWNWARD");   break;
           case FE_UPWARD:     printf ("FE_UPWARD");     break;
           case FE_TOWARDZERO: printf ("FE_TOWARDZERO"); break;
           default:            printf ("unknown");
    };
    printf("\n");
}
 
int main(void)
{
    /* current rounding method:  to nearest                               */
    /* If the result is midway between two representable values, the even */
    /* representable is chosen.                                           */
    show_fe_rounding_method();
    printf("\n");
 
    printf("rint(+2.5)      = %+.1f\n", rint(+2.5));  /* even representable */
    printf("rint(-2.5)      = %+.1f\n", rint(-2.5));
    printf("rint(+1.5)      = %+.1f\n", rint(+1.5));  /* even representable */
    printf("rint(-1.5)      = %+.1f\n", rint(-1.5));
    printf("rint(+0.0)      = %+.1f\n", rint(+0.0));  /* all rounding modes */
    printf("rint(-0.0)      = %+.1f\n", rint(-0.0));  /*        "           */
    printf("rint(+INFINITY) = %+f\n",   rint(+INFINITY)); /*    "           */
    printf("rint(-INFINITY) = %+f\n",   rint(-INFINITY)); /*    "           */
    printf("rint(NAN)       = %f\n",    rint(NAN));   /*        "           */
    printf("\n");
 
    /* FE_INEXACT floating-point exception for finite non-integer arguments */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("rint(2.5) = %.1f\n", rint(2.5));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
    printf("\n");
 
    printf("lrint(+2.5) = %+ld\n", lrint(+2.5));
    printf("lrint(-2.5) = %+ld\n", lrint(-2.5));
    printf("lrint(+1.5) = %+ld\n", lrint(+1.5));
    printf("lrint(-1.5) = %+ld\n", lrint(-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("lrint(LONG_MAX+1.5) = %+ld\n", lrint(LONG_MAX+1.5));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
 
    return 0;
}

Possible output:

current rounding method:    FE_TONEAREST
 
rint(+2.5)      = +2.0
rint(-2.5)      = -2.0
rint(+1.5)      = +2.0
rint(-1.5)      = -2.0
rint(+0.0)      = +0.0
rint(-0.0)      = -0.0
rint(+INFINITY) = +inf
rint(-INFINITY) = -inf
rint(NAN)       = nan
 
rint(2.5) = 2.0
Success
current exceptions raised:  FE_INEXACT
 
lrint(+2.5) = +2
lrint(-2.5) = -2
lrint(+1.5) = +2
lrint(-1.5) = -2
 
LONG_MAX            = +9223372036854775807
lrint(LONG_MAX+1.5) = -9223372036854775808
Success
current exceptions raised:  FE_INVALID

[edit] See also

(C99)(C99)(C99)
rounds to nearest integer not greater in magnitude than the given value
(function)
rounds to an integer using current rounding mode
(function)
gets or sets rounding direction
(function)