C++ nearbyint()

The nearbyint() function in C++ rounds the argument to an integral value using the current rounding mode.

The nearbyint() function in C++ rounds the argument to an integral value using the current rounding mode. The current rounding mode is determined by the function fesetround(). The nearbyint() function is similar to rint(), except that it does not raise FE_INEXACT exceptions as rint().

An FE_INEXACT exception is a floating point exception that occurs when the result of an operation is not represented exactly due to rounding or gradual underflow.


nearbyint() prototype [As of C++ 11 standard]

double nearbyint(double x);
float nearbyint(float x);
long double nearbyint(long double x);
double nearbyint(T x); // For integral type

The nearbyint() function takes a single argument and returns a value of type double, float or long double type. This function is defined in <cmath> header file.


nearbyint() Parameters

The nearbyint() function takes a single argument value to round.


nearbyint() Return value

The nearbyint() function rounds the argument x to an integral value, using the rounding direction specified by fegetround() and returns the value. By default, the rounding direction is set to 'to-nearest'. The rounding direction can be set to other values using fesetround() function.


Example 1: How nearbyint() works in C++?

#include <iostream>
#include <cmath>
#include <cfenv>
using namespace std;

int main()
{
    // by default, rounding direction is to-nearest i.e. fesetround(FE_TONEAREST)
    double x = 11.87, result;
    result = nearbyint(x);
    cout << "Rounding to-nearest (" << x << ") = " << result << endl;
    
    // upper value is taken for mid-way values
    x = 11.5;
    result = nearbyint(x);
    cout << "Rounding to-nearest (" << x << ") = " << result << endl;

    // setting rounding direction to DOWNWARD
    fesetround(FE_DOWNWARD);
    x = 17.87;
    result = nearbyint(x);
    cout << "Rounding downward (" << x << ") = " << nearbyint(x) << endl;
    
    // setting rounding direction to UPWARD
    x = 33.34;
    fesetround(FE_UPWARD);
    result = nearbyint(x);
    cout << "Rounding upward (" << x << ") = " << result << endl;
    
    return 0;
}

When you run the program, the output will be:

Rounding to-nearest (11.87) = 12 Rounding to-nearest (11.5) = 12 Rounding downward (17.87) = 17 Rounding upward (33.3401) = 34

Example 2: nearbyint() function for integral types

#include <iostream>
#include <cmath>
#include <cfenv>
using namespace std;

int main()
{
    int x = 15;
    double result;
    
    // setting rounding direction to DOWNWARD
    fesetround(FE_DOWNWARD);
    result = nearbyint(x);
    cout << "Rounding downward (" << x << ") = " << result << endl;

    return 0;
}

When you run the program, the output will be:

Rounding downward (15) = 15

For integral values, applying the nearbyint function returns the same value as the input. So it is not commonly used for integral values in practice.

Did you find this article helpful?