×

Java Math nextAfter() Method

The nextAfter() method of Math class returns the floating-point value adjacent to the first argument in direction of the second argument.

Syntax:

  1. public static double nextAfter (double start, double direction)
  2. public static float nextAfter (float start, double direction)

Parameters:

The parameter ‘start’ represents the starting floating-point and parameter ‘direction’ represents the value indicating which start or start’s neighbors should be returned.

Return Value:

The nextAfter() method returns the floating-point number adjacent to start in the direction of second parameter.

Special cases of the nextAfter() method are as follows:

  • It returns NaN, is either argument passed is NaN.
  • It returns unchanged direction, if both arguments are signed zero.
  • It returns zero with same sign as start, if start’s value is infinite and direction’s value is such that the result has smaller magnitude.
  • It returns ±MAX_VALUE with same sign as start, if start’s value is ±Double.MIN_VALUE and direction’s value is such that the result has smaller magnitude.
  • It returns infinity with same sign as start, if start’s value is ±MAX_VALUE and direction’s value is such that the result has larger magnitude.

Example 1:

public class JavaMathNextAfterExample1 {
    public static void main(String[] args) {
        Double a = 1d;
        Double b = 10d;
        // returns the floating-point number adjacent to start in the direction of second parameter
        System.out.println("Floating-point number : "+Math.nextAfter(a,b));
    }
}

Output:

Floating-point number : 1.0000000000000002

Example 2:

public class JavaMathNextAfterExample2 {
    public static void main(String[] args) {
        Double a = 1d;
        Double b = 0.0d/0.0d;
        // returns NaN, is either argument passed is NaN.
        System.out.println("Floating-point number : "+Math.nextAfter(a,b));
    }
}

Output:

Floating-point number : NaN

Example 3:

public class JavaMathNextAfterExample3 {
    public static void main(String[] args) {
        Double a = -0.0d;
        Double b = -0.0d;
        // returns unchanged direction, if both arguments are signed zero.
        System.out.println("Floating-point number : "+Math.nextAfter(a,b));
    }
}

Output:

Floating-point number : -0.0

Example 4:

public class JavaMathNextAfterExample4 {
    public static void main(String[] args) {
        Double start = Double.MIN_VALUE;
        Double direction = -0d;
        /*returns ±Double.MAX_VALUE with same sign as start,
        if start’s value is ±Double.MIN_VALUE and direction’s value is such that the result has smaller magnitude. */
        System.out.println("Floating-point number : "+Math.nextAfter(start,direction));
    }
}

Output:

Floating-point number : 0.0

Example 5:

public class JavaMathNextAfterExample5 {
    public static void main(String[] args) {
        Double start = -Double.MIN_VALUE;
        Double direction = -8765643.98;
        /*returns ±Double.MAX_VALUE with same sign as start,
        if start’s value is ±Double.MIN_VALUE and direction’s value is such that the result has smaller magnitude*/
        System.out.println("Floating-point number : "+Math.nextAfter(start,direction));
    }
}

Output:

Floating-point number : -1.0E-323

Related Topics

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

Zigzag Array in Java

In this tutorial, we discuss, what is zigzag array and its example. Even we will create the java program. In this program, we convert the simple array into a zigzag...

4 minutes read.

JAR File in Java

What is a JAR File? JAR stands for Java Archive. It is a file format mainly used to combine many Java class files and the corresponding metadata and resources into one...

4 minutes read.

Java Import Keyword

Java's import keyword used in the code that follows the import statement, a Java class is declared. Once a Java class is declared, it is possible to use the class...

6 minutes read.

Java Integer highestOneBit()

The highestOneBit() method of Java Integer class returns an int value with at most a single one-bit, in the position of the highest-order one-bit in the specified int value.  Syntax public static...

1 minute read.

Iterate JSON array in Java

This article is going to equip the knowledge about what JSON array is and how it works and also how is it distinct with the generic array. Json Array Ordered lists of...

4 minutes read.

Collections in Java

Collection framework is one of Java's most powerful subsystems. It is a set of classes and interfaces that is all-the-rage technology for superintending objects and a group of objects. In...

6 minutes read.

How to Read CSV Files in Java?

Comma-Separated Values is the acronym for this format. It is a straightforward file format storing textual tabular data in a database or spreadsheet. The CSV files can be imported into...

3 minutes read.

Hamming Code in Java

In a computer network, hamming code is a unique set of error-correction codes. It is mostly utilised in computer graphics for mistake detection and correction during data transmission from sender...

8 minutes read.

Why are generics used in Java

Java has a feature called generics that allows you to make a class, interface, and function accepting any (reference) type as a parameter. In other words, it is the idea...

4 minutes read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.

How to override toString() method in Java?

Java is an object-oriented language. It only works with classes and objects. Thus, whenever we need to calculate, we need an object or objects that belong to the class. The Java method...

2 minutes read.

String to JSON in Java

Nowadays, receiving data in JSON String format rather than XML is quite frequent. Java does not transform JSON String to JSON Object when dealing with JSON String. However, using the...

3 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 minutes read.

Bellman Ford Algorithm in Java

Numerous algorithms have been used in dynamic programming to determine the shortest path inside a graph. Among them are Floyd, all-pair shortest path problem, Breadth First Search, Depth First Search,...

6 minutes read.

3N+1 problem program in Java

The 3N+1 problem is a hypothesis in the field of abstract mathematics (not yet proven). Collectively known as the Collatz problem. This tutorial will describe about the 3N+1 problem and...

3 minutes read.

Java AWT

Java AWT Java programming is used to develop different types of applications like window-based applications, web applications, Enterprise applications, or mobile applications. For creating standalone applications, Java AWT API is used....

11 minutes read.

Advantages and Disadvantages of Strings in Java

What is a String? Java is an object-oriented, platform-independent, high-level, general-purpose, and interpreted programming language.Sun Microsystems is known as the founder of java in 1991; the Java programming language was developed...

4 minutes read.

Object Definition in Java

In this article, you will be acknowledged about object in Java, its definition and how is this useful in writing the programs in Java. The comprehension of object-oriented technology depends on...

4 minutes read.