Popular Tutorials
Start Learning JavaCertification Courses
Created with over a decade of experience and thousands of feedback.
Java Math nextDown()
The Java Math nextDown() method returns a number adjacent to the specified argument in the direction of the negative infinity.
That is, if the argument is 6.7, then the adjacent number of 6.7 in direction of negative infinity is 6.699999999999999.
The syntax of the nextDown() method is:
Math.nextDown(start)
Note: The nextDown() method is a static method. Hence, we can call the method directly using the class name Math.
nextDown() Parameters
- start - starting number whose adjacent number is to be returned
Note: The data type of start can be either float or double.
nextDown() Return Values
- returns the number adjacent to start towards negative infinity
- returns NaN if start is NaN
- returns negative infinity if start is negative infinity
Note: The nextDown() method is equivalent to the Math.nextAfter(start, Double.Negative_INFINITY).
Example: Java Math.nextDown()
class Main {
public static void main(String[] args) {
// float arguments
float start1 = 7.9f;
System.out.println(Math.nextDown(start1)); // 7.8999996
// double arguments
double start2 = 7.9;
System.out.println(Math.nextDown(start2)); // 7.8999999999999995
// with positive infinity
double infinity = Double.NEGATIVE_INFINITY;
System.out.println(Math.nextDown(infinity)); // -Infinity
// with NaN
double nan = Math.sqrt(-5);
System.out.println(Math.nextDown(nan)); // NaN
}
}
Here, we have used the Java Math.sqrt(-5) method to calculate the square root of -5. Since, the square root of negative number is not a number, Math.nextDown(nan) returns NaN.
The Double.NEGATIVE_INFINITY is a field of Double class that allows us to implement infinity in a program.
Also Read: