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.
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
Parameter
Description
d / f
The number whose gap to the next stored value you want. Its sign does not matter.
Return Value
Return Value
Description
double / float
The 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).
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.
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.
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.
ulp(1e16) = 2.0
1e16 + 1 == 1e16? true
1e16 + 2 = 1.0000000000000002E16
long id: 9007199254740993
as double: 9007199254740992
How It Works
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.
A full gap survives. Adding 2 reaches the next mark, so big + 2 really changes and prints as 1.0000000000000002E16.
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));
}
}
Output
sum = 0.30000000000000004
sum == 0.3? false
gap in ulps: 1.0
nearlyEqual(sum, 0.3): true
nearlyEqual(1.0, 1.001): false
How It Works
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.
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.
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.