Java Math.nextUp() method returns floating point number adjacent to the passed argument, in the direction of positive infinity.
public class JavaExample { public static void main(String[] args) { double d = 12345; float f = 8.88f; System.out.println(Math.nextUp(d)); System.out.println(Math.nextUp(f)); } }
Output:
12345.000000000002 8.880001
Syntax of Math.nextUp() method
Math.nextUp(12.25f); //returns 12.250001
nextUp() Description
public static double nextUp(double d): Returns floating point value adjacent to d, towards positive infinity. This is equivalent to nextAfter(d, Double.POSITIVE_INFINITY). However it faster compared to nextAfter() method.
public static float nextUp(float f): Returns floating point value adjacent to passed float argument f, towards positive infinity. This is equivalent to nextAfter(d, Float.POSITIVE_INFINITY). However it faster compared to nextAfter() method.
nextUp() Parameters
- d: Double value whose adjacent value to be determined.
- f: Float value whose adjacent value to be determined.
nextUp() Return Value
- Adjacent floating value towards positive infinity.
- If the argument is NaN (Not a number) then it returns NaN.
- If the argument is positive infinity, then it returns positive infinity.
- If the passed argument is zero, it returns Double.MIN_VALUE for double type argument or Float.MIN_VALUE for float type argument.
Example 1: Adjacent value to given double and float
public class JavaExample { public static void main(String[] args) { double d = 455; float f = 3.44f; System.out.println(Math.nextUp(d)); //towards positive infinity System.out.println(Math.nextUp(f)); //towards positive infinity } }
Output:
Example 2: Adjacent to NaN
public class JavaExample { public static void main(String[] args) { double d = 0.0/0; //NaN System.out.println(Math.nextUp(d)); } }
Output:
Example 3: Adjacent to zero double and zero float
public class JavaExample { public static void main(String[] args) { double d = 0; // zero double value float f = 0.0f; //zero float value //This will return Double.MIN_VALUE System.out.println(Math.nextUp(d)); //This will return Float.MIN_VALUE System.out.println(Math.nextUp(f)); } }
Output:
Example 4: Next Up value for positive infinity
public class JavaExample { public static void main(String[] args) { double d = Double.POSITIVE_INFINITY; System.out.println(Math.nextUp(d)); } }
Output: