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)
Call
What 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
Parameter
Description
x
Required. The starting number. It can be float, double, long double, or an integer type (converted to double).
y
Required. The direction. Only the comparison of y with x matters, not how far y is.
Return Value
Return Value
Description
Next value
The closest representable number after x in the direction of y.
y
If x equals y, the result is y.
NaN / inf
NaN 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.
The first neighbour above 0 is the smallest positive double there is, about 4.94066e-324.
That number is exactly std::numeric_limits<double>::denorm_min(), so the comparison prints 1.
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.