Close

Java - Math.nextUp() Examples

Java Java API 


Class:

java.lang.Math

java.lang.Objectjava.lang.Objectjava.lang.Mathjava.lang.MathLogicBig

Methods:

public static double nextUp(double d)

Returns the floating-point value adjacent to d in the direction of positive infinity. This method is semantically equivalent to nextAfter(d, Double.POSITIVE_INFINITY); however, a nextUp implementation may run faster than its equivalent nextAfter call.

public static float nextUp(float f)

Returns the floating-point value adjacent to f in the direction of positive infinity. This method is semantically equivalent to nextAfter(f, Float.POSITIVE_INFINITY); however, a nextUp implementation may run faster than its equivalent nextAfter call.

Examples


package com.logicbig.example.math;

public class NextUpExample {

public static void main(String... args) {
findNextUp(11.7);
findNextUp(736.147);
findNextUp(-23.5);
findNextUp(0.0);
findNextUp(0.0 / 0);
findNextUp(Double.MIN_VALUE);
findNextUp(Double.POSITIVE_INFINITY);
}

private static void findNextUp(double d) {
double nextUp = Math.nextUp(d);
System.out.printf("Math.nextUp(%s) = %s%n", d, nextUp);
}
}

Output

Math.nextUp(11.7) = 11.700000000000001
Math.nextUp(736.147) = 736.1470000000002
Math.nextUp(-23.5) = -23.499999999999996
Math.nextUp(0.0) = 4.9E-324
Math.nextUp(NaN) = NaN
Math.nextUp(4.9E-324) = 1.0E-323
Math.nextUp(Infinity) = Infinity




package com.logicbig.example.math;

public class NextUpExample2 {

public static void main(String... args) {
findNextUp(15.5f);
findNextUp(786.911f);
findNextUp(-55f);
findNextUp(0.0f);
findNextUp(0.0f / 0);
findNextUp(Float.MIN_VALUE);
findNextUp(Float.POSITIVE_INFINITY);
}

private static void findNextUp(float f) {
float nextUp = Math.nextUp(f);
System.out.printf("Math.nextUp(%s) = %s%n", f, nextUp);
}
}

Output

Math.nextUp(15.5) = 15.500001
Math.nextUp(786.911) = 786.9111
Math.nextUp(-55.0) = -54.999996
Math.nextUp(0.0) = 1.4E-45
Math.nextUp(NaN) = NaN
Math.nextUp(1.4E-45) = 2.8E-45
Math.nextUp(Infinity) = Infinity




See Also