In this C++ tutorial, you will learn how to round a given number to an integral value using current rounding mode using lrint() function of cmath, with syntax and examples.

## C++ lrint()

C++ lrint() rounds given number as argument to an integral value using current rounding mode.

The current rounding mode can be set by fesetround(), and read by fegetround(). Include cfenv library if using any of these methods.

There are four routing modes defined in cfenv.

• FE_DOWNWARD – rounding towards negative infinity
• FE_TONEAREST – rounding towards nearest representable value
• FE_TOWARDZERO – rounding towards zero
• FE_UPWARD – rounding towards positive infinity

## Syntax

The syntax of C++ lrint() is

`lrint(x)`

where

Returns

The function returns long int.

The synopsis of lrint() function is

```long int lrint(double x);
long int lrint(float x);
long int lrint(long double x);
long int lrint(T x); // for integral type argument values```

lrint() is a function of cmath library. Include cmath library in the program, if using lrint() function.

## Example

### 1. lrint() with Rounding Mode set to FE_DOWNWARD

In this example, we read a double value from user into x, and find its rounded value using lrint() with rounding mode set to FE_DOWNWARD.

C++ Program

```#include <iostream>
#include<cmath>
#include<cfenv>
using namespace std;

int main() {
double x;
cout << "Enter a number : ";
cin >> x;

fesetround(FE_DOWNWARD);

long int result = lrint(x);
cout << "lrint(" << x << ") : " << result << endl;
}```

Output

```Enter a number : 3.8
lrint(3.79999) : 3
Program ended with exit code: 0```
```Enter a number : -3.14
lrint(-3.14001) : -4
Program ended with exit code: 0```

### 2. lrint() with Rounding Mode set to FE_TONEAREST

In this example, we read a double value from user into x, and find its rounded value using lrint() with rounding mode set to FE_DOWNWARD.

C++ Program

```#include <iostream>
#include<cmath>
#include<cfenv>
using namespace std;

int main() {
double x;
cout << "Enter a number : ";
cin >> x;

fesetround(FE_TONEAREST);

long int result = lrint(x);
cout << "lrint(" << x << ") : " << result << endl;
}```

Output

```Enter a number : 3.8
lrint(3.8) : 4
Program ended with exit code: 0```
```Enter a number : 3.12
lrint(3.12) : 3
Program ended with exit code: 0```

Similarly, we could check out for other rounding modes.

## rint() vs lrint()

The difference between these two functions is that lrint() returns only long int, while the return type of rint() depends on the type of argument.

## Conclusion

In this C++ Tutorial, we learned the syntax of C++ lrint(), and how to use this function to round given number based on current rounding mode, with the help of examples.