Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
nearbyint() function
3.
Syntax
3.1.
Example
4.
Parameter
5.
Return Value
6.
Examples
6.1.
Example 1
6.2.
Example 2
6.3.
Example 3
7.
Frequently Asked Questions
7.1.
What is the Standard Template Library?
7.2.
What function rounds off the input to get the closest integral value?
7.3.
In which header file the nearbyint() function is defined?
7.4.
What is the return value of the nearbyint() function?
8.
Conclusion
Last Updated: Mar 27, 2024

nearbyint() function in C++

Author Nagendra
1 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

C++ is one of the most extensively utilised programming languages for competitive programming. Learning C++ reinforces essential computer science principles while also providing access to a wide range of professional options. 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. It's a container class library with algorithms and iterators. It is a parameterized library since it is a generic library.
The STL had a predefined function for rounding off values to the nearest integer value. Using the current rounding mechanism, nearbyint() function converts the given value to a nearby integral value. The article explains about the nearbyint() function.
Let's have a look at the nearbyint() function in more detail. Also see, Literals in C, Fibonacci Series in C++

 

nearbyint() function

The nearbyint() function is a built-in C++ STL function specified in the <cmath> header file. The nearbyint() function is used to obtain the round integral value specified in the input. The round method is described by fegetround(), and the function rounds off the input to get the closest integral value. As parameters, this function accepts float, double, and large double type values.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax

return_type nearbyint(data_type x);  

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);

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);

Output

3

Input

nearbyint(12.23);

Output

12

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;
}

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

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;
}

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

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;
}

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

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 problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. 

Happy Coding!!

Live masterclass