Java Math nextUp() Method

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

Definition

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

ParameterDescription
d / fThe starting value.

Return Value

Return ValueDescription
double / floatThe neighbour just above the argument. NaN stays NaN, Infinity stays Infinity, and both zeros give Double.MIN_VALUE (or Float.MIN_VALUE).

Compare with Similar Methods

Neighbours and gaps:

MethodHow it differs
nextDown(x)The neighbour below, toward negative infinity. Java 8+.
nextAfter(x, dir)One step in a direction you choose.
ulp(x)The size of the gap instead of the next value.
ceil(x)Jumps up to the next whole number, not the next stored value.

Java Examples

Example 1 — The Next Double Above, From 1.0 to MAX_VALUE

Step up from an ordinary number, from both zeros, and from the extremes.

Java
public class MathNextUpBasics {
    public static void main(String[] args) {
        System.out.println("nextUp(1.0)        = " + Math.nextUp(1.0));
        System.out.println("nextUp(0.0)        = " + Math.nextUp(0.0));
        System.out.println("nextUp(-0.0)       = " + Math.nextUp(-0.0));
        System.out.println("nextUp(-MIN_VALUE) = " + Math.nextUp(-Double.MIN_VALUE));
        System.out.println("nextUp(MAX_VALUE)  = " + Math.nextUp(Double.MAX_VALUE));
        System.out.println("nextUp(NaN)        = " + Math.nextUp(Double.NaN));
    }
}

How It Works

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

Java
public class MathNextUpStrictlyAbove {
    static boolean inRange(double x, double min, double max) {
        return x >= min && x <= max;
    }

    public static void main(String[] args) {
        double firstAbove = Math.nextUp(100.0);
        System.out.println("First double above 100.0: " + firstAbove);
        System.out.println("Bid 100.0 allowed? " + inRange(100.0, firstAbove, 200.0));
        System.out.println("Bid 100.5 allowed? " + inRange(100.5, firstAbove, 200.0));

        double positive = Math.nextUp(0.0);
        System.out.println("Smallest positive double: " + positive);
        System.out.println("Same as Double.MIN_VALUE? " + (positive == Double.MIN_VALUE));
    }
}

How It Works

  1. 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.
  2. 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.
  3. "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));
    }
}

How It Works

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

Next: pow()

Raise a number to a power: a to the b.

pow() 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.

11 people found this page helpful