×

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

Jdoodle Java

Everything is instantaneous and fast-paced in the modern world. Online compilers available via the internet are immensely helpful for programmers seeking to learn a new programming language but lacking the...

4 minutes read.

Java Else Keyword

The else statement specifies a section of Java code that will run if an if statement's condition is false.The following conditional statements can be used in Java:To provide a block...

3 minutes read.

Java Math cbrt() Method

The cbrt() method of Math class returns the cube root of a double value. Syntax: public static double cbrt(double a) Parameters: The parameter ‘a’ represents the value whose cube root is to be determined. Return...

2 minutes read.

FizzBuzz Program in Java

FizzBuzz is a well-known children's game. This game helps children learn division. The FizzBuzz game is becoming a popular programming question, appearing frequently in Core Java interviews. This section will...

3 minutes read.

Constructor Overloading in Java

In Java, constructors can be overloaded just like methods. The idea of having multiple constructors with various parameter sets so that each function Object()  can carry out a particular task...

3 minutes read.

MOOD Factors to Assess a Java Program

In this tutorial, we will comprehendthe meaning of mood factors in Java. For the development of any software system,the quality of anapplication is important. It is more important to maintain large-scale...

4 minutes read.

How to check the Java version in cmd

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated...

5 minutes read.

Gregorian Calendar Java Current Date

GregorianCalendar class uses the Gregorian and Julian calendars. Dates are calculated by projecting present laws forever backward and forward in time. As a consequence, GregorianCalendar may be utilised to create...

8 minutes read.

LCM Program in Java

LCM Program in Java The LCM program in Java outputs the LCM of the given numbers. In Arithmetic, the lowest common multiple, least common multiple or smallest common multiple of the...

6 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.

Maximum length of string in java

In java, String can act as a data type and a class. The string can be defined as the collection of characters that are enclosed with double quotes(“ “). The...

3 minutes read.

Java exception list

Java uses exceptions, like the majority of contemporary programming languages, to deal with both errors and "extraordinary events." When an exception arises inside the program, it messes up the regular...

6 minutes read.

Java Output Formatting

Sometimes we want a program's output to be displayed in a specific format. The printf () function in the C programming language can be used to achieve this. We will...

4 minutes read.

Ternary Operator in Java

In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator. Ternary operator in Java A...

3 minutes read.

Java Thread class

Thread class The thread represents a part of the process. Every process can have multiple associated threads in which every thread may execute the same or different job. By default, each thread assigns...

16 minutes read.

Memory Areas in Java

Let’s have a look at how memory management in Java works. We will be going to discuss how the objects get destroyed, the working of a garbage collector, and things...

5 minutes read.

Access specifiers in Java

What are access specifiers in Java? Access specifiers in Java are used to determine whether other classes can use a particular field or invoke a particular method. Access specifiers mainly specify...

7 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.

Encapsulation Program in Java

Encapsulation Program in Java Encapsulation program in Java demonstrates the technique to bind methods and fields in a single unit. The term encapsulation is inspired by the word ‘capsule’, which is...

3 minutes read.

Java Session

Session indicates interval of time. A session is a simple  time interval in which servers and client interacts. To maintain the state of the client or user we use technologies...

5 minutes read.