lrint() and llrint() in C++


In this section we will see the lrint() and llring() in C++. At first let us discuss about the lrint().

The lrint() function is used to round the fractional given value in the argument to an integral value using current rounding mode. The current mode is determined by using fesetround().>=

This lrint() function takes the double or float or integer value as input parameter, and returns the long int value by rounding the fractional part into an integral part.

Example

#include <cfenv>
#include <cmath>
#include <iostream>
using namespace std;
main() {
   int x = 40;
   long int res;
   fesetround(FE_DOWNWARD); // setting rounding direction to DOWNWARD as downward
   res = lrint(x);
   cout << "Downward rounding of " << x << " is " << res << endl;
}

Output

Downward rounding of 40.0235 is 40

The llrint() function is used to round the fractional given value in the argument to an integral value using current rounding mode. The current mode is determined by using fesetround().

This lrint() function takes the double or float or integer value as input parameter, and returns the long long int value by rounding the fractional part into an integral part.

Example

#include <cfenv>
#include <cmath>
#include <iostream>
using namespace std;
main(){
   double a;
   long int res;
   fesetround(FE_UPWARD); //set rounding direction to upward
   a = 40.3;
   res = llrint(a);
   cout << "Upward rounding of " << a << " is " << res << endl;
   fesetround(FE_DOWNWARD); //set rounding direction to downward
   a = 40.88;
   res = llrint(a);
   cout << "Downward rounding of " << a << " is " << res << endl;
}

Output

Upward rounding of 40.3 is 41
Downward rounding of 40.88 is 40

Updated on: 30-Jul-2019

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements