What is the StrictMath.nextDown(double) method in Java?
StrictMath class
The StrictMath class has utility methods for carrying out basic numeric operations. This class is present in the java.lang package.
nextDown method
The nextDown method returns the next adjacent number (double value) of the argument in the direction of negative infinity.
Syntax
public static double nextDown(double d)
Parameter
d: This is a double value for which the next adjacent number in the direction of negative infinity is to be returned.
Return value
-
This method returns a double value, which is the next adjacent number of the argument in the direction of negative infinity.
-
It returns
NaNif the argument isNaN. -
It returns
-Double.MIN_VALif the value of the argument is0. -
It returns
-Infinityif the argument passed is negative infinity.
Code
The code given below shows us how to use the nextDown(double value) method:
class NextDownExample {public static void main( String args[] ) {double num = 10.0;System.out.println("StrictMath.nextDown(10.0) = " + StrictMath.nextDown(num));num = 10.000000000000001;System.out.println("StrictMath.nextDown(10.000000000000001) = " + StrictMath.nextDown(num));num = -(1.0f/0.0f); // negative infinitySystem.out.println("StrictMath.nextDown(-(1.0/0.0)) = " + StrictMath.nextDown(num));num = 0; //passing 0 as parameterSystem.out.println("StrictMath.nextDown(0) = " + StrictMath.nextDown(num));// checking if passing 0 actually returns -Double.MIN_VALUESystem.out.println("StrictMath.nextDown(0) == -Double.MIN_VALUE :" + (StrictMath.nextDown(num) == -Double.MIN_VALUE));}}
Code explanation
In the code given above:
-
In line 3, we create a
doublevariable callednumwith a value of10.0. -
In line 4, we use the
StrictMath.nextDownfunction to find the next adjacent value of the10.0in the direction of negative infinity. For the number10.0, the next adjacent value is9.999999999999998. -
In lines 6 and 7, we change the value of
numto10.000000000000001and call thenextDownmethod with num as an argument. The next adjacent value in the negative infinity direction of thenumis10.0. -
In lines 9 and 10, we change the value of
numto-(1.0/0.0)(negative infinity) and call thenextDownmethod withnumas an argument. The next adjacent value in the negative infinity direction is-infinity. -
In lines 12 and 13, we change the value of
numto0and call thenextDownmethod withnumas an argument. The next adjacent value in the negative infinity direction is-Double.MIN_VALUE( -4.9E-324).