ADVERTISEMENT
ADVERTISEMENT

rint() Function with Example in C++

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

C++ rint() function

rint() function is a library function of cmath header. It is used to round the given value to an integral value based on the specified direction by fegetround() function. It accepts a parameter and returns the rounded value.

Syntax of rint() function:

C++11:

     double rint (double x);
      float rint (float x);
long double rint (long double x);
     double rint (T x); 

Parameter(s):

  • x – represents the value to round.

Return value:

It returns rounded (to an integral value) value.

Example:

    Input:
    double x = 123.4;
    
    Function call:
    rint(x);
    
    Output:
    123

    Input:
    double x = 123.5;
    
    Function call:
    rint(x);
    
    Output:
    124

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

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

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

int main()
{
    double x = 0.0;

    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.4;
    cout << "rint(" << x << "): " << rint(x) << endl;

    x = 123.5;
    cout << "rint(" << x << "): " << rint(x) << endl;

    x = 123.6;
    cout << "rint(" << x << "): " << rint(x) << endl;

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

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

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

    return 0;
}

Output

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

Reference: C++ rint() function

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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.