What is the Math.nextDown method in Java?
The nextDown method returns the next adjacent number (floating-point value) of the argument in the direction of negative infinity.
Syntax
float Math.nextDown(float num);
double Math.nextDown(double num);
Parameter
num: The number for which the next adjacent number in the direction of negative infinity is to be returned. num can be either float or double.
Return value
-
Returns the next adjacent number of the argument in the direction of negative infinity.
-
Returns
NaNif the argument is NaN. -
Returns
-Float.MIN_VALif the argument type is float and the value of the argument is0. -
Returns
-Double.MIN_VALif the argument type is double and the value of the argument is0. -
Returns
-Infinityif the argument passed is negative infinity.
Code
class HelloWorld {public static void main( String args[] ) {float num = 10.0f; // we can als use double type with Math.nextDownSystem.out.println("Math.nextDown(10.0) = " + Math.nextDown(num));num = 10.000001f;System.out.println("Math.nextDown(10.000001f) = " + Math.nextDown(num));num = -(1.0f/0.0f); // negative infinitySystem.out.println("Math.nextDown(-(1.0/0.0)) = " + Math.nextDown(num));num = 0;System.out.println("Math.nextDown(0) = " + Math.nextDown(num));System.out.println("Math.nextDown(0) == -Float.MIN_VALUE :" + (Math.nextDown(num) == -Float.MIN_VALUE));}}
In the code above, we used the Math.nextDown function to find the next adjacent floating-point value of the arguments in the direction of negative infinity.
-
For the number
10.0, the next adjacent floating-point value is9.999999. -
For the number
10.0000001, the next adjacent floating-point value in the negative infinity direction is10.0. -
For the number
-(1.0/0.0)(negative infinity), the next adjacent floating-point value in the negative infinity direction is-infinity. -
For the number
0, the next adjacent floating-point value in the negative infinity direction is-Float.MIN_VALUE (-1.4E-45).