Home » Java programming language

Java StrictMath nextAfter() method with example

StrictMath Class nextAfter() method: Here, we are going to learn about the nextAfter() method of StrictMath Class with its syntax and example.
Submitted by Preeti Jain, on December 26, 2019

StrictMath Class nextAfter() method

Syntax:

    public static double nextAfter(double starts , double directions);
    public static float nextAfter(float starts , double directions);
  • nextAfter() method is available in java.lang package.
  • nextAfter(double starts, double directions) method is used to return the double floating-point number adjacent to the first parameter (starts) in the direction of the second parameter (directions).
  • nextAfter(float starts, double directions) method is used to return the float type floating-point number adjacent to the first parameter (starts) in the direction of the second parameter (directions).
  • These methods don't throw an exception. These are static methods, it is accessible with the class name and, if we try to access these methods with the class object then we will not get any error.

Parameter(s):

  • start – it represents the initial or starting floating-point value of float or double type.
  • directions – it represents the value denoting which of the given first parameter neighbors (start's neighbour) or start is returned.

Return value:

The return type of this method is float / double – it returns the float type floating point number adjacent to start in the direction of second argument.

Note:

  • If we pass the same value in both arguments, the method returns the same value.
  • If we pass Float.MIN_VALUE / Double.MIN_VALUE as the first argument and the second argument contains another value, the method returns the smaller value.
  • If we pass an infinity as the first argument and the second argument contains another value, the method returns the Float.MAX_VALUE / Double.MAX_VALUE with the sign of the first argument.
  • If we pass Float.MAX_VALUE / Double.MAX_VALUE as the first argument and the second argument contains another value, the method returns the largest value with the sign of the first argument.

Example:

// Java program to demonstrate the example 
// of nextAfter() method of StrictMath class

public class NextAfter {
    public static void main(String[] args) {
        // variable declarations
        double d1 = -2.6;
        double d2 = 0.0;
        double d3 = -0.6;
        double d4 = 7.0 / 0.0;

        float f1 = -2.6f;
        float f2 = 0.0f;
        double d5 = -7.0 / 0.0;


        // Display previous value of d1,d2,d3 and d4  
        System.out.println("d1: " + d1);
        System.out.println("d2: " + d2);
        System.out.println("d3: " + d3);
        System.out.println("d4: " + d4);


        // Display previous value of f1,f2 and d5  
        System.out.println("f1: " + f1);
        System.out.println("f2: " + f2);
        System.out.println("d5: " + d5);


        System.out.println("nextAfter(double, double): ");

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

        // Here , we will get (-4.9(approx)) and we are 
        // passing parameter whose value is (0.0,-2.6)
        System.out.println("StrictMath.nextAfter (d2,d1): " + StrictMath.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("StrictMath.nextAfter (d4,d2): " + StrictMath.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("StrictMath.nextAfter (d2,d4): " + StrictMath.nextAfter(d2, d4));

        System.out.println();
        System.out.println("nextAfter(float, double): ");


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

        // Here , we will get (Float.MAX_VALUE) and we are 
        // passing parameter whose value is (0.0f,-7.0/0.0)
        System.out.println("StrictMath. nextAfter(f2,d5): " + StrictMath.nextAfter(f2, d5));

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

        // Here , we will get (smallest value) and we are
        // passing parameter whose value is (0.0f,-7.0/0.0)
        System.out.println("StrictMath. nextAfter(f2,d5): " + StrictMath.nextAfter(f2, d5));
    }
}

Output

d1: -2.6
d2: 0.0
d3: -0.6
d4: Infinity
f1: -2.6
f2: 0.0
d5: -Infinity
nextAfter(double, double): 
StrictMath.nextAfter (d1,d2): -2.5999999999999996
StrictMath.nextAfter (d2,d1): -4.9E-324
StrictMath.nextAfter (d4,d2): 1.7976931348623157E308
StrictMath.nextAfter (d2,d4): 4.9E-324

nextAfter(float, double): 
StrictMath. nextAfter (f1,d3): -2.5999997
StrictMath. nextAfter(f2,d5): -1.4E-45
StrictMath. nextAfter(f1,d2): -2.5999997
StrictMath. nextAfter(f2,d5): -1.4E-45






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.