Java Math nextUp() Method
The nextUp() method of Math class returns the floating-point number adjacent to the argument in direction of the positive infinity.
Syntax:
- public static double nextUp (double d)
- public static float nextUp (float f)
Parameters:
The parameters ‘d’ and ‘f’ represent the starting floating-point values.
Return Value:
The nextUp () method returns the floating-point number which is closer to positive infinity.
Special cases of the nextUp () method are as follows:
- It returns NaN, is either argument passed is NaN.
- It returns positive infinity, if the argument is positive infinity.
- It returns Double.MIN_VALUE, is the argument is zero.
Example 1:
public class JavaMathNextUpExample1 {
public static void main(String[] args) {
double d = 2d;
// returns the floating-point number which is closer to positive infinity
System.out.println("Floating-point number : "+Math.nextUp(d));
}
}
Output:
Floating-point number : 2.0000000000000004
Example 2:
public class JavaMathNextUpExample2 {
public static void main(String[] args) {
Float f = Float.NEGATIVE_INFINITY;
// returns the floating-point number which is closer to positive infinity
System.out.println("Floating-point number : "+Math.nextUp(f));
}
}
Output:
Floating-point number : -3.4028235E38
Example 3:
public class JavaMathNextUpExample3 {
public static void main(String[] args) {
Double d = Double.POSITIVE_INFINITY;
//returns positive infinity, if the argument is positive infinity
System.out.println("Floating-point number : "+Math.nextUp(d));
}
}
Output:
Floating-point number : Infinity
Example 4:
public class JavaMathNextUpExample4 {
public static void main(String[] args) {
Double d = Double.NaN;
//returns NaN, is the argument is NaN.
System.out.println("Floating-point number : "+Math.nextUp(d));
}
}
Output:
Floating-point number : NaN
Example 5:
public class JavaMathNextUpExample5 {
public static void main(String[] args) {
Double d = 0.0d;
//returns Double.MIN_VALUE, is the argument is zero.
System.out.println("Floating-point number : "+Math.nextUp(d));
}
}
Output:
Floating-point number : 4.9E-324
