Wednesday, July 3, 2024
HomeLanguagesJavaJava Math nextUp() method with Examples

Java Math nextUp() method with Examples

The java.lang.Math.nextUp() is a built-in math function in java which returns the floating-point value adjacent to the parameter provided in the direction of positive infinity.nextUp() method is overloaded which means that we have more than one method with the same name under the Math class.Two overloaded method of the nextUp() :

  • double type : nextUp(double d)
  • float type : nextUp(float f)

Note :

  • If the argument is NaN, the result is NaN.
  • If the argument is zero, the result is Double.MIN_VALUE if we are dealing with
    double and if it’s float then the result is Float.MIN_VALUE.
  • If the argument is positive infinity, the result is positive infinity.

Syntax :

public static dataType nextUp(dataType g)
Parameter :
 g : an input for starting floating-point value.
Return :
The nextUp() method returns the adjacent floating-point value closer to 
 positive infinity.

Example :To show working of java.lang.Math.nextUp() method.




// Java program to demonstrate working
// of java.lang.Math.nextUp() method
import java.lang.Math;
  
class Gfg {
  
    // driver code
    public static void main(String args[])
    {
        double g = 69.19;
  
        // Input double value, Output adjacent floating-point
        System.out.println(Math.nextUp(g));
  
        float gf = 78.1f;
  
        // Input float value, Output adjacent floating-point
        System.out.println(Math.nextUp(gf));
  
        double a = 0.0 / 0;
  
        // Input NaN, Output NaN
        System.out.println(Math.nextUp(a));
  
        float b = 0.0f;
  
        // Input zero, Output Float.MIN_VALUE for float
        System.out.println(Math.nextUp(b));
  
        double c = 1.0 / 0;
  
        // Input positive infinity, Output positive infinity
        System.out.println(Math.nextUp(c));
    }
}


Output:

69.19000000000001
78.100006
NaN
1.4E-45
Infinity
Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments