Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

functions
c++
communitycreator

What is the rint() function in C++?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

In C++, the rint() function is used to round the argument x to an integral value. The argument is rounded to an integral value by making use of the current rounding mode.

Note: The current rounding mode is determined, using the fesetround() function.

Syntax

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

Parameter value

The rint() function takes a single parameter value x, which represents the value taht needs to be rounded to an integral value.

Return value

The rint() function returns a value of either type double, float, or long double.

By default, the rounding is done “to the nearest” integer.

Code example

#include <iostream>
#include <cmath>
#include <cfenv>
using namespace std;
int main()
{
// creatinn our variables
double x = 19.6;
double result;
// setting rounding direction to DOWNWARD using the fesetround function
fesetround(FE_DOWNWARD);
// using the rint() function
result = rint(x);
cout << "Rounding (" << x << ") downward = " << result << endl;
return 0;
}
Implementing the "rint()" function

Code explanation

  • Lines 9–10: We create the variables x and result.
  • Line 13: We set the rounding direction to downward, using the fesetround() function.
  • Line 16: We implement the rint() function on the x variable and assign the output to the result variable.
  • Line 17: We print the result variable.

RELATED TAGS

functions
c++
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring