Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
math
floatingpoint
doublevalue
communitycreator

What is Math.nextUp in Java?

abhilash

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 of the Math class is a static method that returns the floating point or double value that is next to the provided floating point or double value in the direction of positive infinity++\infty.

The nextUp method is defined in the Math class. The Math class is defined in the java.lang package.

To import the Math class, use the following import statement.

import java.lang.Math;

Syntax

public static double nextUp(double d)

Parameter

double d: a value of the double type.

Return value

nextUp returns the nearest double value next to the given value in the direction of ++\infty.

Special cases

  1. The method returns NaN if the passed value is NaN.
  2. The method returns positive infinity if the passed value is ++\infty.
  3. The method returns Double.MIN_VALUE or Float.MIN_VALUE if the passed value is zero.

Overloaded method

public static float nextUp(float f)

Code

In the following code, we pass different double and float values to the nextUp function.

public class Main{

    public static void main(String[] args){
        double val = -1.0 /0;
        System.out.println(Math.nextUp(1));
        System.out.println(Math.nextUp(5.0f));
        System.out.println(Math.nextUp(val));
        System.out.println(Math.nextUp(0));
    }
}

RELATED TAGS

java
math
floatingpoint
doublevalue
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