Math Rint Function - C++ Programming Tutorial
C++ Course / Math Functions / Math Rint Function

Math Rint Function

BLUF: Mastering Math Rint Function 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: Math Rint Function

C++ is renowned for its efficiency. Learn how Math Rint Function enables low-level control and high-performance computing in the tutorial below.

C++ Math rint

The function will round the provided value to the nearest whole number based on the current rounding mode set by fesetround.

For example:

Example

rint(7.9) = 8;
if, fesetround(FE_DOWNWARD) then,
rint(7.9) = 7;

Syntax

Suppose a number 'x'. Syntax would be:

Example

return_type rint(data_type x);

Note: The return_type can be float, double or long double.

Parameter

x : The value that can be either float or double.

Return value

It returns the rounded value of x.

Example 1

Let's see a simple example.

Example

#include <iostream>
#include<math.h>
#include <cfenv>
using namespace std;
int main()
{
    float x=9.9;
    std::cout << "Value of x is :" <<x<< std::endl;
    std::cout << "Rounding to nearest,value is :" <<rint(x)<< std::endl;
   fesetround(FE_UPWARD);
    std::cout << "Rounding to upward,value is :" <<rint(x)<< std::endl;
    fesetround(FE_DOWNWARD);
    std::cout << "Rounding to downward,value is :" <<rint(x)<< std::endl;
    return 0;
}

Output:

Output

Value of x is :9.9
Rounding to nearest,value is :10
Rounding to upward,value is :10
Rounding to downward,value is :9

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