lrint() and llrint() in C++

The lrint() function rounds the fractional value given in the argument to an integral value using the current rounding mode.
Here, the current mode is determined by the function fesetround().

Note:This function returnd 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:

  • The lrint() function rounds the fractional value given in the argument to an integral value using the current rounding mode and return 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 error no matching function for call to ‘lrint()’ like this.
  • # CODE 1
    [sourcecode language=”CPP”]
    // 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;
    }
    [/sourcecode]

    OUTPUT :

    Downward rounding of 15 is 15
    

    # CODE 2
    [sourcecode language=”CPP”]
    // 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;
    }
    [/sourcecode]

    OUTPUT :

    Nearest rounding of 50.35 is 50
    Nearest rounding of 50.5 is 50
    

    # CODE 3
    [sourcecode language=”CPP”]
    // 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;
    }
    [/sourcecode]

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

    Note: Thus function returns 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 and return 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.
  • # CODE 1
    [sourcecode language=”CPP”]
    // 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;
    }
    [/sourcecode]

    OUTPUT :

    Downward rounding of 15 is 15
    

    # CODE 2
    [sourcecode language=”CPP”]
    // 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;
    }
    [/sourcecode]

    OUTPUT :

    Nearest rounding of 50.35 is 50
    Nearest rounding of 50.5 is 50
    

    # CODE 3
    [sourcecode language=”CPP”]
    // 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;
    }
    [/sourcecode]

    OUTPUT :

    Upward rounding of 50.3 is 51
    Downward rounding of 50.88 is 50
    


    My Personal Notes arrow_drop_up

    pawanasipugmailcom

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.