Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is lrintl() in C?

Syed Muhammad Ali Mustafa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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;
}

RELATED TAGS

c

CONTRIBUTOR

Syed Muhammad Ali Mustafa
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring