- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Math nextAfter(double x, double y) Method
Description
The Java Math nextAfter(double start, double direction) returns the floating-point number adjacent to the first argument in the direction of the second argument. If both arguments compare as equal the second argument is returned. Special cases −
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 startis 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.
Declaration
Following is the declaration for java.lang.Math.nextAfter() method
public static double nextAfter(double start, double direction)
Parameters
start − starting floating-point value
direction − value indicating which of start's neighbors or start should be returned
Return Value
This method returns the floating-point number adjacent to start in the direction of direction.
Exception
NA
Getting Next Float After two Positive Double Values Example
The following example shows the usage of Math nextAfter() method for two positive values.
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get two double numbers double x = 98759.765; double y = 154.28764; // print the next number for x towards y System.out.println("Math.nextAfter(" + x + "," + y + ")=" + Math.nextAfter(x, y)); // print the next number for y towards x System.out.println("Math.nextAfter(" + y + "," + x + ")=" + Math.nextAfter(y, x)); } }
Output
Let us compile and run the above program, this will produce the following result −
Math.nextAfter(98759.765,154.28764)=98759.76499999998 Math.nextAfter(154.28764,98759.765)=154.28764000000004
Getting Next Float After a Positive and a Negative Double Value Example
The following example shows the usage of Math nextAfter() method for a positive and a negative value.
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get two double numbers double x = -98759.765; double y = 154.28764; // print the next number for x towards y System.out.println("Math.nextAfter(" + x + "," + y + ")=" + Math.nextAfter(x, y)); // print the next number for y towards x System.out.println("Math.nextAfter(" + y + "," + x + ")=" + Math.nextAfter(y, x)); } }
Output
Let us compile and run the above program, this will produce the following result −
Math.nextAfter(-98759.765,154.28764)=-98759.76499999998 Math.nextAfter(154.28764,-98759.765)=154.28763999999998
Getting Next Float After two Negative Double Values Example
The following example shows the usage of Math nextAfter() method for negative values.
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get two double numbers double x = -98759.765; double y = -154.28764; // print the next number for x towards y System.out.println("Math.nextAfter(" + x + "," + y + ")=" + Math.nextAfter(x, y)); // print the next number for y towards x System.out.println("Math.nextAfter(" + y + "," + x + ")=" + Math.nextAfter(y, x)); } }
Output
Let us compile and run the above program, this will produce the following result −
Math.nextAfter(-98759.765,-154.28764)=-98759.76499999998 Math.nextAfter(-154.28764,-98759.765)=-154.28764000000004
To Continue Learning Please Login
Login with Google