C++

C++ rint() Function

Today, we will study one of the important functions of rounding off the integer value in C++ programming language. We will learn how we will implement this rounding method. But, before that, let’s take a quick look at the basics of C++ so that no second thought will be left for the user.

C++ is a procedural and easy-to-understand object-oriented language of programming that offers programs a clear structure that enables code to be evaluated within the same program. But sometimes, there is a complex problem that we want to solve. For that, we use multiple functions in C++ to divide the complex problem into smaller chunks, which are the functions we used in our program. And today, we are studying one of the important functions, which is the rint() function in C++.

Introduction

In C++, the rint() function is the predefined function that rounds off the value into the nearest integer value. To round off the input value, we use the current rounding mode, which is fesetround() mode. To understand and learn more clearly about the rint() function, let’s dig deep and see how we will implement this function in C++.

Syntax

Let’s understand the writing style and implementation of the rint() function in C++. First, we will write the return type of the rint() function. In the rint() function brackets, we will write the data type of the input variable and pass the input parameter in it so that we get the input value into the rounded integer type.

Parameter

Input_variable: can be any variable name containing any value in it. For example, we have a parameter x which we want to round off.

Errors and Exceptions

If we pass the 0 parameter and infinite parameter, in return, we will get the original input value. And if the function’s output is outside the acceptable parameters for the return type, a domain error could have occurred.

Return Value

In return, we will get the rounded integer type value of the input value.

Example 01

Let’s start implementing our very first and simplest example of the rint() function, which we will write in C++. We need a C++ compiler to implement the rint() function. Open the compiler and start writing the code.

In the C++ program, we first include the basic libraries related to our program. These libraries are the predefined libraries of C++. We only have to write a single line of code to include these libraries instead of writing hundreds of lines to make the library. To include the file, we first write the “#” sign that informs the compiler to load the header file, the term “include” consists of the header file into the program, and the “iostream” indicates receiving data from the user and displaying it to the user.

We have also incorporated the second header file with the prefix “#include <cmath>” so that we can perform the round function because it is a mathematical function. That’s why we have used the “cmath” library. To keep classes, functions, and variables from using the same context throughout the entire program, we next utilized the “using namespace std” directive.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    float X = 9.1, Y = 0.9;

    cout << "The value of X after rounding off: " << rint(X) << endl;
    cout << "The value of Y after rounding off: " << rint(Y);
   
    return 0;
}

 

Then we will start writing the main() function because here. We will write the actual line of code or implement the function we want to implement. In the main() function brackets, we have declared the two variables named “X and Y” of type float and assigned them different values. Then we will call the round-off function that we want to perform, which is the rint() function. We call the function by first writing the function’s name, which is the rint() function, and then the input variable “X” in it. And then, we will print them by writing the cout() method and passing the function. We have done the same for variable “Y,”. And in the end, we will return 0 to the main() function and close the bracket.

Here, we have the desired output, which is the value of “X” is 9, and the value of “Y” is 1 in integer type.

Example 02

Now, let’s move on to the second example of the rint() function in C++ language. In this example, we have used the current mode method, which is the fesetround() mode. The fesetround() method creates the “current rounding direction” in the rint() function that directs the input value upward, downward, tonearest, and towardzero direction.

#include <iostream>  
#include<cmath>  
#include <cfenv>  

using namespace std;  

int main()  
{  
    double X;
   
    cout << "Enter the input value of X is: ";
    cin >> X;
   
    cout << "\nRounding to nearest integer of X(" << X << "): " << rint(X) << endl;
   
    fesetround(FE_UPWARD);  
    cout << "Rounding X(" << X << ")  upward: " << rint(X) << endl;  
   
    fesetround(FE_DOWNWARD);  
    cout << "Rounding X(" << X << ")  downward: " << rint(X) << endl;  
   
    return 0;  
}

 

We have included some basic libraries related to the functions we will implement in the program. The first header file is “#include <iostream>” to input and output the data. The second header file is “#include <cmath>” because the rint() function is a mathematical function and the third header file is “#include <cfenv>” so that we can use the fesetround() function in our program. Then we have included the preprocessor directive “namespace std”.

Then we call the main() function and start writing the actual line of code. First, we will declare the variable “X” of type double, and then we will get the value from the user by using the cin() method of C++ and then print it by using the cout() method. Next, we will call the rint() function to print the nearest rounded value of “X” through the cout() method.

Now, we have used the fesetround() method to print the values in upward and downward directions. For that, call the fesetround() function and write the “FE_UPWARD” in capital letters in function brackets and print it by passing the rint() function in the cout() method. Then we print the values in a downward direction so write the fesetround() method and pass “FE_DOWNWARD” in capital letters and write the rint() function in the cout() method. And in the end, return 0 to the main() function and close the brackets.

Let’s see the output of the previous example:

Conclusion

In this article, we have learned about the role of functions in C++, and we covered our main topic, which is the rint() function in C++. We have learned how the rint() function works in C++ and how we get the rounded integer value by using the fesetround() method. We have also implemented some examples in the detailed explanation of every line of code so that the user can easily understand the examples.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.