Java Math nextDown() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

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
CallWhat 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

ParameterDescription
d / fThe starting value.

Return Value

Return ValueDescription
double / floatThe 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.

Compare with Similar Methods

Ways to move to a neighbouring value:

MethodHow it differs
nextUp(x)The neighbour above, toward positive infinity. Java 6+.
nextAfter(x, dir)Either direction, chosen by the second argument. Java 6+.
x - Math.ulp(x)Usually the same, but skips a value at powers of two.
floor(x)Rounds down to a whole number, a much bigger jump.

Java Examples

Example 1 — The Neighbour Below, Including the Edge Cases

Step down from ordinary values, from zero, and from the infinities.

Java
public class MathNextDownBasics {
    public static void main(String[] args) {
        System.out.println("nextDown(1.0)       = " + Math.nextDown(1.0));
        System.out.println("nextDown(0.0)       = " + Math.nextDown(0.0));
        System.out.println("nextDown(MIN_VALUE) = " + Math.nextDown(Double.MIN_VALUE));
        System.out.println("nextDown(Infinity)  = " + Math.nextDown(Double.POSITIVE_INFINITY));
        System.out.println("nextDown(-Infinity) = " + Math.nextDown(Double.NEGATIVE_INFINITY));
        System.out.println("nextDown(NaN)       = " + Math.nextDown(Double.NaN));
    }
}

How It Works

  1. Just under one. The gap below 1.0 is about 1.1E-16, so the neighbour prints as sixteen 9s after the decimal point.
  2. 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.
  3. 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 + ")");
        }
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Java
public class MathNextDownVsUlp {
    public static void main(String[] args) {
        System.out.println("nextDown(1.0)  = " + Math.nextDown(1.0));
        System.out.println("1.0 - ulp(1.0) = " + (1.0 - Math.ulp(1.0)));
        System.out.println("nextDown(3.0)  = " + Math.nextDown(3.0));
        System.out.println("3.0 - ulp(3.0) = " + (3.0 - Math.ulp(3.0)));
        System.out.println("nextDown(-1.0) = " + Math.nextDown(-1.0));
        System.out.println("nextDown(1.0f) = " + Math.nextDown(1.0f));
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Next: nextUp()

The next larger float or double value.

nextUp() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful