Home »
C++ programming language
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
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.
Sample Input and Output
Input:
double x = 123.4;
Function call:
rint(x);
Output:
123
Input:
double x = 123.5;
Function call:
rint(x);
Output:
124
Example
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