Java Math nextAfter() Function

nextafter function in java

Java Math Class

Java Math class provides several methods to perform several operations on math calculations like max(), min(), sin(), cos(), round(), ceil(), floor(), nextafter function etc.
The java.lang.Math class contains various methods for performing basic numeric operations

Here, in the page we will discuss about the nextafter() Function in java.

Java nextAfter Function :

Java Collection contains Math class and The class Math contains methods for performing several basic and advance numeric operations such as the exponential, logarithm, square root, and trigonometric functions.
The nextAfter() Function in Java returns the floating-point number adjacent to the first argument in the direction of the second argument. For example, if the first argument is 1.7 and the second argument is 0.3, the adjacent number of 1.7 in direction of 0.3 is 1.699999999999999.
It is a static function of java math class and can be accessed using the math class.

Syntax:

Math.nextAfter(num1,num2);

Definition of Parameters:

Return Type :

  • If argument is NaN, nextafter method will return NaN.
  • If both arguments are 0, the direction will be unchanged.
  • If first argument is equal to Double.MIN_VALUE or Float.MIN_VALUE and second argument has a value such that the result have to be a smaller value, nextAfter method will return Zero.
  • If first argument is equal to Double.MAX_VALUE or Float.MAX_VALUE and second Argument has a value such that the result have to be a lager value, nextAfter method will return Infinite.
  • If first argument is infinity and the second argument has a value such that the result should have a smaller value, then nextAfter method will return Double.MAX_VALUE.

Example :

Run
import java.util.*;

public class Main{
  public static void main(String[] args){
    // Example for integer Value
    int num1 = 5;
    int num2 = 2;
    
    // assigning adjacent value
    float ans = Math.nextAfter(num1,num2);
    
    // Printing the adjacent number
    System.out.print("Next Adjacent Number towards Second Value is : " +ans);
  }
}
Output:

Next Adjacent Number towards Second Value is : 4.9999995

In the above Example, We had taken an Integer value. The nextAfter() Function returns the floating-point number adjacent to the first input in the direction of the second input.

Example for Different Data Values:

Run
import java.util.*;

public class Main{
  public static void main(String[] args){
     // floating point arguments
    float num1 = 0.6f;
    float num2 = 1.8f;
    System.out.println("Next Adjacent Number towards Second Number is : " 
    + Math.nextAfter(num1,num2));

    // double arguments
    double num1 = 1.5;
    double num2 = 1.0;
    System.out.println("Next Adjacent Number towards Second Number is : " 
    + Math.nextAfter(num1,num2));

    // positive infinity as input
    double num2 = 1.0;
    System.out.println("Next Adjacent Number towards Second Number is : " 
    + Math.nextDown(Double.NEGATIVE_INFINITY,num2));

    // NaN argumnet
    double num2 = 1.0;
    System.out.println("Next Adjacent Number towards Second Number is : " 
    + Math.nextDown(Math.sqrt(-5)),num2);
  }
}
Output:

Next Adjacent Number towards Negative Infinity is : 0.59999996
Next Adjacent Number towards Negative Infinity is : 1.4999999999999998
Next Adjacent Number towards Negative Infinity is : -Infinity
Next Adjacent Number towards Negative Infinity is : NaN

In the above Example, We had taken examples for floating point arguments, double arguments, Negative infinity and a NaN value as arguments in the code. The nestAfter() method will return the floating-point number adjacent to the first input in the direction of the Second input. 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription