Java Math - nextAfter() Method
The Java nextAfter() method returns the floating-point number adjacent to the first argument in the direction of the second argument. If both arguments compare as equal a value equivalent to the second argument is returned. In special cases it returns the following:
- If either argument is a NaN, then NaN is returned.
- If both arguments are signed zeros, a value equivalent to direction is returned.
- If start is infinite and direction has a value such that the result should have a smaller magnitude, Double.MAX_VALUE or Float.MAX_VALUE with the same sign as start is returned.
- If start is ±Double.MIN_VALUE or ±Float.MIN_VALUE and direction has a value such that the result should have a smaller magnitude, then a zero with the same sign as start is returned.
- If start is equal to ±Double.MAX_VALUE or ±Float.MAX_VALUE and direction has a value such that the result should have a larger magnitude, an infinity with same sign as start is returned.
Syntax
public static double nextAfter(double start, double direction) public static float nextAfter(float start, double direction)
Parameters
start |
Specify starting floating-point value. |
direction |
Specify value for direction. |
Return Value
Returns the floating-point number adjacent to the first argument in the direction of the second argument.
Exception
NA.
Example:
In the example below, nextAfter() method returns the floating-point number adjacent to the first argument in the direction of the second argument.
public class MyClass { public static void main(String[] args) { System.out.println(Math.nextAfter(2.55, 4)); System.out.println(Math.nextAfter(10.1, 3)); } }
The output of the above code will be:
2.5500000000000003 10.099999999999998
❮ Java Math Methods