# C++ cmath lround,lroundf,lroundl and llround,llroundf,llroundl

The C++ <cmath> lround,lroundf and lroundl and llround,llroundf,llroundl function compute the rounded integer value of the parameter.The declaration of the functions are given below.

lround

1 long int lround(float x);
2 long int lround(double x);
3 long int lround(long double x);
4 long int lroundf(float x);
5 long int lroundl(long double x);

llround

1 long long int llround(float x);
2 long long int llround(double x);
3 long long int llround(long double x);
4 long long int llroundf(float x);
5 long long int llroundl(long double x);

Note the only difference between lround and llround is llround returns ‘long long int’ type,but lround returns ‘long int’ type.

Some points to note:

i) The lround and llround functions round their argument to the nearest integer value,rounding halfway cases away from zero, regardless of the current rounding direction.(Current rounding direction means if the fractional part is equal to or greater than 0.5 the value is rounded to the next integer value).

ii) If the rounded value is outside the range the return type can represent than return value is unspecified and range or domain error may occur.

iii) If ±∞ is passed the returned the returned value is unspecified and compiler dependent.

iv) If NaN is passed the returned value is 0.

Code example

cout<< “lround(23.56)=” << lround(23.56) << endl
<< “llround(27.06)=” << llround(27.06) << endl ;

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

Output in code::blocks

llround(23.56)= 24
llround(27.06)= 27
llround(INFINITY)= 2147483647 (Visual Studio gives 0 for this)

#### lroundf,lroundl and llroundf,llroundl

Note the explanation below also holds true for llroundf and llroundl.

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

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

Code example

cout<< lround( float(99.06) ) << endl ///calls the float type parameter version
<< lroundf( 700.6 ) << endl ;
<< llroundf( 2.76789e3 ) << endl ;

cout<< “\n”;

cout<< lround( long double(12.9e3) ) << endl ///calls the long double type parameter version
<< lroundl( 1.6 ) << endl ;
<< llroundl( 0.4 ) << endl ;

Output

99
701
2768

12900
2
0

*Side Note

The only difference between llround and lround function and lrint and llrint functions is that unlike lrint and llrint,the lround and llround round halfway cases away from zero and need not raise the ‘inexact’ floating-point exception for non-integer arguments that round to within the range of the return type.