C++ Math rint
The function rounds off the given value to the nearest integer using current rounding mode. The current rounding mode is determined by fesetround.
For example:
Example
rint(7.9) = 8;
if, fesetround(FE_DOWNWARD) then,
rint(7.9) = 7;
Syntax
Suppose a number 'x'. Syntax would be:
Example
return_type rint(data_type x);
Note: The return_type can be float, double or long double.
Parameter
x : The value that can be either float or double.
Return value
It returns the rounded value of x.
Example 1
Let's see a simple example.
Example
#include <iostream>
#include<math.h>
#include <cfenv>
using namespace std;
int main()
{
float x=9.9;
std::cout << "Value of x is :" <<x<< std::endl;
std::cout << "Rounding to nearest,value is :" <<rint(x)<< std::endl;
fesetround(FE_UPWARD);
std::cout << "Rounding to upward,value is :" <<rint(x)<< std::endl;
fesetround(FE_DOWNWARD);
std::cout << "Rounding to downward,value is :" <<rint(x)<< std::endl;
return 0;
}
Output:
Output
Value of x is :9.9
Rounding to nearest,value is :10
Rounding to upward,value is :10
Rounding to downward,value is :9