# C++ cmath lrint,lrintf,lrintl , llrint,llrintf,llrintl function

The C++ <cmath> lrint,lrintf,lrintl and llrint,llrintf,llrintl functions round the argument to the nearest integer value.The declaration of the functions are given below.

lrint

1 long lrint(float x);
2 long lrint(double x);
3 long lrint(long double x);
4 long lrintf(float x);
5 long lrintl(long double x);

llrint

1 long long llrint(float x);
2 long long llrint(double x);
3 long long llrint(long double x);
4 long long llrintf(float x);
5 long long llrintl(long double x);

Some points to note:

i) lrint and llrint functions compute the rounded value of the floating point argument using the current rounded direction.If the fractional part is equal to or greater than 0.5 the value is rounded to next integer value and if it is smaller than 0.5,the fractional part is drop and the integer value is taken.

ii) If the argument passed is very large or very small and the return value is outside the range of the return type than the return value is unspecified and a domain or range error may occur.

iii) If NAN is passed the returned value is compiler dependent.

iv) If ±∞ is passed the returned value is compiler dependent.

Code example

/*Passing valid floating point value*/
cout<< “lrint( 34.5 )=” << lrint( 34.5 ) << endl
<< “llrint( 39.3 )=” << llrint( 39.3 ) << endl ;

/*Passing very large value */
cout<< “lrint( 3.23e20 )=” << lrint( 3.23e20 ) << endl ;

/*Passing very small value */
cout<< “lrint( 34.e-20 )=” << lrint( 34.e-20 ) << endl ;

/*Passing NAN */
cout<< “lrint( NAN )=” << lrint( NAN ) << endl ;

/*Passing INFINITY */
cout<< “lrint( INFINITY )=” << lrint( INFINITY ) << endl ;

Output in Code::blocks

lrint( 34.5 )= 35
llrint( 39.3 ) = 39
lrint( 3.23e20 ) = -2147483648
lrint( 34.e-20 )= 0
lrint( NAN )= -2147483648
lrint( INFINITY )= -2147483648

Output in Visual Studio 2015

lrint( 34.5 )= 35
llrint( 39.3 ) = 39
lrint( 3.23e20 ) = 0
lrint( 34.e-20 )= 0
lrint( NAN )= 0
lrint( INFINITY )= 0

#### lrintf and lrintl

*Note

i)The 4th version lrintf is same as the 1st version long lrint(float).The last character ‘f‘ in ‘lrintf’ stands for ‘float’ which signify the argument and return type of the function.

ii)The 5th version lrintl is same as the 3rd version long long lrint(long double).The last character ‘l‘ in ‘lrintl’ stands for ‘long double’ which signify the argument and return type of the function.

llrintf and llrintl also has the same similarity with llrint(float) and llrint(long double) .

Code example:To check the return type of lrint , lrinf, lrintl and llrint , llrintf , llrintl.

#include <typeinfo>

float f=2;
long double ld=2;

/*lrint ,lrintf and lrintl */
cout<< “*lrintf , lrintf and lrintl \n”;
cout<< typeid( lrint(f) ).name( ) << endl /*identify type of lrint returned value */
cout<< typeid( lrintf(2) ).name( ) << endl /*identify type of lrintf returned value */
<< typeid( lrintl(ld) ).name( ) << endl; /*identify type of lrintl(long double) returned value */

/*llrint,llrintf and llrintl*/
cout<< “*lrintl , llrintf and llrintl \n”;

cout<< typeid( llrint(f) ).name() << endl /*indentify type of llrint returned value */
cout<< typeid( llrintf(2) ).name() << endl /*indentify type of llrintf returned value */
<< typeid( llrintl( ld) ).name() << endl; /*identify type of llrintl(long double) returned value*/

Output (in code::blocks)

*lrint , lrintf and lrintl
l (means ‘long int’)
l (means ‘long int’)
l (means ‘long int’)
*llrintl , llrintf and llrintl
x (means ‘long long’)
x (means ‘long long’)
x (means ‘long long’)

*Side Note

Some facts about lrint and llrint :

i) If the rounded value is outside the range of return type an inexact floating-point exception is raised.

ii) When the function raise no other floating-point exception and the result differs from the argument,they raise the inexact floating-point exception.