Java Math nextAfter() Method
The nextAfter() method of Math class returns the floating-point value adjacent to the first argument in direction of the second argument.
Syntax:
- public static double nextAfter (double start, double direction)
- public static float nextAfter (float start, double direction)
Parameters:
The parameter ‘start’ represents the starting floating-point and parameter ‘direction’ represents the value indicating which start or start’s neighbors should be returned.
Return Value:
The nextAfter() method returns the floating-point number adjacent to start in the direction of second parameter.
Special cases of the nextAfter() method are as follows:
- It returns NaN, is either argument passed is NaN.
- It returns unchanged direction, if both arguments are signed zero.
- It returns zero with same sign as start, if start’s value is infinite and direction’s value is such that the result has smaller magnitude.
- It returns ±MAX_VALUE with same sign as start, if start’s value is ±Double.MIN_VALUE and direction’s value is such that the result has smaller magnitude.
- It returns infinity with same sign as start, if start’s value is ±MAX_VALUE and direction’s value is such that the result has larger magnitude.
Example 1:
public class JavaMathNextAfterExample1 { public static void main(String[] args) { Double a = 1d; Double b = 10d; // returns the floating-point number adjacent to start in the direction of second parameter System.out.println("Floating-point number : "+Math.nextAfter(a,b)); } }
Output:
Floating-point number : 1.0000000000000002
Example 2:
public class JavaMathNextAfterExample2 { public static void main(String[] args) { Double a = 1d; Double b = 0.0d/0.0d; // returns NaN, is either argument passed is NaN. System.out.println("Floating-point number : "+Math.nextAfter(a,b)); } }
Output:
Floating-point number : NaN
Example 3:
public class JavaMathNextAfterExample3 { public static void main(String[] args) { Double a = -0.0d; Double b = -0.0d; // returns unchanged direction, if both arguments are signed zero. System.out.println("Floating-point number : "+Math.nextAfter(a,b)); } }
Output:
Floating-point number : -0.0
Example 4:
public class JavaMathNextAfterExample4 { public static void main(String[] args) { Double start = Double.MIN_VALUE; Double direction = -0d; /*returns ±Double.MAX_VALUE with same sign as start, if start’s value is ±Double.MIN_VALUE and direction’s value is such that the result has smaller magnitude. */ System.out.println("Floating-point number : "+Math.nextAfter(start,direction)); } }
Output:
Floating-point number : 0.0
Example 5:
public class JavaMathNextAfterExample5 { public static void main(String[] args) { Double start = -Double.MIN_VALUE; Double direction = -8765643.98; /*returns ±Double.MAX_VALUE with same sign as start, if start’s value is ±Double.MIN_VALUE and direction’s value is such that the result has smaller magnitude*/ System.out.println("Floating-point number : "+Math.nextAfter(start,direction)); } }
Output:
Floating-point number : -1.0E-323