Thursday, July 4, 2024
HomeLanguagesJavaStrictMath nextAfter() Method in Java

StrictMath nextAfter() Method in Java

  1. The nextAfter(double start, double direction) is the inbuilt method of StrictMath class in Java which is used to get the float number, adjacent to the start in the direction of the second argument direction. This method gives rise to few conditions depending on the type and variation of parameters.
    • The result is the second argument when both the arguments are equal.
    • The result is NaN when any one of the arguments is a NaN.
    • The result is the direction without any change when both arguments are signed zeros.
    • The result is a zero with the same sign as start when start is ±Double.MIN_VALUE and direction has a value such that the result has a smaller magnitude.
    • The result is the Double.MAX_VALUE with the same sign as start when start is infinite and direction has a value such that the result should have a smaller magnitude.
    • The result is an infinity with same sign of start when start is equal to ±Double.MAX_VALUE and direction has a value such that the result should have a larger magnitude.

    Syntax:

    public static double nextAfter(double start, double direction)

    Parameters: The method accepts two parameters:

    • start: This is of double type which refers to the starting floating-point value.
    • direction: This is of double type and refers to the value which indicates whether the start’s neighbors or start should be returned.

    Return Value: The method returns the floating point number that is adjacent to start in the direction of direction.

    Examples :

    Input: start = 72.74d
           direction = 1.2d
    
    Output: 72.73999999999998
    

    Below programs illustrate the java.lang.StrictMath.nextAfter() method:




    // Java program to demonstrate nextAfter()
      
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            double start = 23.62d, direction = 0.0d;
      
            double result = StrictMath.nextAfter(start, direction);
            System.out.println("The NextAfter is = " + result);
      
            result = StrictMath.nextAfter(start, 9.2d);
            System.out.println("The NextAfter is = " + result);
      
            result = StrictMath.nextAfter(direction, 7.2d);
            System.out.println("The NextAfter is =" + result);
      
            // Returns 0 if both arguments are zero
            result = StrictMath.nextAfter(direction, 0.0d);
            System.out.println("The NextAfter is =" + result);
        }
    }

    
    
    Output:

    The NextAfter is = 23.619999999999997
    The NextAfter is = 23.619999999999997
    The NextAfter is =4.9E-324
    The NextAfter is =0.0
    
  2. The nextAfter(float start, double direction) is an inbuilt method of StrictMath class in Java which is used to get the float number which is adjacent to the start in the direction of the second argument direction .
    • The result is the second argument when both the arguments are equal.
    • The result is NaN when any one of the arguments is a NaN.
    • The result is the direction without any change when both arguments are signed zeros.
    • The result is a zero with the same sign as start when start is ±Float.MIN_VALUE and direction has a value such that the result should have a smaller magnitude.
    • The result is the Float.MAX_VALUE with the same sign as start when start is infinite and direction has a value such that the result should have a smaller magnitude.
    • The result is an infinity with same sign of start when start is equal to ±Float.MAX_VALUE and direction has a value such that the result should have a larger magnitude.

    Syntax:

    public static double nextAfter(float start, double direction)

    Parameters: The method accepts two parameters:

    • start: This is of float type which refers to the starting floating-point value.
    • direction: This is of double type and refers to the value which indicates whether the start’s neighbors or start should be returned.

    Return Value: The method returns the floating point number, adjacent to start in the direction of direction.

    Examples :

    Input: start = 22.2f
           direction = 0.0d
    
    Output: 22.20000076293945
    

    Below programs illustrate the java.lang.StrictMath.nextAfter() method:




    // Java program to demonstrate nextAfter()
      
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            double start = 42.9f;
            double direction = 0.0d;
      
            double result = StrictMath.nextAfter(start, direction);
            System.out.println("The NextAfter is = " + result);
      
            result = StrictMath.nextAfter(start, 9.2d);
            System.out.println("The NextAfter is = " + result);
      
            result = StrictMath.nextAfter(direction, 7.2d);
            System.out.println("The NextAfter is =" + result);
      
            // Returns 0 if both arguments is zero
            result = StrictMath.nextAfter(direction, 0.0d);
            System.out.println("The NextAfter is =" + result);
        }
    }

    
    
    Output:

    The NextAfter is = 42.9000015258789
    The NextAfter is = 42.9000015258789
    The NextAfter is =4.9E-324
    The NextAfter is =0.0
    

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments