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


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()

...