Syntax
return_type nearbyint(data_type x);
You can also try this code with Online C++ Compiler
Run Code
Let's have a look over the implementation of the nearbyint() function.
Example
The following example briefs an overview about the implementation of the nearbyint() function.
double nearbyint(double x);
long double nearbyint(long double x);
float nearbyint(float x);
You can also try this code with Online C++ Compiler
Run Code
Parameter
This method has only one parameter, x, denoting the value to be rounded off.
Return Value
The function returns the rounded value of the parameterized number. To alter the return result of this method, we can utilise the fesetround() function.
It's time to learn more about the nearbyint() function by looking at several examples.
Also Read - C++ Interview Questions
Examples
Input
nearbyint(2.73);
You can also try this code with Online C++ Compiler
Run Code
Output
3
You can also try this code with Online C++ Compiler
Run Code
Input
nearbyint(12.23);
You can also try this code with Online C++ Compiler
Run Code
Output
12
You can also try this code with Online C++ Compiler
Run Code
Example 1
Consider the following code snippet :
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float f1 = 4.8;
double d1 = 3.3;
long double l1 = -8.9;
cout << "Number 1 = " <<f1 << endl;
cout << "Near by integer("<<f1<<") = " << nearbyint(f1) << endl;
cout << "Number 2 = " <<d1 << endl;
cout << "Near by integer("<<d1<<") = " << nearbyint(d1) << endl;
cout << "Number 3 = " <<l1 << endl;
cout << "Near by integer("<<l1<<") = " << nearbyint(l1) << endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
Number 1 = 4.8
Near by integer(4.8) = 5
Number 2 = 3.3
Near by integer(3.3) = 3
Number 3 = -8.9
Near by integer(-8.9) = -9
You can also try this code with Online C++ Compiler
Run Code
Explanation :
The nearbyint() function rounds all input values to the nearest integer.
Example 2
Consider the following code snippet :
#include <iostream>
#include <cmath>
#include <cfenv>
using namespace std;
int main()
{
float f1 = 4.8;
double d1 = 3.3;
long double l1 = -8.9;
fesetround(FE_UPWARD);
cout << "Number 1 = " <<f1 << endl;
cout << "Near by integer("<<f1<<") = " << nearbyint(f1) << endl;
cout << "Number 2 = " <<d1 << endl;
cout << "Near by integer("<<d1<<") = " << nearbyint(d1) << endl;
cout << "Number 3 = " <<l1 << endl;
cout << "Near by integer("<<l1<<") = " << nearbyint(l1) << endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
Number 1 = 4.8
Near by integer(4.8) = 5
Number 2 = 3.3
Near by integer(3.3) = 4
Number 3 = -8.9
Near by integer(-8.9) = -8
You can also try this code with Online C++ Compiler
Run Code
Explanation:
In the above code, to change the rounding mode to upward, we utilised the fesetround() function. The nearbyint() function returns the nearest upward value in this case. fesetround(FE_UPWARD) returns the nearest upward value.
Example 3
Consider the following code snippet :
#include <iostream>
#include <cmath>
#include <cfenv>
using namespace std;
int main()
{
float f1 = 4.8;
double d1 = 3.3;
long double l1 = -8.9;
fesetround(FE_DOWNWARD);
cout << "Number 1 = " <<f1 << endl;
cout << "Near by integer("<<f1<<") = " << nearbyint(f1) << endl;
cout << "Number 2 = " <<d1 << endl;
cout << "Near by integer("<<d1<<") = " << nearbyint(d1) << endl;
cout << "Number 3 = " <<l1 << endl;
cout << "Near by integer("<<l1<<") = " << nearbyint(l1) << endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
Number 1 = 4.8
Near by integer(4.8) = 4
Number 2 = 3.3
Near by integer(3.3) = 3
Number 3 = -8.9
Near by integer(-8.9) = -9
You can also try this code with Online C++ Compiler
Run Code
Explanation:
In the above code, to change the rounding mode to downward, we utilised the fesetround() function. The nearbyint() function returns the nearest downward value in this case. fesetround(FE_DOWNWARD) returns the nearest downward value.
Try and compile with online c++ compiler.
Must Read Dynamic Binding in C++
Frequently Asked Questions
What is the Standard Template Library?
The Standard Template Library is a set of template classes written in C++. that provide common data structures and functions, including lists, stacks, and arrays.
What function rounds off the input to get the closest integral value?
The fegetround() rounds off the input to get the closest integral value.
In which header file the nearbyint() function is defined?
The <cmath> header file defines the quick_exit() function.
What is the return value of the nearbyint() function?
The nearbyint() returns the rounded value of the parameterized number.
Conclusion
In this article, we have extensively discussed the nearbyint() function in C++. The article explains the details of the nearbyint() function in C++.
We hope that this blog has helped you enhance your knowledge regarding the nearbyint() function in C++ and if you would like to learn more, check out our articles on C++. You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problems, Interview experience, Coding interview questions, and the Ultimate guide path for interviews.
Do upvote our blog to help other ninjas grow.
Happy Coding!!