Java Math hypot() method with Example

java.lang.Math.hypot()
2
2
  • If either argument is infinite, then the result is positive infinity.
  • If either argument is NaN and neither argument is infinite, then the result is NaN.
Syntax :
public static double hypot(double x, double y)
Parameter :
x and y are the values. 
Returns :
2
2
Example 1 :
// Java program to demonstrate working
// of java.lang.Math.hypot() method
import java.lang.Math;
  
class Gfg {
  
    // Driver code
    public static void main(String args[])
    {
        double x = 3;
        double y = 4;
          
        // when both are not infinity
        double result = Math.hypot(x, y);
        System.out.println(result);
  
        double positiveInfinity = 
               Double.POSITIVE_INFINITY;
        double negativeInfinity = 
               Double.NEGATIVE_INFINITY;
        double nan = Double.NaN;
  
        // when 1 or more argument is NAN
        result = Math.hypot(nan, y);
        System.out.println(result);
  
        // when both arguments are infinity
        result = Math.hypot(positiveInfinity, 
                            negativeInfinity);
        System.out.println(result);
    }
}

                    
Output:
5.0
NaN
Infinity

Contact Us