What is the rint() function in C++?
Overview
In C++, the rint() function is used to round the argument x to an integral value. The argument is rounded to an integral value by making use of the current rounding mode.
Note: The current rounding mode is determined, using the
fesetround()function.
Syntax
double rint(double x);
float rint(float x);
long double rint(long double x);
double rint(T x);
Parameter value
The rint() function takes a single parameter value x, which represents the value taht needs to be rounded to an integral value.
Return value
The rint() function returns a value of either type double, float, or long double.
By default, the rounding is done “to the nearest” integer.
Code example
#include <iostream>#include <cmath>#include <cfenv>using namespace std;int main(){// creatinn our variablesdouble x = 19.6;double result;// setting rounding direction to DOWNWARD using the fesetround functionfesetround(FE_DOWNWARD);// using the rint() functionresult = rint(x);cout << "Rounding (" << x << ") downward = " << result << endl;return 0;}
Code explanation
- Lines 9–10: We create the variables
xandresult. - Line 13: We set the rounding direction to downward, using the
fesetround()function. - Line 16: We implement the
rint()function on thexvariable and assign the output to theresultvariable. - Line 17: We print the
resultvariable.