java.lang.Math.nextAfter()
Description
- public static double nextAfter(double start, double direction)
- public static float nextAfter(float start, float direction)
The two overloaded methods are basically the same, it’s just that they deal with different data type either float or double.
Most of the methods of the Math class is static and the nextAfter() method is no exception. Thus don’t forget that in order to call this method, you don’t have to create a new object. Use the method in the format Math.nextAfter(start, direction).
Notes:
- If either argument is a NaN, then NaN is returned.
- If both arguments are signed zeros, direction is returned unchanged (as implied by the requirement of returning the second argument if the arguments compare as equal).
- If start is ±Double.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 infinite and direction has a value such that the result should have a smaller magnitude, Double.MAX_VALUE with the same sign as start is returned.
- If start is equal to ± Double.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.
Method Syntax
public static float nextAfter(float start, float direction)
public static double nextAfter(double start, double direction)
Method Returns
The nextAfter() method returns the floating-point number adjacent to start in the direction of direction.
Compatibility
Requires Java 1.6 and up
Java Math nextAfter() Example
Below is a java code demonstrates the use of nextAfter() method of Math class. The example presented might be simple however it shows the behavior of the nextAfter() method.
package com.javatutorialhq.java.examples; import java.util.Scanner; /* * This example source code demonstrates the use of * nextAfter() method of Math class */ public class MathNextAfterExample { public static void main(String[] args) { // Ask for user input System.out.print("start:"); // use scanner to read the console input Scanner scan = new Scanner(System.in); // Assign the 1st input to String variable String start = scan.nextLine(); // ask for the second input System.out.print("direction:"); // Assign the 2nd input to String variable String direction = scan.nextLine(); // close the scanner object scan.close(); // convert the values to double double doubleStart = Double.parseDouble(start); double doubleDirection = Double.parseDouble(direction); // get the result of nextAfter() method double result = Math.nextAfter(doubleStart,doubleDirection); System.out.println("Result of the operation:"+result); } }
The above java example source code demonstrates the use of nextAfter() method of Math class. We simply ask for two user input and we use the Scanner class to parse it. Since we have used the nextLine() method to get the console value which is having a return data type of String thus we have used the Double.parseDouble() to transform it into double. Alternatively if the requirements is to use float then you must use the Float.parseFloat() instead. This conversion is required because the argument for nextAfter() method only accepts either float or double.