What is Math.nextUp in Java?
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
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 .
Special cases
- The method returns
NaNif the passed value is NaN. - The method returns
positive infinityif the passed value is . - The method returns
Double.MIN_VALUEorFloat.MIN_VALUEif 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));}}