Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

math
java
nextup
floatingpoint
communitycreator

# What is Math.nextUp 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 nextUp method will return the next adjacent number (floating-point value) of the argument in the direction of positive Infinity.

### Syntax

float Math.nextUp(float num);
double Math.nextUp(double num);


### Argument

• num: The number for which the next adjacent number in the direction of positive Infinity is to be returned. The num can be either float or double.

### Return value

• The nextUp() method returns the next adjacent number of the argument in the direction of positive Infinity.

• It returns NaNNot a Number if the argument passed is NaN.

• Method returns Float.MIN_VAL if the argument type is float and the value of the argument is 0.

• It returns Double.MIN_VAL if the argument type is double and the value of the argument is 0.

• nextUp() returns Infinity if the argument passed is POSITIVE_INFINITY.

### Code

class HelloWorld {    public static void main( String args[] ) {        float num = 10.0f; // we can als use double type with Math.nextUp        System.out.println("Math.nextUp(10.0) = " + Math.nextUp(num));        num = 10.999999f;        System.out.println("Math.nextUp(10.999999f) = " + Math.nextUp(num));        num = (1.0f/0.0f);  // positive infinity        System.out.println("Math.nextUp(1.0/0.0) = " + Math.nextUp(num));        num = 0;        System.out.println("Math.nextUp(0) = " + Math.nextUp(num));        System.out.println("Math.nextUp(0) == Float.MIN_VALUE :" + (Math.nextUp(num) == Float.MIN_VALUE));    }}

### Explanation

In the code above, we used the Math.nextUp 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 in the positive infinity direction is 10.000001.

• For the number 10.999999, the next adjacent floating-point value in the positive infinity direction is 11.0.

• For the number (1.0/0.0)(POSITIVE_INFINITY), the next adjacent floating-point value in the positive infinity direction is Infinity.

• For the number 0, the next adjacent floating-point in the positive infinity direction value is Float.MIN_VALUE(1.4E-45).

RELATED TAGS

math
java
nextup
floatingpoint
communitycreator

CONTRIBUTOR

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.

Keep Exploring

Learn in-demand tech skills in half the time 