Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is lrint() 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.

Answers Code

The lrint() function casts the given double value to an integral number: a long int, based on the selected mode of rounding.

Library

#include<math.h>

Declaration

The following are the declarations of lrint():

long int lrint(double x);

Modes of rounding

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

1. FE_TONEAREST

In this mode, the floating point value is rounded to the nearest integer.

2. FE_DOWNWARD

In this mode, lrint() returns the floor of the floating-point value.

3. FE_UPWARD

In this mode, lrint() returns the ceil of the floating-point value.

4. FE_TOWARDZERO

In this mode, lrint() 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()
{
printf("%ld", lrint(1.5)); //rounding off the number
fesetround(FE_DOWNWARD);
printf("\n%ld", lrint(2.9)); //calculating floor of number
fesetround(FE_UPWARD);
printf("\n%ld", lrint(0.2)); //calculating ceil of number
fesetround(FE_TOWARDZERO);
printf("\n%ld", lrint(-2.8)); //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.

Answers Code
Keep Exploring