In C++, std::nexttoward(x, y) finds the neighbour of x in the direction of y. It works just like std::nextafter(), with one difference: y is always a long double.
That makes a difference when x is a float or double. The direction is decided by comparing with the full long double, so a target that is not exactly representable as a double still gives the right direction. The result has the type of x.
std::nexttoward() 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::nexttoward. Start with these lines.
C++
double d = std::nexttoward(1.0, 2.0L); // 1.0000000000000002
float f = std::nexttoward(1.0f, 2.0L); // 1.00000012f
double e = std::nexttoward(1.0, 0.0L); // 0.99999999999999989
Call
What it does
std::nexttoward(1.0, 2.0L)
Moves a double up one step. Returns 1.0000000000000002.
std::nexttoward(1.0f, 2.0L)
Moves a float up one step. The step is bigger: 1.00000012.
std::nexttoward(1.0, 0.0L)
Moves a double down one step. Returns 0.99999999999999989.
Write L after a decimal to make a long double literal, as in 2.0L. A plain double is also accepted and converted.
Library declarations
C++
float nexttoward(float x, long double y);
double nexttoward(double x, long double y);
long double nexttoward(long double x, long double y);
double nexttoward(Integer x, long double y); // for integer types
Parameters
Parameter
Description
x
Required. The starting number. It can be float, double, or long double.
y
Required. The direction, as a long double. Only the comparison of y with x matters.
Return Value
Return Value
Description
Next value
The closest representable number after x in the direction of y, in the type of x.
y
If x equals y, the result is y converted to the type of x.
NaN / inf
NaN if an argument is NaN. Stepping past the largest value gives infinity.
C++ Examples
Example 1 — Float and Double Neighbours of 1.0
Step from 1.0 in both types. The float step is much bigger, because a float has fewer digits.
C++
#include <cmath>
#include <iomanip>
#include <iostream>
int main() {
float f = std::nexttoward(1.0f, 2.0L);
double d = std::nexttoward(1.0, 2.0L);
std::cout << std::setprecision(17);
std::cout << "double: " << d << '\n';
std::cout << std::setprecision(9);
std::cout << "float: " << f << '\n';
return 0;
}
Output
double: 1.0000000000000002
float: 1.00000012
How It Works
The target 2.0L is a long double. Both calls only use it to decide that the direction is up.
The double neighbour of 1.0 is 1.0000000000000002, which is a step of about 2.2e-16.
The float neighbour is 1.00000012, a step of about 1.2e-7. The type of the first argument decides the size of the step.
Example 2 — Walk Upward Step by Step
Start at 1.0f and take three steps toward 2. Each step moves to the next float.
C++
#include <cmath>
#include <iomanip>
#include <iostream>
int main() {
float value = 1.0f;
std::cout << std::setprecision(9);
for (int step = 1; step <= 3; ++step) {
value = std::nexttoward(value, 2.0L);
std::cout << "step " << step << ": " << value << '\n';
}
return 0;
}
The variable value is a float, so each call moves it to the next float in the upward direction.
The values are 1.00000012, 1.00000024, and 1.00000036. They grow by the same tiny step each time.
setprecision(9) is the right number of digits for a float. With the default six digits, all four numbers would just print as 1.
Example 3 — Count the Steps Between Two Floats
How many representable floats are there from 1.0 up to a target? Walk with nexttoward() and count.
C++
#include <cmath>
#include <iostream>
#include <limits>
int main() {
float eps = std::numeric_limits<float>::epsilon();
float target = 1.0f + 8 * eps;
float value = 1.0f;
int steps = 0;
while (value < target) {
value = std::nexttoward(value, target);
++steps;
}
std::cout << "Steps from 1 to the target: " << steps << '\n';
std::cout << "Reached the target: " << (value == target) << '\n';
return 0;
}
Output
Steps from 1 to the target: 8
Reached the target: 1
How It Works
The float epsilon is the gap between 1.0f and its next neighbour. The target is exactly 8 of those gaps above 1.0f.
The loop moves one neighbour at a time with nexttoward(value, target) and counts the steps. The float target is converted to a long double without any loss.
After exactly 8 steps value is equal to target, so the loop stops and the last line prints 1.
Frequently Asked Questions
std::nexttoward(x, y) returns the closest number after x in the type of x, moving toward y. The only difference from nextafter() is that y is a long double.
The next representable value after x in the direction of y, in the type of x. If x equals y, the result is y converted to the type of x.
Only the type of the second argument. nextafter() takes y in the same type as x, and nexttoward() takes y as a long double, so the direction is decided with more precision. The result has the type of x.
A float has about 7 digits and a double has about 16. With fewer digits the numbers are further apart, so a step from 1.0f is about 1.2e-7 while a step from 1.0 is about 2.2e-16.
Include <cmath> and write std::nexttoward(). That always works and picks the C++ overloads. The global name ::nexttoward often works too, but the standard does not promise it, and it may only take a double.