C++ <cmath> - lrint() Function
The C++ <cmath> lrint() function rounds the specified number to an integral value, using the rounding direction specified by fegetround, and returns it as long int type.
The llrint() function is similar to this function, except it returns the result as long long int type.
Syntax
long int lrint (double x); long int lrint (float x); long int lrint (long double x); long int lrint (T x);
Parameters
x |
Specify a value to round. |
Return Value
Returns the value of x which is first rounded to nearby integral value then casted to long int type.
Example:
In the example below, lrint() 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<<"lrint(10.2): "<<lrint(10.2)<<"\n"; cout<<"lrint(10.8): "<<lrint(10.8)<<"\n"; cout<<"lrint(-5.2): "<<lrint(-5.2)<<"\n"; cout<<"lrint(-5.8): "<<lrint(-5.8)<<"\n"; return 0; }
The output of the above code will be:
Rounding using to-nearest method: lrint(10.2): 10 lrint(10.8): 11 lrint(-5.2): -5 lrint(-5.8): -6
❮ C++ <cmath> Library