lrint() and llrint() in C++
Last Updated :
27 Jun, 2023
lrint() in C++
The lrint() function rounds the fractional value given in the argument to an integral value using the current rounding mode. This function is defined in <cmath> library. The current mode is determined by the function fesetround().
Note:This function returns the final value in long int.
Syntax
lrint(double a);
lrint(float a);
Parameter
- The lrint() function takes double or float or integer value as an argument.
Return Value
- The lrint() function rounds the fractional value given in the argument to an integral value using the current rounding mode and returns the value in a long int.
Here, the current mode is determined by the function fesetround(). By default, the rounding direction is set to ‘to-nearest’. It can be changed using fesetround().
Error
- It is mandatory to give an argument otherwise it will give an error no matching function for call to ‘lrint()’.
Examples of lrint()
Example 1
The below CPP code illustrates the functionality of lrint() and setting rounding direction to DOWNWARD.
C++
#include <cfenv>
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int a = 15;
long int answer;
fesetround(FE_DOWNWARD);
answer = lrint(a);
cout << "Downward rounding of " << a << " is " << answer
<< endl;
return 0;
}
|
Output
Downward rounding of 15 is 15
Example 2
The CPP code illustrates the functionality of lrint() and values are rounded to the nearest integer using the default rounding mode.
C++
#include <cfenv>
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double a;
long int answer;
a = 50.35;
answer = lrint(a);
cout << "Nearest rounding of " << a << " is " << answer
<< endl;
a = 50.5;
answer = lrint(a);
cout << "Nearest rounding of " << a << " is " << answer
<< endl;
return 0;
}
|
Output
Nearest rounding of 50.35 is 50
Nearest rounding of 50.5 is 50
Example 3
The below CPP code illustrates the functionality of lrint() and the rounding direction is set to UPWARD.
C++
#include <cfenv>
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double a;
long int answer;
fesetround(FE_UPWARD);
a = 50.3;
answer = lrint(a);
cout << "Upward rounding of " << a << " is " << answer
<< endl;
fesetround(FE_DOWNWARD);
a = 50.88;
answer = lrint(a);
cout << "Downward rounding of " << a << " is " << answer
<< endl;
return 0;
}
|
Output
Upward rounding of 50.3 is 51
Downward rounding of 50.88 is 50
llrint() in C++
The llrint() function rounds the fractional value given in the argument to an integral value using the current rounding mode. This function is defined in <cmath> library. The current mode is determined by the function fesetround().
Note: This function returns the value in a long long int.
Syntax
llrint(double a);
llrint(float a);
Parameter
- The llrint() function takes double or float or integer value as an argument.
Return
- The llrint() function rounds the fractional value given in the argument to an integral value using the current rounding mode and returns the value in long long int.
Here, the current mode is determined by the function fesetround(). By default, the rounding direction is set to ‘to-nearest’. It can be changed using fesetround().
Error
- It is mandatory to give an argument otherwise it will give error no matching function for call to ‘llrint()’ like this.
Examples of llrint()
Example 1
The below CPP code illustrates the functionality of llrint() and the rounding directions set to DOWNWARD.
C++
#include <cfenv>
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int a = 15;
long long int answer;
fesetround(FE_DOWNWARD);
answer = llrint(a);
cout << "Downward rounding of " << a << " is " << answer
<< endl;
return 0;
}
|
Output
Downward rounding of 15 is 15
Example 2
The below CPP code illustrates the functionality of llrint() and by default, the rounding direction is set to ‘to-nearest’.
C++
#include <cfenv>
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double a;
long long int answer;
a = 50.35;
answer = llrint(a);
cout << "Nearest rounding of " << a << " is " << answer
<< endl;
a = 50.5;
answer = llrint(a);
cout << "Nearest rounding of " << a << " is " << answer
<< endl;
return 0;
}
|
Output
Nearest rounding of 50.35 is 50
Nearest rounding of 50.5 is 50
Example 3
The below CPP code illustrates the functionality of llrint() and the rounding direction is set to DOWNWARD.
C++
#include <cfenv>
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double a;
long long int answer;
fesetround(FE_UPWARD);
a = 50.3;
answer = llrint(a);
cout << "Upward rounding of " << a << " is " << answer
<< endl;
fesetround(FE_DOWNWARD);
a = 50.88;
answer = llrint(a);
cout << "Downward rounding of " << a << " is " << answer
<< endl;
return 0;
}
|
Output
Upward rounding of 50.3 is 51
Downward rounding of 50.88 is 50
Share your thoughts in the comments
Please Login to comment...