nearbyint() function in C++


In this article we will be discussing the working, syntax and examples of nearbyint() function in C++ STL.

What is nearbyint()?

nearbyint() function is an inbuilt function in C++ STL, which is defined in <cmath> header file. nearbyint() function is used to get the round integral value as per the input.

The function rounds off the input to get a nearest integral value, the round method is described by fegetround.

This function accepts the float, double, and long double type values, as arguments.

Syntax

double nearbyint(double num);
float nearbyint(float num);
long double nearbyint(long double num);

Parameters

The function accepts following parameter(s) −

  • num − The value which is to be rounded off.

Return value

This function returns a rounded off value of num.

Example

Input

nearbyint(2.13);

Output

2

Input

nearbyint(3.4);

Output

3

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   float var = 3.4;
   cout<<"value of var is: " <<var<< endl;
   cout<<"value after round off is: "<<nearbyint(var);
   return 0;
}

Output

value of var is: 3.4
value after round off is: 3

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   float var = 7.9;
   cout<<"value of var is: " <<var<< endl;
   cout<<"value after round off is: "<<nearbyint(var);
   return 0;
}

Output

value of var is: 7.9
value after round off is: 8

Updated on: 17-Apr-2020

29 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements