What is lrintl() in C?

The lrintl() function casts the given long double argument to an integral number, a long int, based on the chosen mode of rounding.

lrintl() is a variation of the lrint() function.

Library

#include<math.h>

Declaration

The following are the declarations of lrintl():

long int lrintl(long double x);

Modes of rounding

We can specify the mode of rounding before calling lrintl(). The four modes of rounding are as follows:

1. FE_TONEAREST

In this mode, the long double value is rounded to the nearest integer.

2. FE_DOWNWARD

In this mode, lrintl() returns the floor of the long double value.

3. FE_UPWARD

In this mode, lrintl() returns the ceil of the long double value.

4. FE_TOWARDZERO

In this mode, lrintl() returns the integer that is closer to zero.

Note: The default mode of rounding is FE_TONEAREST.

Code

#include <stdio.h>
#include <fenv.h> //header file for fesetround()
#include <math.h>
int main()
{
long double x = 0.4;
printf("%ld", lrintl(x)); //rounding off the number
fesetround(FE_DOWNWARD);
x = -1.2;
printf("\n%ld", lrintl(x)); //calculating floor of number
fesetround(FE_UPWARD);
x = -2.8;
printf("\n%ld", lrintl(x)); //calculating ceil of number
fesetround(FE_TOWARDZERO);
x = 1.9;
printf("\n%ld", lrintl(x)); //return number closer to zero
return 0;
}

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved