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 NaN if the argument is NaN.

  • It returns -Double.MIN_VAL if the value of the argument is 0.

  • It returns -Infinity if 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 infinity
System.out.println("StrictMath.nextDown(-(1.0/0.0)) = " + StrictMath.nextDown(num));
num = 0; //passing 0 as parameter
System.out.println("StrictMath.nextDown(0) = " + StrictMath.nextDown(num));
// checking if passing 0 actually returns -Double.MIN_VALUE
System.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 double variable called num with a value of 10.0.

  • In line 4, we use the StrictMath.nextDown function to find the next adjacent value of the 10.0 in the direction of negative infinity. For the number 10.0, the next adjacent value is 9.999999999999998.

  • In lines 6 and 7, we change the value of num to 10.000000000000001 and call the nextDown method with num as an argument. The next adjacent value in the negative infinity direction of the num is 10.0.

  • In lines 9 and 10, we change the value of num to -(1.0/0.0) (negative infinity) and call the nextDown method with num as an argument. The next adjacent value in the negative infinity direction is -infinity.

  • In lines 12 and 13, we change the value of num to 0 and call the nextDown method with num as an argument. The next adjacent value in the negative infinity direction is -Double.MIN_VALUE ( -4.9E-324).

Free Resources