C++ std::nearbyint() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

In C++, std::nearbyint(x) rounds to a whole number following the current rounding mode of the program. In the default mode it rounds to the nearest number, and when a value is exactly halfway it goes to the even neighbour. So 2.5 becomes 2 and 3.5 becomes 4.

This way of splitting halves is called "banker's rounding". It stops results from drifting up in long sums. You can change the mode with std::fesetround() from <cfenv>. It is the same as rint(), except that it never raises the "inexact" floating-point flag.

std::nearbyint() is declared in <cmath> and is part of the group Rounding. Rounding functions turn a fractional number into a whole number. They differ in which way they go for negative numbers and for ties such as 2.5.

Syntax

Include <cmath> and call std::nearbyint. Start with these lines.

C++
double a = std::nearbyint(2.5);    // double version
float  b = std::nearbyint(2.5f);   // float version
CallWhat it does
std::nearbyint(2.5)Passes a double and returns a double. Here the result is 2.
std::nearbyint(2.5f)Passes a float, so the float overload runs and returns a float.
std::nearbyint(3)An integer is converted to double. Returns 3 as a double.

Write std::nearbyint so the C++ overloads for float, double, and long double are used.

Library declarations
C++
float       nearbyint(float x);
double      nearbyint(double x);
long double nearbyint(long double x);
double      nearbyint(Integer x);   // for integer types

Parameters

ParameterDescription
xRequired. The number to round. It can be float, double, long double, or an integer type (converted to double).

Return Value

Return ValueDescription
Rounded valueA whole number as a floating-point type, chosen by the current rounding mode.
xIf x is already whole, inf, or NaN, it is returned as it is.
No exceptionUnlike rint(), it does not raise FE_INEXACT.

C++ Examples

Example 1 — Round Several Numbers

Round some numbers. The halves go to the even neighbour in the default mode.

C++
#include <cmath>
#include <iostream>

int main() {
    double values[] = {2.4, 2.5, 3.5, -2.5};

    for (double v : values) {
        std::cout << "nearbyint(" << v << ") = " << std::nearbyint(v) << '\n';
    }
    return 0;
}

How It Works

  1. The loop gives each number in the array to std::nearbyint(), which uses the default mode "to nearest".
  2. 2.5 is exactly between 2 and 3. The even one, 2, is chosen. 3.5 is between 3 and 4, and again the even one, 4, wins.
  3. -2.5 follows the same rule and goes to -2, the even neighbour. 2.4 is not a half, so it simply goes to 2.

Example 2 — Change the Rounding Mode

Use std::fesetround() from <cfenv> to change the mode, and see how nearbyint() follows it.

C++
#include <cfenv>
#include <cmath>
#include <iostream>

int main() {
    volatile double a = 2.5;
    volatile double b = -2.5;

    struct Mode { const char* name; int value; };
    Mode modes[] = {
        {"to nearest", FE_TONEAREST},
        {"upward", FE_UPWARD},
        {"downward", FE_DOWNWARD},
        {"toward zero", FE_TOWARDZERO}
    };

    for (const Mode& m : modes) {
        std::fesetround(m.value);
        std::cout << m.name << ": " << std::nearbyint(a)
                  << ", " << std::nearbyint(b) << '\n';
    }
    std::fesetround(FE_TONEAREST);
    return 0;
}

How It Works

  1. Each Mode holds a name and a constant such as FE_UPWARD from <cfenv>. fesetround() switches the program to that mode. The volatile variables stop the compiler from working out the answer early, so the mode is really used.
  2. "To nearest" gives 2 and -2, the even neighbours. "Upward" gives 3 and -2, both the bigger number. "Downward" gives 2 and -3.
  3. "Toward zero" cuts the halves and gives 2 and -2. The last line puts the mode back to the default, which is a good habit.

Example 3 — Why Banker's Rounding Helps in Sums

Add up four values that all end in .5. Normal half-up rounding drifts upward, while nearbyint() stays close to the real sum.

C++
#include <cmath>
#include <iostream>

int main() {
    double values[] = {0.5, 1.5, 2.5, 3.5};
    double exact = 0, byRound = 0, byNearby = 0;

    for (double v : values) {
        exact += v;
        byRound += std::round(v);
        byNearby += std::nearbyint(v);
    }
    std::cout << "Exact sum:      " << exact << '\n';
    std::cout << "round sum:      " << byRound << '\n';
    std::cout << "nearbyint sum:  " << byNearby << '\n';
    return 0;
}

How It Works

  1. The three variables add the original numbers, the round() results, and the nearbyint() results.
  2. round() turns the four halves into 1, 2, 3, and 4. They add up to 10, which is too high by 2.
  3. nearbyint() gives 0, 2, 2, and 4, which add up to 8. Sometimes up and sometimes down, so the errors cancel out.

Frequently Asked Questions

std::nearbyint(x) rounds x to a whole number using the current rounding mode. The default mode rounds to the nearest, and halves go to the even number, so nearbyint(2.5) is 2.
The rounded value as a floating-point number. The result depends on the rounding mode set with std::fesetround(), and no inexact exception is raised.
They give the same rounded value. The difference is that rint() may raise the "inexact" floating-point exception when the result differs from the input, and nearbyint() never does.
The default rounding mode picks the even number when a value is exactly halfway. 2 is even and 3 is odd, so 2.5 goes to 2. 3.5 goes to 4.
Include <cmath> and write std::nearbyint(). That always works and picks the C++ overloads. The global name ::nearbyint often works too, but the standard does not promise it, and it may only take a double.

Next: nextafter()

Keep going through the <cmath> functions. The next representable number after x, toward y.

nextafter() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful