In Java, Math.nextUp(d) returns the smallest number the computer can store that is bigger than d. Math.nextUp(1.0) is 1.0000000000000002; nothing in between can be held in a double.
Think of stored doubles as marks on a ruler. nextUp moves to the next mark on the right. It is useful for "strictly greater than" limits, for stepping through neighbouring values, and for seeing how far apart the marks really are.
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.nextUp(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass a double or a float; the neighbour above comes back in the same type.
Java
double a = Math.nextUp(1.0); // 1.0000000000000002
double b = Math.nextUp(0.0); // 4.9E-324 (Double.MIN_VALUE)
float c = Math.nextUp(1.0f); // 1.0000001
double min = Math.nextUp(limit); // smallest value > limit
Call
What it does
Math.nextUp(100.0)
Returns 100.00000000000001, the first double above 100.
Math.nextUp(Double.MAX_VALUE)
Returns Infinity.
Math.nextUp(-Double.MIN_VALUE)
Returns -0.0 (negative zero).
Math.nextUp(x)
Same as Math.nextAfter(x, Double.POSITIVE_INFINITY), but shorter and possibly faster.
Math.nextUp(x) - x equals Math.ulp(x) for positive finite values below Double.MAX_VALUE: the step size and the gap are the same thing.
Library declarations
Java
// java.lang.Math
public static double nextUp(double d)
public static float nextUp(float f)
Parameters
Parameter
Description
d / f
The starting value.
Return Value
Return Value
Description
double / float
The neighbour just above the argument. NaN stays NaN, Infinity stays Infinity, and both zeros give Double.MIN_VALUE (or Float.MIN_VALUE).
One mark to the right of 1.0. The gap above 1.0 is 2-52, about 2.2E-16, so the neighbour prints as 1.0000000000000002.
Both zeros step to the same place. The smallest positive double, 4.9E-324 (Double.MIN_VALUE), is the first mark above 0.0 and above -0.0. Coming up from the tiniest negative value lands on -0.0.
Running off the top. Nothing finite is bigger than Double.MAX_VALUE, so the next step is Infinity; NaN has no neighbours and is returned as is.
Example 2 — Turn "Strictly Greater Than" Into an Inclusive Limit
An auction only accepts bids strictly above 100.0, but the range checker uses inclusive limits. nextUp gives the right lower limit.
First double above 100.0: 100.00000000000001
Bid 100.0 allowed? false
Bid 100.5 allowed? true
Smallest positive double: 4.9E-324
Same as Double.MIN_VALUE? true
How It Works
The first value over the line. Near 100 the gap between doubles is about 1.4E-14, so Math.nextUp(100.0) is 100.00000000000001, and x >= firstAbove means exactly x > 100.0.
The boundary is excluded. A bid of exactly 100.0 is below the new lower limit, so it is rejected, while 100.5 is accepted.
"Greater than zero" works the same way.Math.nextUp(0.0) is the smallest positive double, identical to Double.MIN_VALUE, so it is the inclusive lower limit for "positive".
Example 3 — Step Through Neighbours: double vs float
Take two steps up from 1.0, compare with the float spacing, and look at the gaps around 1e16.
Java
public class MathNextUpSteps {
public static void main(String[] args) {
double d = 1.0;
for (int i = 1; i <= 2; i++) {
d = Math.nextUp(d);
System.out.println("double step " + i + ": " + d);
}
float f = Math.nextUp(1.0f);
System.out.println("float step 1: " + f);
System.out.println("float gap == ulp? " + (f - 1.0f == Math.ulp(1.0f)));
System.out.println("after 1e16: " + Math.nextUp(1e16));
}
}
Output
double step 1: 1.0000000000000002
double step 2: 1.0000000000000004
float step 1: 1.0000001
float gap == ulp? true
after 1e16: 1.0000000000000002E16
How It Works
Equal-sized steps near 1.0. Each nextUp adds the same tiny gap, so the last digits go ...02, then ...04: there is no double in between.
float marks are wider apart. A float keeps about 7 digits instead of 16, so its next value above 1.0 is 1.0000001; that step is exactly Math.ulp(1.0f), so the comparison prints true.
Big numbers, big steps. At 1e16 the next stored double is 10000000000000002, two whole units away, printed in E notation as 1.0000000000000002E16.
Frequently Asked Questions
It returns the smallest float or double that is greater than the argument, one tiny step toward positive infinity. Math.nextUp(1.0) is 1.0000000000000002.
It is 4.9E-324, which is Double.MIN_VALUE, the smallest positive double. Math.nextUp(-0.0) gives the same value.
Call Math.nextUp(x). This is useful for turning a strict limit such as x greater than 100.0 into an inclusive one: x greater than or equal to Math.nextUp(100.0).
It returns Infinity, because no finite double is larger than Double.MAX_VALUE. Infinity itself stays Infinity, and NaN stays NaN.
Java 6 (JDK 1.6). Its partner Math.nextDown was added later, in Java 8.