Rint Rintf And Rintl Functions In C++ - C++ Programming Tutorial
C++ Course / Functions / Rint Rintf And Rintl Functions In C++

Rint Rintf And Rintl Functions In C++

BLUF: Mastering Rint Rintf And Rintl Functions In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Rint Rintf And Rintl Functions In C++

C++ is renowned for its efficiency. Learn how Rint Rintf And Rintl Functions In C++ enables low-level control and high-performance computing in the tutorial below.

In this guide, you will discover the functionalities of the rint, rintf, and rintl functions in C++ along with their respective syntax and usage instances.

Introduction of "rint, rintf, rintl function in C++":

In C++, the functions rint, rintf, and rintl are included in the header file <cmath>, and they are employed to round a floating-point number to the nearest integer. These functions use the round-to-nearest mode, rounding to the nearest even integer when there is a tie.

Here is a brief demonstration for each of those functions:

1. Rint :

This functionality accepts a range of double-precision floating-point numbers as input and outputs the nearest integer after rounding.

Syntax:

It has the following syntax:

Example

#include <cmath>
double rint (double x);
float rint (float x);
long double rint(long double x);

Example:

Let's consider a scenario to demonstrate the utilization of the rint function in C++.

Example

#include <iostream>
#include <cmath>
int main() {
 double x = 3.6;
 double roundedValue = std::rint(x);
 std::cout << "Original value: " << x << std::endl;
 std::cout << "Rounded value: " << roundedValue << std::endl;

 return 0;
}

Output:

Output

Original value: 3.6
Rounded value: 4

The time and space efficiency of the std::rint function in C++ is generally constant (O(1)). Now, we will delve into the intricacies of these complexities:

Time Complexity: The std::rint function typically involves arithmetic calculations and possibly some bitwise operations to round a floating-point number to the nearest integer. These calculations exhibit consistent time complexity, indicating that their execution time is independent of the input size.

Space Utilization: The space complexity of std::rint remains constant (O(1)). It utilizes a consistent amount of memory for storing local variables and executing computations. The memory usage does not increase with the size of the input. To sum up, the std::rint function in C++ is engineered to exhibit efficient constant time and space complexity, ensuring its suitability for diverse applications without causing performance issues related to input size.

2. Rintf :

Similar to the function rint, this function accepts a single-precision floating-point number as an argument and outputs the nearest integer value after rounding.

Syntax:

It has the following syntax:

Example

#include <cmath>
float rintf (float x);

Example:

Let's consider an example to demonstrate the utilization of the rintf function in C++.

Example

#include <iostream>
#include <cmath>

int main() {
 float x = 3.6f;
 float roundedValue = std::rintf(x);

 std::cout << "Original value: " << x << std::endl;
 std::cout << "Rounded value: " << roundedValue << std::endl;

 return 0;
}

Output:

Output

Original value: 3.6
Rounded value: 4

Explanation:

In this scenario, the original value is 3.6f, and following the rounding process using std::rintf, the outcome becomes 4.0f. Similar to the prior instance, when a decimal .0 is printed, the output does not explicitly show it as it is automatically implied for whole numbers.

Complexity Analyses:

The time and space efficiency of the std::rintf function in C++ generally remains constant at O(1). Here is a detailed explanation of these complexities:

Time Complexity: The std::rintf function involves mathematical computations to round a single-precision floating-point number to the nearest integer. Typically, these computations exhibit constant time complexity, meaning the time taken for these calculations remains consistent regardless of the input size.

The space complexity of the std::rintf function remains constant at O(1). It utilizes a fixed amount of memory to store local variables and perform computations. This memory usage does not increase with larger input sizes.

3. Rintl:

This feature is employed for extended double-precision floating-point numbers. It accepts a long double as an argument and outputs the rounded integer value.

Syntax:

It has the following syntax:

Example

#include <cmath>
long double rintl (long double x);

Example:

Let's consider a scenario to demonstrate the application of the rintl function in C++.

Example

#include <iostream>
#include <cmath>

int main () {
 long double x = 3.6L;
 long double roundedValue = std::rintl(x);

 std::cout << "Original value: " << x << std::endl;
 std::cout << "Rounded value: " << roundedValue << std::endl;

 return 0;
}

Output:

Output

Original value: 3.6
Rounded value: 4

Explanation:

In this scenario, the original value is 3.6L and after using std::rintl, the outcome becomes 4.0L. Similar to previous cases, the .0 decimal part is omitted in the final printout since it is automatically inferred for whole numbers.

Complexity Analyses:

The time and space efficiency of the std::rintl function in C++ typically remains constant at O(1). Here is an analysis of the complexity:

The std::rintl function involves mathematical calculations to round a long double-precision floating-point number to the closest integer. These calculations generally exhibit a consistent time complexity. The duration for executing these calculations remains unaffected by the magnitude of the input.

The space complexity of std::rintl remains constant (O(1)) as it utilizes a fixed amount of memory for storing variables and executing computations, irrespective of the input size.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience