C programming lround,lroundf,lroundl and llround,llroundf,llroundl


In C programming the <math.h> 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);

Parameters:(lround and llround)
x -The floating point value to be rounded off.

Return type
integer type -The nearest rounded integer value of the argument.

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

double d=89.45 , x1=4.35e200 ;

float f=89.45 ;

long double ld=89.45 ;

/*Calling lround */
printf(“%li”, lround( d ) ) ; //calls the 2nd version
printf(“\n%li”, lround( f ) ) ; //calls the 1st version
printf(“\n%li”, lround( ld ) ) ;//calls the 3rd version

/*Calling llround */
printf(“\n\n%lli”, llround( d ) ) ; //calls the 2nd version
printf(“\n%lli”, llround( f ) ) ; //calls the 1st version
printf(“\n%lli”, llround( ld ) ) ;//calls the 3rd version

printf(“\n\n%li”, lrint( x1 ) ) ;
printf(“\n%lli”, llrint( INFINITY ) ) ;

Output in Visual Studio ,

89
89
89

89
89
89
0
0

Since x1 is very large the rounded value cannot be represented by the return type so 0 is returned and infinity is passed as the argument the returned value is also 0.





lroundf and llroundf

The ‘lroundf‘ function is same as ‘long int lround(float x)’.And llroundf is same as ‘long long llround(float x)’ .The ‘f‘ character appended to ‘lroundf‘ and ‘llroundf‘ stands for float and it signify the parameter type and return type of the function.

Code example

float x=900.234 ;

printf(“%li”, lroundf( x ) ); //Calls the 4th version
printf(“\n%lli”, llroundf( x) ); Calls the 4th version

Output in Visual Studio,

900
900


lroundl and llroundl

The ‘lroundl‘ function is same as ‘long lround(long double x)’.And ‘llroundl‘ is same as ‘long long llroundl(long double x)’ .The ‘l‘ character appended to ‘lroundl‘ and ‘llroundl‘ stands for long double and it signify the parameter type and return type of the function.

Code example

long double x=900.234 ;

printf(“%li”, lroundl( x ) ); //Calls the 5th version
printf(“\n%lli”, llroundl( x) ); Calls the 5th version

Output in Visual Studio,

900
900


*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.

Link: C programming lrint,lrintf,lrintl and llrint,llrintf,llrintl math.h