Open In App

lrint() and llrint() in C++

Last Updated : 27 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// CPP code to illustrate
// the functionality of lrint()
#include <cfenv>
#include <cmath>
#include <iostream>
using namespace std;
 
int main()
{
    int a = 15;
    long int answer;
 
    // setting rounding direction to DOWNWARD
    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++




// CPP code to illustrate
// the functionality of lrint()
#include <cfenv>
#include <cmath>
#include <iostream>
using namespace std;
 
int main()
{
    double a;
    long int answer;
 
    // By default, the rounding direction
    // is set to 'to-nearest'.
    // fesetround(FE_TONEAREST)
    a = 50.35;
    answer = lrint(a);
    cout << "Nearest rounding of " << a << " is " << answer
         << endl;
 
    // mid values are rounded off to higher integer
    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++




// CPP code to illustrate
// the functionality of lrint()
#include <cfenv>
#include <cmath>
#include <iostream>
using namespace std;
 
int main()
{
    double a;
    long int answer;
 
    // Now, the rounding direction
    // is set to UPWARD
    fesetround(FE_UPWARD);
    a = 50.3;
    answer = lrint(a);
    cout << "Upward rounding of " << a << " is " << answer
         << endl;
 
    // Now, the rounding direction
    // is set to DOWNWARD
    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++




// CPP code to illustrate
// the functionality of llrint()
#include <cfenv>
#include <cmath>
#include <iostream>
using namespace std;
 
int main()
{
    int a = 15;
    long long int answer;
 
    // setting rounding direction to DOWNWARD
    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++




// CPP code to illustrate
// the functionality of llrint()
#include <cfenv>
#include <cmath>
#include <iostream>
using namespace std;
 
int main()
{
    double a;
    long long int answer;
 
    // By default, the rounding direction is
    // set to 'to-nearest'. fesetround(FE_TONEAREST)
    a = 50.35;
    answer = llrint(a);
    cout << "Nearest rounding of " << a << " is " << answer
         << endl;
 
    // mid values are rounded off to higher integer
    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++




// CPP code to illustrate
// the functionality of llrint()
#include <cfenv>
#include <cmath>
#include <iostream>
using namespace std;
 
int main()
{
    double a;
    long long int answer;
 
    // Now, the rounding direction
    // is set to UPWARD
    fesetround(FE_UPWARD);
    a = 50.3;
    answer = llrint(a);
    cout << "Upward rounding of " << a << " is " << answer
         << endl;
 
    // Now, the rounding direction is set to DOWNWARD
    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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads