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

lrint() and llrint() in C++

Similar Reads

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 library. The current mode is determined by the function fesetround()....

Examples of lrint()

Example 1...

llrint() in C++

...

Examples of llrint()

...