Java Math hypot() method with Example

java.lang.Math.hypot() function is an inbuilt math function in Java that returns the Euclidean norm,  \sqrt{(x * x + y * y)} . The function returns sqrt(x2 + y2) without intermediate overflow or underflow.

  • 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 :
sqrt(x2 +y2) without intermediate overflow or underflow.



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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

5.0
NaN
Infinity


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.