In Java, Math.nextAfter(start, direction) returns the closest number the computer can store next to start, on the side where direction lies. Math.nextAfter(1.0, 2.0) is 1.0000000000000002, the very next double above 1.0.
Doubles are not a smooth line: there is a tiny gap between each stored value and the next. nextAfter takes exactly one step across that gap, toward any target you choose. It combines nextUp and nextDown in one method.
A static method of the java.lang.Math class, added in Java 6 (JDK 1.6). Call it on the class name, as in Math.nextAfter(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass the value to start from and a value that shows which way to go.
Java
double up = Math.nextAfter(1.0, 2.0); // 1.0000000000000002
double down = Math.nextAfter(1.0, 0.0); // 0.9999999999999999
double same = Math.nextAfter(1.0, 1.0); // 1.0 (already there)
float f = Math.nextAfter(1.0f, 2.0); // 1.0000001 (float result)
Call
What it does
Math.nextAfter(x, Double.POSITIVE_INFINITY)
Same as Math.nextUp(x).
Math.nextAfter(x, Double.NEGATIVE_INFINITY)
Same as Math.nextDown(x).
Math.nextAfter(x, target)
One step from x toward target.
Math.nextAfter(0.0, -1.0)
Returns -4.9E-324, the negative double closest to zero.
The float version takes a float start but a double direction. The direction only says which way to go; the step and the result stay in float precision.
Library declarations
Java
// java.lang.Math
public static double nextAfter(double start, double direction)
public static float nextAfter(float start, double direction)
Parameters
Parameter
Description
start
The value to step away from.
direction
Any value that shows the way: larger than start means up, smaller means down. Only its position matters, not how far away it is.
Return Value
Return Value
Description
double / float
The neighbour of start toward direction. If the two compare equal, direction itself is returned. If either is NaN, the result is NaN.
Direction above or below. 2.0 is above 1.0, so the method returns the next double up; 0.0 is below, so it returns the next double down. How far away the direction is makes no difference.
Equal means no move. When start and direction are both 1.0, there is nowhere to go and 1.0 comes back.
Crossing zero and NaN. From 0.0 toward a negative value, the first stop is the tiniest negative double, -4.9E-324. A NaN direction has no "side", so the result is NaN.
Example 2 — Count How Many Doubles Lie Between Two Values
Walk from one value to another, one stored double at a time, and count the steps in both directions.
Java
public class MathNextAfterCount {
static int stepsBetween(double from, double to) {
int steps = 0;
for (double d = from; d != to; d = Math.nextAfter(d, to)) {
steps++;
}
return steps;
}
public static void main(String[] args) {
double end = 1.0000000000000009;
System.out.println("end = " + end);
System.out.println("1.0 up to end: " + stepsBetween(1.0, end) + " steps");
System.out.println("end down to 1.0: " + stepsBetween(end, 1.0) + " steps");
System.out.println("1.0 to 1.0: " + stepsBetween(1.0, 1.0) + " steps");
}
}
Output
end = 1.0000000000000009
1.0 up to end: 4 steps
end down to 1.0: 4 steps
1.0 to 1.0: 0 steps
How It Works
The literal lands on a stored double. The closest double to 1.0000000000000009 is 1.0 plus four gaps of 2.220446049250313E-16 (about 1.00000000000000089), so Java stores that value and prints it back as 1.0000000000000009.
One gap per loop pass. Each nextAfter(d, to) moves exactly one stored value toward to, so the loop runs 4 times going up and 4 times coming back down.
A loop that always ends. Because the target is a real double and nextAfter never jumps over it, d != to becomes false exactly on arrival; equal inputs give 0 steps.
Example 3 — Step Away From Zero, and the float Overload
Pair nextAfter with copySign to push any number outward, then try the float version.
Java
public class MathNextAfterAwayFromZero {
static double awayFromZero(double x) {
return Math.nextAfter(x, Math.copySign(Double.POSITIVE_INFINITY, x));
}
public static void main(String[] args) {
System.out.println("away(2.5) = " + awayFromZero(2.5));
System.out.println("away(-2.5) = " + awayFromZero(-2.5));
float f = Math.nextAfter(1.0f, 2.0);
System.out.println("float step = " + f);
System.out.println("float back = " + Math.nextAfter(f, 0.0));
}
}
An outward direction from copySign.Math.copySign(Double.POSITIVE_INFINITY, x) is +Infinity for positive x and -Infinity for negative x, so the step always moves away from zero.
Bigger numbers, bigger gaps. Between 2 and 4 the gap is 4.44E-16, twice the gap near 1.0, so the neighbours of 2.5 and -2.5 end in ...0004.
A float start keeps float steps.Math.nextAfter(1.0f, 2.0) returns a float, and the float gap at 1.0 is much wider (about 1.19E-7), giving 1.0000001; one step back toward 0.0 returns exactly 1.0.
Frequently Asked Questions
It returns the floating-point number right next to the first argument, in the direction of the second argument. Math.nextAfter(1.0, 2.0) is 1.0000000000000002.
It returns the second argument (direction). So Math.nextAfter(1.0, 1.0) is 1.0.
nextUp always steps toward positive infinity and nextDown always toward negative infinity. nextAfter lets you choose the direction with a second argument, so nextAfter(x, Double.POSITIVE_INFINITY) equals nextUp(x).
The direction is only used to decide which way to step, and a double can describe any float position. The start value, the step, and the result are all float.
If either the start or the direction is NaN, Math.nextAfter returns NaN.