C++ Standard Library C++ STL Library

C++ <cmath> - llrint() Function



The C++ <cmath> llrint() function rounds the specified number to an integral value, using the rounding direction specified by fegetround, and returns it as long long int type.

The lrint() function is similar to this function, except it returns the result as long int type.

Syntax

long long int llrint (double x);
long long int llrint (float x);
long long int llrint (long double x);
long long int llrint (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 long int type.

Example:

In the example below, llrint() 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<<"llrint(10.2): "<<llrint(10.2)<<"\n";
  cout<<"llrint(10.8): "<<llrint(10.8)<<"\n";
  cout<<"llrint(-5.2): "<<llrint(-5.2)<<"\n";
  cout<<"llrint(-5.8): "<<llrint(-5.8)<<"\n";

  return 0;
}

The output of the above code will be:

Rounding using to-nearest method:
llrint(10.2): 10
llrint(10.8): 11
llrint(-5.2): -5
llrint(-5.8): -6

❮ C++ <cmath> Library