In Java, Math.nextDown(d) returns the largest number the computer can store that is still smaller than d. Math.nextDown(1.0) is 0.9999999999999999: there is no double between that value and 1.0.
It is the mirror image of nextUp. Use it when you need a value "just below" a limit, for example to turn an exclusive upper bound (x < 1.0) into an inclusive one (x <= Math.nextDown(1.0)).
A static method of the java.lang.Math class, added in Java 8 (JDK 1.8). Call it on the class name, as in Math.nextDown(...). No import is needed, because classes in java.lang are always visible. It was added later than nextUp, which came in Java 6.
Syntax
Pass a double or a float; you get the neighbour below it in the same type.
Java
double a = Math.nextDown(1.0); // 0.9999999999999999
double b = Math.nextDown(0.0); // -4.9E-324
float c = Math.nextDown(1.0f); // 0.99999994
double cap = Math.min(x, Math.nextDown(1.0)); // keep x below 1.0
Call
What it does
Math.nextDown(1.0)
The largest double smaller than 1.0.
Math.nextDown(-1.0)
Returns -1.0000000000000002 (further from zero).
Math.nextDown(Double.POSITIVE_INFINITY)
Returns Double.MAX_VALUE.
Math.nextDown(x)
Same as Math.nextAfter(x, Double.NEGATIVE_INFINITY).
For negative numbers, "down" means further from zero, so the result has a bigger size. For positive numbers it moves toward zero.
Library declarations
Java
// java.lang.Math (Java 8+)
public static double nextDown(double d)
public static float nextDown(float f)
Parameters
Parameter
Description
d / f
The starting value.
Return Value
Return Value
Description
double / float
The neighbour just below the argument. NaN stays NaN, -Infinity stays -Infinity, and both zeros give -Double.MIN_VALUE (-4.9E-324), or -Float.MIN_VALUE for float.
Just under one. The gap below 1.0 is about 1.1E-16, so the neighbour prints as sixteen 9s after the decimal point.
Around zero. Below 0.0 the first stored value is the tiniest negative double, -4.9E-324; one step down from the tiniest positive double lands back on 0.0.
Infinities and NaN. The value below +Infinity is the largest finite double, Double.MAX_VALUE. Nothing is below -Infinity, so it stays, and NaN stays NaN.
Example 2 — Keep a Progress Bar Index Inside the Array
A bar has 10 segments, indexes 0 to 9. A progress of exactly 1.0 would give index 10, one past the end; capping at nextDown(1.0) fixes it.
Java
public class MathNextDownProgress {
public static void main(String[] args) {
double almostOne = Math.nextDown(1.0);
double[] progress = {0.0, 0.42, 0.95, 1.0};
for (double p : progress) {
int raw = (int) (p * 10);
int safe = (int) (Math.min(p, almostOne) * 10);
System.out.println(p + " -> segment " + safe + " (without nextDown: " + raw + ")");
}
}
}
Turning "less than 1" into a cap. Valid fractions must be strictly below 1.0. Math.nextDown(1.0) is the largest such value, so Math.min(p, almostOne) keeps every input in range.
Normal values are untouched. 0.42 and 0.95 are already below the cap, so both versions agree: 4.2 and 9.5 are cut to segments 4 and 9.
Why 1.0 now gives 9. 0.9999999999999999 × 10 rounds to 9.999999999999998, not 10.0, so the cast keeps 9, while the raw 1.0 × 10 gives 10, an index that would throw ArrayIndexOutOfBoundsException.
Example 3 — Why nextDown(x) Is Not Always x - ulp(x)
Compare both approaches at 1.0 (a power of two) and at 3.0, then try a negative number and a float.
A smaller gap below a power of two.Math.ulp(1.0) is the gap above 1.0 (2.2E-16). Below 1.0 the stored values are twice as dense, so subtracting a full ulp jumps two neighbours down to ...98.
Same answer away from powers of two. 3.0 sits in the middle of the 2-to-4 range, where the gap is the same on both sides, so both lines print 2.9999999999999996.
Negative and float inputs. Below -1.0 means further from zero, giving -1.0000000000000002. The float overload uses the much wider float spacing, so its neighbour is 0.99999994.
Frequently Asked Questions
It returns the largest float or double that is smaller than the argument, one tiny step toward negative infinity. Math.nextDown(1.0) is 0.9999999999999999.
Java 8. On Java 6 or 7, use Math.nextAfter(x, Double.NEGATIVE_INFINITY), which gives the same result.
Call Math.nextDown(1.0). It returns 0.9999999999999999, and no double lies between that value and 1.0.
Not always. At exact powers of two, such as 1.0, the gap below the number is half the gap above it, so x - Math.ulp(x) skips one value. nextDown always returns the true neighbour.
It returns -4.9E-324, which is -Double.MIN_VALUE, the negative double closest to zero.