What is Math.nextAfter() method in Java?
The nextAfter() method will return the next adjacent number (floating-point value) of the first argument in the direction of the second argument.
Syntax
// numbers can be float or double
Math.nextAfter(double start, double direction);
Return value
-
If the
start < direction, then the next adjacent number in the direction of positive infinity is returned. -
If the
start > direction, then the next adjacent number in the direction of negative infinity is returned. -
If the
start == direction, then thedirectionis returned. -
If the
startis equal to± Float.MIN_VALUE or ± Double.MIN_VALUEand thedirectionhas a value that is smaller thanstart, then0with the same sign as thestartis returned. -
If the
startis equal to± Float.MAX_VALUE or ± Double.MAX_VALUEand thedirectionhas a value that is greater thanstart, thenInfinitywith the same sign as thestartis returned. -
If the
startisInfinityand thedirectionis smaller thanstartthen theMAX_VALUEwith the sign ofstartwill be returned. -
NaNis returned if any one of the arguments isNaN.
Example
class HelloWorld {public static void main( String args[] ) {float start;double direction;// start < directionstart = 9.999999f;direction = 11;System.out.println("Math.nextAfter(9.999999, 11) :" + Math.nextAfter(start, direction));// start > directionstart = 10;direction = 1;System.out.println("Math.nextAfter(10, 1) :" + Math.nextAfter(start, direction));// start == directionstart = 10;direction = 10;System.out.println("Math.nextAfter(10, 10) :" + Math.nextAfter(start, direction));// start = Float.MIN_VALUE , start < directionstart = Float.MIN_VALUE;direction = Double.MIN_VALUE;System.out.println("Math.nextAfter(Float.MIN_VALUE, Double.MIN_VALUE) :" + Math.nextAfter(start, direction));// start = Float.MAX_VALUE , start < directionstart = Float.MAX_VALUE;direction = Double.MAX_VALUE;System.out.println("Math.nextAfter(Float.MAX_VALUE, Double.MAX_VALUE) :" + Math.nextAfter(start, direction));// start = Infinity , start > directionstart = Float.POSITIVE_INFINITY;direction = 10;System.out.println("Math.nextAfter(Float.POSITIVE_INFINITY, 10) :" + Math.nextAfter(start, direction));}}
In the code above, we see the following.
- For the first case:
// start < direction
start = 9.999999f;
direction = 11;
Math.nextAfter(start, direction);
If the start < direction, then the next adjacent number in the direction of positive infinity is returned. In our case 10.0 is returned.
- For the second case:
// start > direction
start = 10;
direction = 1;
Math.nextAfter(start, direction);
If the start > direction, then the next adjacent number in the direction of negative infinity is returned. In this case 9.999999 is returned.
- For the third case:
// start == direction
start = 10;
direction = 10;
Math.nextAfter(start, direction);
If the start == direction, then the direction is returned. In this case 10 is returned.
- For the fourth case:
// start = Float.MIN_VALUE , start < direction
start = Float.MIN_VALUE;
direction = Double.MIN_VALUE;
Math.nextAfter(start, direction); // 0.0
If the start is equal to ± Float.MIN_VALUE or ± Double.MIN_VALUE and the direction has a value that is smaller than start, then 0 with the same sign as the start is returned.
- For the fifth case:
// start = Float.MAX_VALUE , start < direction
start = Float.MAX_VALUE;
direction = Double.MAX_VALUE;
Math.nextAfter(start, direction); // Infinity
If the start is equal to ± Float.MAX_VALUE or ± Double.MAX_VALUE and the direction has a value that is greater than start, then Infinity with the same sign as the start is returned.
- For the sixth case:
// start = Infinity , start > direction
start = Float.POSITIVE_INFINITY;
direction = 10;
Math.nextAfter(start, direction); // 3.4028235E38
If the start is Infinity and the direction is smaller than start, then the MAX_VALUE with the sign of start will be returned.