Math nextUp nextDown Adjacent Doubles
Step between representable doubles with Math.nextUp and nextDown, measure the gap with ulp, and watch nextUp of MAX_VALUE reach Infinity.
Code
public class AdjacentDoubles {
public static void main(String[] args) {
// 1.0 之后的下一个可表示 double:步长是一个 ULP,不是 1.1
double next = Math.nextUp(1.0);
System.out.println("nextUp(1.0) = " + next);
System.out.println("ulp(1.0) = " + Math.ulp(1.0));
System.out.println("nextDown(next) = " + Math.nextDown(next));
System.out.println("1.0 == nextUp(1.0)? " + (1.0 == Math.nextUp(1.0)));
// 零附近的步长:ulp(0.0) 等于 Double.MIN_VALUE(最小可表示正数)
System.out.println("ulp(0.0) = " + Math.ulp(0.0));
// nextAfter 朝指定方向走一步;MAX_VALUE 之上没有下一个,结果是 Infinity
System.out.println("nextAfter(1.0, 2.0) = " + Math.nextAfter(1.0, 2.0));
System.out.println("nextUp(MAX_VALUE) = " + Math.nextUp(Double.MAX_VALUE));
}
}Inside the snippet
All of the logic lives in the class AdjacentDoubles and the function main.
What to watch for
- Self-sufficient by design — there is no registry and no lockfile entry.
Using it
The signature you will call most reads public static void main(String[] args)
Each step the file takes is announced in the output: nextUp(1.0) =, ulp(1.0) =, nextDown(next) =.
Key statements
double next = Math.nextUp(1.0);
Interactive Tools for This Topic
GCD & LCM CalculatorFind the greatest common divisor and least common multiple of two or more integers, computed exactly with BigInt.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.Vector Similarity CalculatorCompute Cosine Similarity, Dot Product, Euclidean L2, Manhattan L1, and Chebyshev distances between numeric vectors with dimension checks.
FAQ
- What does Math nextUp nextDown Adjacent Doubles print when run?
- Invoke the file as-is — the `nextUp(1.0) =`, `ulp(1.0) =`, `nextDown(next) =` prefixes identify which step printed each line.
Report a problem with this exampleOpens a pre-filled issue on GitHub.