C Standard Library

C <math.h> - llrint() Function



The C <math.h> 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 llrintf (float x);
long long int llrintl (long double 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 <stdio.h>
#include <fenv.h>
#include <math.h>

void Rounding_Direction_Message(void) {
  printf("Rounding using ");
  switch(fegetround()) {
    case FE_DOWNWARD: 
      printf("downward"); break;
    case FE_TONEAREST:   
      printf("to-nearest"); break;
    case FE_TOWARDZERO:   
      printf("toward-zero"); break;
    case FE_UPWARD:  
      printf("upward"); break;
    default:
      printf("unknown");
  }
  printf(" method:\n");
}

int main (){
  Rounding_Direction_Message();

  printf("llrint(10.2): %lld\n", llrint(10.2));
  printf("llrint(10.8): %lld\n", llrint(10.8));
  printf("llrint(-5.2): %lld\n", llrint(-5.2));
  printf("llrint(-5.8): %lld\n", llrint(-5.8));

  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 <math.h> Library

5