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


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

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

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

All the functions are the same,they rounded the ‘x’ to the nearest integer value.The 1st,2nd and the 3rd function have the same name i.e.’lrint’ and ‘llrint’.The 4th and 5th function have different names.What does having a different names mean is explained in detail below.

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

double d=5.534 , x1=298778.9e30 ;

float f=5.534 ;

long double ld=5.534 ;

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

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

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

Output in Visual Studio ,

6
6
6

6
6
6

0
0

The x1 argument value is very large and it’s rounded value cannot be represented by ‘long int’ so the output is 0,in Code::Blocks, you will get the output as -2147483648.Check the output in your compiler if you are not using any of the two IDE.


 


lrintf and llrintf

The ‘lrintf‘ function is same as ‘long int lrint(float x)’.And llrintf is same as ‘long long llrint(float x)’ .The ‘f‘ character appended to ‘lrintf‘ and ‘llrintf‘ stands for float and it signify the parameter type and return type of the function.

Code example

float x=59.901 ;

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

Output in Visual Studio,

60
60


lrintl and llrintl

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

Code example

long double x=59.901 ;

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

Output in Visual Studio,

60
60


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


Related links

->C programmming nearbyint,nearbyintf and nearbyintl

->C programming ceil,ceilf and ceill