# rint(), fesetround(), rintf(), rintl() in C++

• Difficulty Level : Basic
• Last Updated : 28 Feb, 2022

1) rint(): It is used to roundoff the floating-point arguments to an integer value (in floating-point format). You can also determine the current rounding mode using a function fesetround() according to which the rint function returns the rounded integer value. It is included in the <cmath> header file.

Syntax:

```double rint(double x);
float rint(float x);
long double rint(long double x);```

Parameters:  The rint() function takes a single argument value to round.

Returns: By default, the rounding direction is set to ‘to-nearest’ otherwise the rint() function rounds the argument to an integral value, using the rounding direction specified by fegetround() and returns the value.

Exceptions or Errors:

• If the result of the function is outside the range of the return type, a domain error error may occur.
• If the argument is 0, it is returned unmodified.
• If the argument is infinite, it is returned unmodified.

Examples:

```Input : 3.3
Output : 3

Input : 3.5
Output : 4

Input : 3.7
Output : 4```

## CPP

 `// CPP program to illustrate rint()``#include ``#include ``using` `namespace` `std;`` ` `// Driver Code``int` `main()``{``    ``double` `a, b, x = 3.3, y = 3.7;`` ` `    ``// Saves the rounded value to a variable``    ``a = rint(x);``    ``b = rint(y);`` ` `    ``// Prints the rounded value``    ``cout << a << endl;`` ` `    ``cout << b;``    ``return` `0;``}`

Output

```3
4```

2) fesetround() function: By default, the rounding direction is set to the nearest integer. But using fesetround() function, we can the direction of our choice. It is include in the <cfenv> header file.

Syntax:

```fesetround(FE_DOWNWARD)
fesetround(FE_UPWARD)```

Example:

• If rint() function is used with argument 3.7 and fesetround(FE_DOWNWARD) is also used, the output would be 3
• If rint() function is used with argument 3.3 and fesetround(FE_UPWARD) is also used, the output would be 4

## CPP

 `// CPP program to illustrate rint()``// with fesetround() function``#include ``#include ``#include ``using` `namespace` `std;`` ` `// Driver Code``int` `main()``{``    ``double` `x = 3.7, y = 3.3, result;`` ` `    ``// setting rounding direction to DOWNWARD``    ``fesetround(FE_DOWNWARD);`` ` `    ``result = rint(x);``    ``cout << result << endl;`` ` `    ``// setting rounding direction to UPWARD``    ``fesetround(FE_UPWARD);`` ` `    ``result = rint(y);``    ``cout << result << endl;`` ` `    ``return` `0;``}`

Output

```3
4```

3) rintf() function: rintf() function is same as the rint function. The only difference is that the parameter and return type of the function is float type. The ‘f‘ character appended to ‘rintf‘ stands for float and it signify the parameter type and return type of the function.

Syntax:

`float rintf(float x);`

Here, variables are assigned float type otherwise type mismatch error occurs.

## CPP

 `// CPP program to illustrate rinft()``#include ``#include ``#include ``using` `namespace` `std;`` ` `// Driver Code``int` `main()``{``    ``float` `x = 3.76542, y = 3.37562, result;``    ``// setting rounding direction to DOWNWARD``    ``fesetround(FE_DOWNWARD);`` ` `    ``result = rintf(x);``    ``cout << result << endl;`` ` `    ``// setting rounding direction to UPWARD``    ``fesetround(FE_UPWARD);`` ` `    ``result = rintf(y);``    ``cout << result << endl;`` ` `    ``return` `0;``}`

Output

```3
4```

4) rintl() function: rintl() function is same as the rint function.The only difference is that the parameter and return type of the function is long double type.The ‘l‘ character appended to ‘rintl‘ stands for long double and it signify the parameter type and return type of the function.
Syntax:

`long double rintl(long double x);`

Here, variables are assigned long double type otherwise type mismatch error occurs.

## CPP

 `// CPP program to illustrate rinfl()`` ` `#include ``#include ``#include ``using` `namespace` `std;`` ` `// Driver Code``int` `main()``{``    ``long` `double` `x = 3.765426764, y = 3.37562657, result;`` ` `    ``// setting rounding direction to DOWNWARD``    ``fesetround(FE_DOWNWARD);`` ` `    ``result = rintl(x);``    ``cout << result << endl;`` ` `    ``// setting rounding direction to UPWARD``    ``fesetround(FE_UPWARD);`` ` `    ``result = rintl(y);``    ``cout << result << endl;`` ` `    ``return` `0;``}`

Output

```3
4```