The Java nextAfter Function is one of the Java Math Library function which is used to return the floating point value adjacent to the first argument in the direction of second argument. If both first argument and second argument is equal, then nextAfter Function will return second argument. In this article we will show you, How to use Math.nextAfter Function in Java Programming language with example.
Java nextAfter Function syntax
The basic syntax of the Math.nextAfter Function in Java Programming language is as shown below:
1 |
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 will find the adjacent value or neighbor 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 are not a number, java Math.nextAfter function will return NaN.
- If start argument is equal to positive or negative Double.MIN_VALUE and the direction argument has a value such that the result should have a smaller magnitude, then Math.nextAfter function will return Zero with same sign.
- If 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 Math.nextAfter function will return Infinity with same sign.
- If start argument is infinity and the direction argument has a value such that the result should have a smaller magnitude, then Math.nextAfter function will return Double.MAX_VALUE with same sign.
- If both arguments are signed zeros, direction will be unchanged
Java Programming provides two different Math.nextAfter Java functions to find the adjacent floating point value in the direction of second argument. Following function will accept positive or negative double value as first and second argument and returns the result
1 2 3 4 |
static double nextAfter(double start, double direction); //Return Type is double // In order to use in program: Math.nextAfter(double start, double direction); |
Following function will accept positive or negative float value as first and second argument and returns the adjacent floating value of first in the direction of second argument
1 2 3 4 |
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
The Java Math.nextAfter function is used to return the floating point value adjacent to the first argument in the direction of second argument. In this example, We are going to find the same of different data types and display the output
JAVA CODE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// 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)); } } |
OUTPUT
ANALYSIS
First, We declared variables of type Double and Float to display the functionality of java Math.nextAfter function on different data types.
1 2 |
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; |
Next, We used the Math.nextAfter Function directly on both the Positive and negative values.
1 2 3 4 |
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 Java Math.nextAfter Function on variable b, c, d and e (they belongs to double type). Following statements will call the nextAfter method of double type ( static double nextAfter(double start, double direction) ) to return the adjacent floating value.
1 2 3 4 |
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)); |
Next, We used the Math.nextAfter Function on variable g, h, i and j (they belongs to float type). Following statements will call the nextAfter method of float type ( static float nextAfter(float start, float direction) ) to return the adjacent floating value.
1 2 3 4 |
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)); |
Thank You for Visiting Our Blog
Share your Feedback, or Code!!