Java Math ulp() Method

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

Definition

In Java, Math.ulp(d) returns the gap between d and the next larger number the computer can store. "ulp" stands for unit in the last place. Math.ulp(1.0) is 2.220446049250313E-16: no double exists between 1.0 and 1.0 plus that gap.

The gap is not fixed. It grows with the size of the number: tiny near zero, 0.125 at 1e15, and a whole 2.0 at 1e16. That is why big doubles lose the fractional part, why a large long can change when stored in a double, and why ulp is a good yardstick for "close enough" comparisons.

A static method of the java.lang.Math class, added in Java 5 (JDK 1.5). Call it on the class name, as in Math.ulp(...). No import is needed, because classes in java.lang are always visible.

Picture it

Imagine a ruler whose marks get further apart the further you go from zero. A double can only sit exactly on a mark. ulp(x) is the distance from the mark at x to the next mark. Anything that falls between two marks is rounded to the nearest one.

Syntax

Pass a double or a float; the gap comes back in the same type and is always positive.

Java
double g1 = Math.ulp(1.0);      // 2.220446049250313E-16
double g2 = Math.ulp(1e16);     // 2.0
float g3 = Math.ulp(1.0f);      // 1.1920929E-7
boolean close = Math.abs(a - b) <= 4 * Math.ulp(a);
CallWhat it does
Math.ulp(100.0)Returns 1.4210854715202004E-14.
Math.ulp(-100.0)Same gap: the sign is ignored.
Math.ulp(0.0)Returns 4.9E-324 (Double.MIN_VALUE).
Math.abs(a - b) <= 4 * Math.ulp(a)"Equal within 4 steps" test for doubles.

An int argument widens to float, so Math.ulp(100) gives the much larger float gap 7.6293945E-6. Write Math.ulp(100.0) for the double gap.

Library declarations
Java
// java.lang.Math
public static double ulp(double d)
public static float ulp(float f)

Parameters

ParameterDescription
d / fThe number whose gap to the next stored value you want. Its sign does not matter.

Return Value

Return ValueDescription
double / floatThe positive distance between |d| and the next larger stored value; always a power of two. NaN gives NaN, either infinity gives Infinity, and either zero gives Double.MIN_VALUE (Float.MIN_VALUE for float).

Compare with Similar Methods

Methods that deal with gaps and rounding:

MethodHow it differs
nextUp(x)The next stored value itself: x + ulp(x) for positive x.
nextDown(x)The stored value below; the gap there can be half of ulp(x).
round(x)Rounds to whole numbers; useless once ulp(x) is 1 or more.
rint(x)Nearest whole number, ties to even, kept as a double.

Java Examples

Example 1 — See How the Gap Grows With the Number

Print the gap at small, medium, and huge values, for double and float, and at zero.

Java
public class MathUlpGapSizes {
    public static void main(String[] args) {
        System.out.println("ulp(1.0)   = " + Math.ulp(1.0));
        System.out.println("ulp(100.0) = " + Math.ulp(100.0));
        System.out.println("ulp(1e15)  = " + Math.ulp(1e15));
        System.out.println("ulp(1e16)  = " + Math.ulp(1e16));
        System.out.println("ulp(1.0f)  = " + Math.ulp(1.0f));
        System.out.println("ulp(0.0)   = " + Math.ulp(0.0));
    }
}

How It Works

  1. Always a power of two. 1.0 sits at 20, so its gap is 2-52; 100.0 sits between 64 and 128, so its gap is 26-52 = 2-46, about 1.4E-14.
  2. Huge numbers, coarse marks. At 1e15 neighbouring doubles are 0.125 apart, and at 1e16 they are 2.0 apart, so not even every whole number can be stored there.
  3. float and zero. A float has only 24 bits of digits, so its gap at 1.0 is 2-23, about 1.19E-7. At zero the gap is the smallest positive double, 4.9E-324.

Example 2 — Why 1e16 + 1 == 1e16 and a long Changes in a double

Two everyday surprises explained by the gap: adding 1 to a big double, and storing a big long ID in a double.

Java
public class MathUlpLostPrecision {
    public static void main(String[] args) {
        double big = 1e16;
        System.out.println("ulp(1e16) = " + Math.ulp(big));
        System.out.println("1e16 + 1 == 1e16? " + (big + 1 == big));
        System.out.println("1e16 + 2 = " + (big + 2));

        long id = 9007199254740993L;
        double asDouble = id;
        System.out.println("long id:   " + id);
        System.out.println("as double: " + (long) asDouble);
    }
}

How It Works

  1. Half a gap rounds away. With marks 2.0 apart, 1e16 + 1 falls exactly halfway between two marks; the tie goes to the "even" mark, which is 1e16 itself, so the sum equals big.
  2. A full gap survives. Adding 2 reaches the next mark, so big + 2 really changes and prints as 1.0000000000000002E16.
  3. Long IDs are not safe in a double. 9007199254740993 is 253 + 1. At that size the gap is 2.0, so the value is rounded to 9007199254740992 and the ID silently changes. Keep IDs in long or String.

Example 3 — Compare Doubles With an ulp-Based Tolerance

0.1 + 0.2 == 0.3 is false. Measure the error in ulps, then write a "nearly equal" check that scales with the numbers.

Java
public class MathUlpTolerance {
    static boolean nearlyEqual(double a, double b, int maxUlps) {
        double scale = Math.max(Math.abs(a), Math.abs(b));
        return Math.abs(a - b) <= maxUlps * Math.ulp(scale);
    }

    public static void main(String[] args) {
        double sum = 0.1 + 0.2;
        System.out.println("sum = " + sum);
        System.out.println("sum == 0.3? " + (sum == 0.3));
        System.out.println("gap in ulps: " + Math.abs(sum - 0.3) / Math.ulp(0.3));
        System.out.println("nearlyEqual(sum, 0.3): " + nearlyEqual(sum, 0.3, 4));
        System.out.println("nearlyEqual(1.0, 1.001): " + nearlyEqual(1.0, 1.001, 4));
    }
}

How It Works

  1. Off by exactly one mark. 0.1 and 0.2 are each rounded to the nearest mark, and their sum lands on the mark right after the one closest to 0.3, so == fails.
  2. Measure the error in ulps. Dividing the difference by Math.ulp(0.3) gives 1.0: the two values are next-door neighbours, the smallest possible miss.
  3. A tolerance that scales. maxUlps * Math.ulp(scale) is tiny for small numbers and larger for big ones, so "within 4 ulps" accepts the rounding noise in sum but still rejects 1.0 vs 1.001, which are trillions of ulps apart.

Frequently Asked Questions

The distance between the number and the next larger number a float or double can store. Math.ulp(1.0) is 2.220446049250313E-16 and Math.ulp(1e16) is 2.0.
Unit in the last place. It is the value of the smallest digit a floating-point number can change by at its current size.
Because Math.ulp(1e16) is 2.0: neighbouring doubles at that size are 2 apart. 1e16 + 1 lands exactly halfway and is rounded back to 1e16.
Check whether Math.abs(a - b) is at most a few ulps of the larger value, for example 4 * Math.ulp(Math.max(Math.abs(a), Math.abs(b))). The tolerance then grows and shrinks with the size of the numbers.
Most decimal amounts, such as 0.1, fall between two values a double can store and are rounded, so sums drift by a few ulps. Store whole cents in a long or use java.math.BigDecimal.

Back to the method index

See all 57 Math method guides grouped by what they do.

Method index →

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.

9 people found this page helpful