Home » Java programming language

Java Math Class static double nextAfter(double starts , double directions) with example

Java Math Class static double nextAfter(double starts , double directions) method: Here, we are going to learn about the static double nextAfter(double starts , double directions) method of Math Class with its syntax and example.
Submitted by Preeti Jain, on September 07, 2019

Math Class static double nextAfter(double starts , double directions)

  • This method is available in java.lang package.
  • This method is used to return the double floating-point number adjacent to the first parameter (starts) in the direction of the second parameter (directions).
  • Let suppose both arguments passed in the method are equivalent so in that case the second parameter is returned.
  • This is a static method, so it is accessible with the class name too.
  • The return type of this method is double, it returns the double floating-point number adjacent to start in the direction of the second argument.
  • In this method, we pass two parameters of double type so the first parameter represents the initial or starting floating-point value and the second parameter represents the value denoting which of the given first parameter neighbor (Starts neighbor) or start is returned.
  • This method does not throw any exception.

Syntax:

    public static double nextAfter(double starts , double directions){
    }

Parameter(s):

  • starts – represents the initial or starting floating-point value.
  • directions – represents the value denoting which of the given first parameter neighbor (starts neighbor).

Return value:

The return type of this method is double, it returns the double floating-point number adjacent to the first parameter (starts) in the direction of second parameter (directions).

Note:

  • If we pass "NaN" (Not a Number), it returns the same i.e. "NaN".
  • If we pass the same value in both of the parameters, it returns the same value.
  • If we pass "Double.MIN_VALUE" as the first parameter and second parameter holds another value, it returns smaller value i.e. the same value is with the same sign as the first parameter.
  • If we pass infinity as first parameter and second parameter holds another value, it returns the "Double.MAX_VALUE" with the same sign as the first parameter.
  • If we pass "Double.MAX_VALUE" as the first parameter and second parameter holds another value, it returns the largest value with the same sign as the first parameter.

Java program to demonstrate example of nextAfter(double starts , double directions) method

// Java program to demonstrate the example of 
// nextAfter(double starts , double directions) method of Math Class.

public class NextAfterDoubleTypeMethod {
    public static void main(String[] args) {
        // declaring the variables
        double d1 = -2.6;
        double d2 = 0.0;
        double d3 = -0.6;
        double d4 = 7.0 / 0.0;

        // displaying the values
        System.out.println("d1: " + d1);
        System.out.println("d2: " + d2);
        System.out.println("d3: " + d3);
        System.out.println("d4: " + d4);

        // Here , we will get (-2.5 (approx.)) because we are passing 
        // parameter whose value is (-2.6,0.0)
        System.out.println("Math.nextAfter(d1,d2):" + Math.nextAfter(d1, d2));

        // Here , we will get (2.6) and we are passing parameter 
        // whose value is (0.0,-2.6)
        System.out.println("Math.nextAfter(d2,d1):" + Math.nextAfter(d2, d1));

        // Here , we will get (Double.MAX_VALUE) and we are passing parameter
        // whose value is (7.0/0.0,0.0)
        System.out.println("Math.nextAfter(d4,d2):" + Math.nextAfter(d4, d2));

        // Here , we will get (largest value) and we are passing parameter 
        // whose value is (0.0,7.0/0.0)
        System.out.println("Math. nextAfter(d2,d4):" + Math.nextAfter(d2, d4));
    }
}

Output

E:\Programs>javac NextAfterDoubleTypeMethod.java

E:\Programs>java NextAfterDoubleTypeMethod
d1: -2.6
d2: 0.0
d3: -0.6
d4: Infinity
Math.nextAfter(d1,d2):-2.5999999999999996
Math.nextAfter(d2,d1):-4.9E-324
Math.nextAfter(d4,d2):1.7976931348623157E308
Math. nextAfter(d2,d4):4.9E-324


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.