Java Math nextUp() Function

nextUp Function in Java

Java Math Class

Here, in the page we will discuss about the nextUp() Function in java.
Java Math class provides several methods to perform several operations on math calculations like max(), min(), sin(), cos(), round(), ceil(), floor(), nextUp function etc.The java.lang.Math class contains various methods for performing basic numeric operations

Java nextUp 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 nextUp() Function in Java returns the floating-point number adjacent to the given input in the direction of the positive infinity. For example, if the input is 0.5, then the adjacent number of 0.5 in direction of negative infinity is 0.500000000000001.
It is a static function of java math class and can be accessed using the math class.

Syntax:

Math.nextUp(num);

Definition of Parameters:

Return Type :

  • If the input is Positive Infinity, nextUp() method will return Positive Infinity.
  • If the input is Zero, nextUp() method will return MIN_VALUE.
  • If the input is NaN, then the result is NaN.

Example :

Run
import java.util.*;

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

Output:

Next Adjacent Number towards Negative Infinity is : 5.0000005

In the above Example, We had taken an Integer value. The nextUp() Function returns the floating-point number adjacent to the given input in the direction of the Positive infinity.

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;
    System.out.println("Next Adjacent Number towards Positive Infinity is : " 
    + Math.nextUp(num1));

    // double arguments
    double num2 = 1.5;
    System.out.println("Next Adjacent Number towards Positive Infinity is : " 
    + Math.nextUp(num2));

    // positive infinity as input
    System.out.println("Next Adjacent Number towards Positive Infinity is : " 
    + Math.nextUp(Double.POSITIVE_INFINITY));

    // NaN argumnet
    System.out.println("Next Adjacent Number towards Positive Infinity is : " 
    + Math.nextUp(Math.sqrt(-5)));
  }
}

Output:

Next Adjacent Number towards Positive Infinity is : 0.6000001
Next Adjacent Number towards Positive Infinity is : 1.5000000000000002
Next Adjacent Number towards Positive Infinity is : Infinity
Next Adjacent Number towards Positive Infinity is : NaN

Explanation:

In the above Example, We had taken examples for floating point, Integwer of double type, Positive infinity and a NaN value as arguments in the code. The nestUp() method will return the floating-point number adjacent to the given input in the direction of the negative infinity. 

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