rint, rintf, rintl, lrint, lrintf, lrintl, llrint, llrintf, llrintl

Header: <math.h>

1-3) Rounds the floating-point argument arg to an integer value in floating-point format, using the current rounding mode.

# Declarations

float rintf( float arg );

(since C99)

double rint( double arg );

(since C99)

long double rintl( long double arg );

(since C99)

#define rint( arg )

(since C99)

long lrintf( float arg );

(since C99)

long lrint( double arg );

(since C99)

long lrintl( long double arg );

(since C99)

#define lrint( 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)

#define llrint( arg )

(since C99)

# Parameters

# Return value

If no errors occur, the nearest integer value to arg, according to the current rounding mode, is returned.

# Notes

POSIX specifies that all cases where lrint or llrint raise FE_INEXACT are domain errors.

As specified in math_errhandling, FE_INEXACT may be (but isn’t required to be on non-IEEE floating-point platforms) raised by rint when rounding a non-integer finite value.

The only difference between rint and nearbyint is that nearbyint never raises FE_INEXACT.

The largest representable floating-point values are exact integers in all standard floating-point formats, so rint never overflows on its own; however the result may overflow any integer type (including intmax_t), when stored in an integer variable.

If the current rounding mode is…

# Example

#include <fenv.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
 
int main(void)
{
#pragma STDC FENV_ACCESS ON
    fesetround(FE_TONEAREST);
    printf("rounding to nearest (halfway cases to even):\n"
           "rint(+2.3) = %+.1f  ", rint(2.3));
    printf("rint(+2.5) = %+.1f  ", rint(2.5));
    printf("rint(+3.5) = %+.1f\n", rint(3.5));
    printf("rint(-2.3) = %+.1f  ", rint(-2.3));
    printf("rint(-2.5) = %+.1f  ", rint(-2.5));
    printf("rint(-3.5) = %+.1f\n", rint(-3.5));
 
    fesetround(FE_DOWNWARD);
    printf("rounding down: \nrint(+2.3) = %+.1f  ", rint(2.3));
    printf("rint(+2.5) = %+.1f  ", rint(2.5));
    printf("rint(+3.5) = %+.1f\n", rint(3.5));
    printf("rint(-2.3) = %+.1f  ", rint(-2.3));
    printf("rint(-2.5) = %+.1f  ", rint(-2.5));
    printf("rint(-3.5) = %+.1f\n", rint(-3.5));
    printf("rounding down with lrint: \nlrint(+2.3) = %ld  ", lrint(2.3));
    printf("lrint(+2.5) = %ld  ", lrint(2.5));
    printf("lrint(+3.5) = %ld\n", lrint(3.5));
    printf("lrint(-2.3) = %ld  ", lrint(-2.3));
    printf("lrint(-2.5) = %ld  ", lrint(-2.5));
    printf("lrint(-3.5) = %ld\n", lrint(-3.5));
 
    printf("lrint(-0.0) = %ld\n", lrint(-0.0));
    printf("lrint(-Inf) = %ld\n", lrint(-INFINITY)); // FE_INVALID raised
 
    // error handling
    feclearexcept(FE_ALL_EXCEPT);
    printf("rint(1.1) = %.1f\n", rint(1.1));
    if (fetestexcept(FE_INEXACT))
        puts("    FE_INEXACT was raised");
 
    feclearexcept(FE_ALL_EXCEPT);
    printf("lrint(LONG_MIN-2048.0) = %ld\n", lrint(LONG_MIN-2048.0));
    if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID was raised");
}

# See also