Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the Math.nextDown method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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

  • Returns -Float.MIN_VAL if the argument type is float and the value of the argument is 0.

  • Returns -Double.MIN_VAL if the argument type is double and the value of the argument is 0.

  • Returns -Infinity if 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.nextDown
System.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 infinity
System.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 is 9.999999.

  • For the number 10.0000001, the next adjacent floating-point value in the negative infinity direction is 10.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).

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring