Java Math nextAfter() method with Example
The java.lang.Math.nextAfter() returns the floating-point number adjacent to the first argument in the direction of the second argument. If both arguments are equal then the second argument is returned.
Syntax :
// datatype can be float or double.
public static dataType nextAfter(dataType st, dataType dir)Parameter :
st :starting floating-point value.
dir :value indicating which of start’s neighbors or start should be returned.Return :
This method returns the floating-point number adjacent to start in the direction of direction.
Note :
- If one of the arguments is NaN, Output is NaN
- 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 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 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 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.
Example 1 :To show working of java.lang.Math.nextAfter() method.
// Java program to demonstrate working // of java.lang.Math.nextAfter() method import java.lang.Math; class GfG { // driver code public static void main(String args[]) { double a = 0.0 / 0; double b = 12.2; // Input a is NaN, Output NaN System.out.println(Math.nextAfter(a, b)); double c = 0.0; double d = 0.0; // Both Input are signed zeros, Output zero System.out.println(Math.nextAfter(c, d)); float e = Float.MIN_VALUE; float f = 12.2f; System.out.println(Math.nextAfter(e, f)); float g = 1.0f / 0f; float h = 1.0f; System.out.println(Math.nextAfter(g, h)); double i = Double.MAX_VALUE; double j = 12344.2; System.out.println(Math.nextAfter(i, j)); } } |
Output:
NaN 0.0 2.8E-45 3.4028235E38 1.7976931348623155E308
Recommended Posts:
- StrictMath nextAfter() Method in Java
- Java.math.BigInteger.probablePrime() method in Java
- Java.math.BigInteger.modInverse() method in Java
- Java Math exp() method with Example
- Java Math log() method with example
- Java Math asin() method with Example
- Java Math getExponent() method with Example
- Java Math getExponent() method with Example
- Java Math expm1() method with Example
- Java Math addExact(int a, int b) method
- Java Math ulp() method with Examples
- Java Math round() method with Example
- Java Math cos() method with Examples
- Java Math hypot() method with Example
- Math floorMod() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



