C++ <cmath> - rint() Function
The C++ <cmath> rint() function returns an integral value by rounding up the specified number, using the rounding direction specified by fegetround.
This function may raise FE_INEXACT exceptions, if the returned value differs from x. The nearbyint() function is similar to this function, except it do not raise FE_INEXACT exceptions.
Syntax
double rint (double x); float rint (float x); long double rint (long double x); double rint (T x);
Parameters
x |
Specify a value to round. |
Return Value
Returns an integral value by rounding up the x, using the rounding direction specified by fegetround.
Example:
In the example below, rint() function is used to round the given number.
#include <iostream> #include <cfenv> #include <cmath> using namespace std; void Rounding_Direction_Message(void) { cout<<"Rounding using "; switch(fegetround()) { case FE_DOWNWARD: cout<<"downward"; break; case FE_TONEAREST: cout<<"to-nearest"; break; case FE_TOWARDZERO: cout<<"toward-zero"; break; case FE_UPWARD: cout<<"upward"; break; default: cout<<"unknown"; } cout<<" method:"<<endl; } int main (){ Rounding_Direction_Message(); cout<<"rint(10.2): "<<rint(10.2)<<"\n"; cout<<"rint(10.8): "<<rint(10.8)<<"\n"; cout<<"rint(-5.2): "<<rint(-5.2)<<"\n"; cout<<"rint(-5.8): "<<rint(-5.8)<<"\n"; return 0; }
The output of the above code will be:
Rounding using to-nearest method: rint(10.2): 10 rint(10.8): 11 rint(-5.2): -5 rint(-5.8): -6
❮ C++ <cmath> Library