Math Nearbyint Function - C++ Programming Tutorial
C++ Course / Math Functions / Math Nearbyint Function

Math Nearbyint Function

BLUF: Mastering Math Nearbyint Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Math Nearbyint Function

C++ is renowned for its efficiency. Learn how Math Nearbyint Function enables low-level control and high-performance computing in the tutorial below.

C++ Math nearbyint

The function will approximate the provided value to the nearest whole number based on the current rounding method in place, which is defined by fegetround.

Syntax

Suppose a number is 'x'. Syntax would be:

Example

return_type nearbyint(data_type x);

Note: The return_type can be float, double or long double.

Parameter

x : The value can be float or double.

Return value

It provides the nearest whole number value for x by employing a specific rounding technique.

Example 1

Let's see a simple example.

Example

#include <iostream>
#include<math.h>
#include <cfenv>
using namespace std;
int main()
{
     float x=2.3;
    std::cout << "Value of x is:" <<x<< std::endl;
    cout<<"Rounding to nearby,value is :"<<nearbyint(x);
    return 0;
}

Output:

Output

Value of x is:2.3
Rounding to nearby,value is :2

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience