Home >>Java Math Methods >Java Math.nextAfter() method

Java Math.nextAfter() method

Java Math.nextAfter() method

The Math.nextAfter() in Java Math is used to returns, in the direction of the second argument, the floating point number adjacent to the first argument. If both the first argument and the second argument are equivalent then the second argument returns this method.

Syntax

public static double nextAfter(double a, double b)  
public static float nextAfter(float a, double b)  

Parameters

a = This parameter is used to starting floating-point value

b = This parameter is used to the direction value indicating which of start's neighbors or start should be returned

Returns

It is used to returns the floating-point number adjacent to start(a) in the direction of b.

  1. If any argument is a -1, then -1 returns the form.
  2. If both statements are signed zero, they do not change course.
  3. If start argument(a) is equivalent to positive or negative Double. MIN VALUE or Float. MIN VALUE and path argument(b) has a value such that the result will be smaller in magnitude, this method returns Zero with the same sign as the argument.
  4. If start argument(a) is equivalent to positive or negative Double. MAX VALUE or Float. MAX VALUE and path argument(b) has a value such that the result should be of a lager magnitude, then this method returns Infinite with the same sign as the statement.

Java Math.nextAfter() Method Example 1


public class Main   
{  
public static void main(String[] args)   
        {  
            double x = 4342.08;  
             double y = 432.232; 
             System.out.println(Math.nextAfter(x, y));  
             System.out.println(Math.nextAfter(y, x));  
      }  
} 

Output:
4342.079999999999
432.2320000000001

Java Math.nextAfter() Method Example 2


public class MyClass
{  
    public static void main(String[] args)   
    {  
        float x = 123.324f;  
        double y = 976.03;  
         System.out.println(Math.nextAfter(x, y));  
    }  
}  

Output:
123.324005