Java Math nextDown() Function

Anonymous Class

Java Math Class

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

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

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

Syntax:

Math.nextDown(num);

Definition of Parameters:

Return Type :

  • If the input is Negative Infinity, nextDown() method will return Negative Infinity.
  • If the input is Zero, nextDown() 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.nextDown(num);
    
    // Printing the adjacent number
    System.out.print("Next Adjacent Number towards Negative Infinity is : " +ans);
  }
}
Output:

Next Adjacent Number towards Negative Infinity is : 4.9999995

In the above Example, We had taken an Integer value. The nextDown() Function returns the floating-point number adjacent to the given input in the direction of the negative 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 Negative Infinity is : " 
    + Math.nextDown(num1));

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

    // positive infinity as input
    System.out.println("Next Adjacent Number towards Negative Infinity is : " 
    + Math.nextDown(Double.NEGATIVE_INFINITY));

    // NaN argumnet
    System.out.println("Next Adjacent Number towards Negative Infinity is : " 
    + Math.nextDown(Math.sqrt(-5)));
  }
}
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 nestDown() 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