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:

  1. public static double nextAfter (double start, double direction)
  2. 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:

Output:

Example 2:

Output:

Example 3:

Output:

Example 4:

Output:

Example 5:

Output: