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 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 retuns 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    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 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 retuns 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:

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


My Personal Notes arrow_drop_up

lets make it a lil simple

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.