Java nextAfter Function

The Java nextAfter Function is one of the Java Math Library functions, which is to return the floating-point value adjacent to the first argument in the direction of the second argument. If both the first argument and the second argument are equal, the Java nextAfter Function returns the second argument.

Java nextAfter Function syntax

The basic syntax of the Math.nextAfter Function in Java Programming language is as shown below:

Math.nextAfter(data_type start, data_type direction);
  • start: Please specify the starting floating-point value here.
  • direction: Please specify the direction here. Math.nextAfter Function finds the adjacent value or neighbor of the start argument and returns the output.

The nextAfter function will not return the expected output for the below-specified cases:

  • If either of the arguments is not a number, java Math.nextAfter function returns NaN.
  • If the start argument is equal to positive or negative Double.MIN_VALUE and the direction argument have a value such that the result should have a smaller magnitude, then Math.nextAfter function returns Zero with the same sign.
  • If the start argument is equal to positive or negative Double.MAX_VALUE and the direction argument has a value such that the result should have a larger magnitude. Then the Math.nextAfter function returns Infinity with the same sign.
  • When the start argument is infinity and the direction argument has a value such that the result should have a smaller magnitude, then Math.nextAfter function returns Double.MAX_VALUE with the same sign.
  • If both arguments are signed zeros, the direction is unchanged.

Java Programming provides two different Math.nextAfter Java functions to find the adjacent floating-point value in the direction of the second argument. The following nextAfter double function accepts a positive or negative double value as the first and second argument and returns the result.

static double nextAfter(double start,  double direction); //Return Type is double

// In order to use in program: 
Math.nextAfter(double start,  double direction);

This Java method accepts a positive or negative float value

static float nextAfter(float start,  float direction); //Return Type is float

// In order to use in program: 
Math.nextAfter(float start,  float direction);

Java nextAfter Function Example

In this example, we are going to use the Java Math.nextAfter function to returns the floating-point value adjacent to the first argument in the direction of the second argument.

// Java Math.nextAfter Function
package MathFunctions;

public class NextAfter {
	public static void main(String[] args) {
		double b = 14.9666, c = 100.6674, d = -95.474, e = -214.9865;
		float g = 65.23f, h = 74.809f, i = -22.89f, j = -3.54f;
		
		System.out.println("NextAfter result of Positive Values: " + Math.nextAfter(10, 100));
		System.out.println("NextAfter result of Negative Values: " + Math.nextAfter(-10, -1000));
		System.out.println("NextAfter result of both Positive & Negative Values: " + Math.nextAfter(10, -1000));
		System.out.println("NextAfter result of both Positive & Negative Values: " + Math.nextAfter(-10, 1));
		
		System.out.println("\nNextAfter result of Positive Doubles: " + Math.nextAfter(b, c));
		System.out.println("NextAfter result of Negative Double Values: " + Math.nextAfter(d, e));
		System.out.println("NextAfter result of both Positive & Negative Double Values: " + Math.nextAfter(c, d));
		System.out.println("NextAfter result of both Positive & Negative Double Values: " + Math.nextAfter(e, b));
		
		System.out.println("\nNextAfter result of Positive Float Values: " + Math.nextAfter(g, h));
		System.out.println("NextAfter result of Negative Float Values: " + Math.nextAfter(i, j));
		System.out.println("CNextAfter result of both Positive & Negative Float Values: " + Math.nextAfter(h, i));
		System.out.println("NextAfter result of both Positive & Negative Float Values: " + Math.nextAfter(j, h));
	}
}
Java nextAfter Function 1

Within this example, We used the math nextAfter Function directly on both the Positive and negative values.

System.out.println("NextAfter result of Positive Values: " + Math.nextAfter(10, 100));
System.out.println("NextAfter result of Negative Values: " + Math.nextAfter(-10, -1000));
System.out.println("NextAfter result of both Positive & Negative Values: " + Math.nextAfter(10, -1000));
System.out.println("NextAfter result of both Positive & Negative Values: " + Math.nextAfter(-10, 1));

Next, we used the Math.nextAfter Function on variable b, c, d, and e (they belongs to double type). It calls the nextAfter method of double type ( static double nextAfter(double start, double direction) ) to return the adjacent floating value.

System.out.println("\nNextAfter result of Positive Doubles: " + Math.nextAfter(b, c));
System.out.println("NextAfter result of Negative Double Values: " + Math.nextAfter(d, e));
System.out.println("NextAfter result of both Positive & Negative Double Values: " + Math.nextAfter(c, d));
System.out.println("NextAfter result of both Positive & Negative Double Values: " + Math.nextAfter(e, b));

Here, we used the nextAfter Math function on variable g, h, i, and j (they belong to float type). It calls the nextAfter method of float type ( static float nextAfter(float start, float direction) ) to return the adjacent floating value.

System.out.println("\nNextAfter result of Positive Float Values: " + Math.nextAfter(g, h));
System.out.println("NextAfter result of Negative Float Values: " + Math.nextAfter(i, j));
System.out.println("CNextAfter result of both Positive & Negative Float Values: " + Math.nextAfter(h, i));
System.out.println("NextAfter result of both Positive & Negative Float Values: " + Math.nextAfter(j, h));