llrint() Function with Example in C++

C++ llrint() function: Here, we are going to learn about the llrint() function with example of cmath header in C++ programming language.
Submitted by IncludeHelp, on May 20, 2020

C++ llrint() function

llrint() function is a library function of cmath header, it is used to round the given value and convert it to a long long integer. It accepts a value (float, double, or long double) and returns a long long integer value after rounding it based on the rounding direction specified by fegetround() function.

Syntax of llrint() function:

C++11:

    long long int llrint (double x);
    long long int llrint (float x);
    long long int llrint (long double x);
    long long int llrint (T x); 

Parameter(s):

  • x – represents the value to round.

Return value:

The returns type of this function is long long int, it returns a long long integer value rounded to nearby integral.

Example:

    Input:
    float x = 123.4f;
    
    Function call:
    llrint(x);
    
    Output:
    123

    Input:
    float x = 123.5f;
    
    Function call:
    llrint(x);
    
    Output:
    124

C++ code to demonstrate the example of llrint() function

// C++ code to demonstrate the example of
// llrint() function

#include <iostream>
#include <cmath>
#include <fenv.h> // for fegetround()
using namespace std;

int main()
{
    float x = 0.0f;

    cout << "Specified rounding is: ";
    switch (fegetround()) {
    case FE_DOWNWARD:
        cout << "Downward" << endl;
        break;
    case FE_TONEAREST:
        cout << "To-nearest" << endl;
        break;
    case FE_TOWARDZERO:
        cout << "Toward-zero" << endl;
        break;
    case FE_UPWARD:
        cout << "Upward" << endl;
        break;
    default:
        cout << "Unknown" << endl;
    }

    x = 123.4f;
    cout << "llrint(" << x << "): " << llrint(x) << endl;

    x = 123.5f;
    cout << "llrint(" << x << "): " << llrint(x) << endl;

    x = 123.6f;
    cout << "llrint(" << x << "): " << llrint(x) << endl;

    x = -123.4f;
    cout << "llrint(" << x << "): " << llrint(x) << endl;

    x = -123.5f;
    cout << "llrint(" << x << "): " << llrint(x) << endl;

    x = -123.6f;
    cout << "llrint(" << x << "): " << llrint(x) << endl;

    return 0;
}

Output

Specified rounding is: To-nearest
llrint(123.4): 123
llrint(123.5): 124
llrint(123.6): 124
llrint(-123.4): -123
llrint(-123.5): -124
llrint(-123.6): -124

Reference: C++ llrint() function







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.