C++ std::nextafter() Function

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

Definition

In C++, std::nextafter(x, y) finds the neighbour of a number. A double cannot store every number, only a huge but limited set. nextafter() returns the next one in that set, going from x toward y.

The size of the step depends on the number. Near 1.0 the neighbours are about 2.2e-16 apart, and near 1000 they are about 1.1e-13 apart. This function is used for testing, for open intervals such as "just below 1", and to learn how decimals are stored.

std::nextafter() is declared in <cmath> and is part of the group Remainder, scale, and compare. These functions split a number, scale it by a power of two, find its remainder, or pick between two numbers. They work directly on the binary form of a double.

Syntax

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

C++
double up   = std::nextafter(1.0, 2.0);    // just above 1
double down = std::nextafter(1.0, 0.0);    // just below 1
double same = std::nextafter(1.0, 1.0);    // 1 (x equals y)
CallWhat it does
std::nextafter(1.0, 2.0)Moves up one step. Returns 1.0000000000000002.
std::nextafter(1.0, 0.0)Moves down one step. Returns 0.99999999999999989.
std::nextafter(1.0, 1.0)x equals y, so it returns y, which is 1.

Print the result with std::setprecision(17). With the default six digits the neighbour looks the same as the original.

Library declarations
C++
float       nextafter(float x, float y);
double      nextafter(double x, double y);
long double nextafter(long double x, long double y);
// plus overloads for mixed and integer arguments

Parameters

ParameterDescription
xRequired. The starting number. It can be float, double, long double, or an integer type (converted to double).
yRequired. The direction. Only the comparison of y with x matters, not how far y is.

Return Value

Return ValueDescription
Next valueThe closest representable number after x in the direction of y.
yIf x equals y, the result is y.
NaN / infNaN if an argument is NaN. Stepping past the largest double gives inf.

C++ Examples

Example 1 — The Neighbours of 1.0

Step up, step down, and stay in place. The results are printed with 17 digits so you can see the tiny difference.

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

int main() {
    std::cout << std::setprecision(17);

    std::cout << "up:   " << std::nextafter(1.0, 2.0) << '\n';
    std::cout << "down: " << std::nextafter(1.0, 0.0) << '\n';
    std::cout << "same: " << std::nextafter(1.0, 1.0) << '\n';
    return 0;
}

How It Works

  1. std::setprecision(17) makes cout show 17 significant digits, enough to tell two neighbouring doubles apart.
  2. The next number above 1.0 is 1.0000000000000002. The next one below is 0.99999999999999989, which is closer, because the gap below 1 is half as big.
  3. When x and y are equal there is nowhere to go, so the function returns 1 itself.

Example 2 — The Gap Between Neighbours Depends on the Size

Measure the distance to the next number for 1.0 and for 1000.0. For 1.0 it is the famous machine epsilon.

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

int main() {
    double gap1 = std::nextafter(1.0, 2.0) - 1.0;
    double gap2 = std::nextafter(1000.0, 2000.0) - 1000.0;

    std::cout << "gap near 1:    " << gap1 << '\n';
    std::cout << "gap near 1000: " << gap2 << '\n';
    std::cout << "epsilon matches: "
              << (gap1 == std::numeric_limits<double>::epsilon()) << '\n';
    return 0;
}

How It Works

  1. Each gap is the next number minus the number itself. This is how far apart the neighbours are at that place.
  2. Near 1.0 the gap is 2.22045e-16. Near 1000 it is much larger, 1.13687e-13, because bigger numbers have less room for decimals.
  3. The gap near 1.0 equals std::numeric_limits<double>::epsilon(), so the last line prints 1.

Example 3 — The Smallest Step from Zero and the Step Past the Largest

Go one step from 0 toward 1 and one step past the largest double.

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

int main() {
    double tiny = std::nextafter(0.0, 1.0);
    double huge = std::numeric_limits<double>::max();
    double inf = std::numeric_limits<double>::infinity();

    std::cout << "nextafter(0, 1) = " << tiny << '\n';
    std::cout << "equals denorm_min: "
              << (tiny == std::numeric_limits<double>::denorm_min()) << '\n';
    std::cout << "nextafter(max, inf) = " << std::nextafter(huge, inf) << '\n';
    return 0;
}

How It Works

  1. The first neighbour above 0 is the smallest positive double there is, about 4.94066e-324.
  2. That number is exactly std::numeric_limits<double>::denorm_min(), so the comparison prints 1.
  3. The largest finite double has no neighbour above it that is finite, so one step toward infinity gives inf.

Frequently Asked Questions

std::nextafter(x, y) returns the closest number after x that a double can store, moving toward y. If x equals y, it returns y.
The next representable value after x in the direction of y. If x equals y the result is y, and if either argument is NaN the result is NaN.
It returns y, which is the same as x. There is no step to take.
Adding 1e-16 to 1000.0 changes nothing, because 1e-16 is smaller than the gap. nextafter() always moves exactly one representable step, whatever the size of the number.
Include <cmath> and write std::nextafter(). That always works and picks the C++ overloads. The global name ::nextafter often works too, but the standard does not promise it, and it may only take a double.

Next: nexttoward()

Keep going through the <cmath> functions. Like nextafter(), but the direction is a long double.

nexttoward() 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.

15 people found this page helpful