The nearbyint() function is defined in cmath header file. This function round off the given value to a nearby integral value by using the current rounding method. The current rounding method is described by fegetround.
Syntax:
float nearbyint(float x); or, double nearbyint(double x); or, long double nearbyint(long double x);
Parameters: This function accepts single parameter x which contains floating point value.
Return Value: It returns the rounded value of x to a nearby integral value by using the rounding method.
Below programs illustrate the nearbyint() function in C++:
Program 1:
// C++ program to demonstrate // example of nearbyint() function. #include <bits/stdc++.h> using namespace std; int main() { float x = 2.7; cout << "Value of x = " << x << endl; cout << "Round off value of x = " << nearbyint(x); return 0; } |
Value of x = 2.7 Round off value of x = 3
Program 2:
// C++ program to demonstrate // example of nearbyint() function. #include <bits/stdc++.h> using namespace std; int main() { double x = 3.2; cout << "Value of x = " << x << endl; cout << "Round off value of x = " << nearbyint(x); return 0; } |
Value of x = 3.2 Round off value of x = 3
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommended Posts:
- What happens when a virtual function is called inside a non-virtual function in C++
- Function Overloading vs Function Overriding in C++
- Difference between Virtual function and Pure virtual function in C++
- How to call some function before main() function in C++?
- How to call function within function in C or C++
- Difference between user defined function and library function in C/C++
- Difference between virtual function and inline function in C++
- Can we call an undeclared function in C++?
- Can we use function on left side of an expression in C and C++?
- Declare a C/C++ function returning pointer to array of integer pointers
- Function overloading and return type
- Function Pointer in C
- Default arguments and virtual function
- Function overloading and const keyword
- How to measure time taken by a function in C?
- Comparator function of qsort() in C
- Can we access private data members of a class without using a member or a friend function?
- Function Overloading and float in C++
- Extra brackets with function names in C/C++
- Function Interposition in C with an example of user defined malloc()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

