Skip to content
Related Articles
StrictMath hypot() Method in Java
• Last Updated : 04 Jun, 2019

According to basic geometry, hypotenuse is nothing but the longest side of a right-angled triangle. It is the side which is opposite to the right angle of the triangle. To find the length of the hypotenuse of a right-angled triangle, the Pythagorean theorem is applied. According to this theorem, given two perpendicular sides of a triangle of length p and b, the hypotenuse can be found by the formula .
The Java.lang.StrictMath.hypot() is an inbuilt method of StrictMath class which is used to get the hypotenuse or the square-root of summation of the square of given two sides or arguments i.e. . The method excludes all the intermediate overflow and underflow. It gives rise to few special results:

• The method returns positive infinity when either of num1 or num2 is infinite.
• It returns NAN when either any argument is NAN and neither argument is infinite.

Syntax:

public static double hypot(double num1, double num2)

Parameters: The method accepts two parameters of Double type:

• num1: This is the first value or any one side.
• num2: This is the second value or the other side.

Return Value: The method returns i.e. length of hypotenuse.
Examples :

Input: num1 = 3
num2 = 4

Output: 5.0


Below programs illustrate the Java.lang.StrictMath.hypot() Method:
Program 1:

 // Java praogram to illustrate the// Java.lang.StrictMath.hypot() Methodimport java.lang.*;  public class Geeks {      public static void main(String[] args)    {          double num1 = 11, num2 = 13.8;          // It returns the hypotenuse        double hypotlen = StrictMath.hypot(num1, num2);        System.out.println("Length of hypotenuse  of side "        + num1 + " & " + num2 + " = " + hypotlen);    }}
Output:
Length of hypotenuse  of side 11.0 & 13.8 = 17.647662734764623


Program 2:

 // Java praogram to illustrate the// Java.lang.StrictMath.hypot() Methodimport java.lang.*;  public class Geeks {      public static void main(String[] args)    {          double num1 = -54, num2 = -24.8;          // It returns the hypotenuse        double hypotlen = StrictMath.hypot(num1, num2);        System.out.println("Length of hypotenuse  of side "        + num1 + " & " + num2 + " = " + hypotlen);    }}
Output:
Length of hypotenuse  of side -54.0 & -24.8 = 59.422554640473


Program 3:

 // Java praogram to illustrate the// Java.lang.StrictMath.hypot() Methodimport java.lang.*;  public class Geeks {      public static void main(String[] args)    {          double num1 = 4;        double positive_Infinity = Double.POSITIVE_INFINITY;        double negative_Infinity = Double.NEGATIVE_INFINITY;        double nan = Double.NaN;          // When 1 or more argument is NAN        double hypotlen = StrictMath.hypot(nan, num1);        System.out.println("Hypotenuse length = " + hypotlen);          // When both arguments are infinity        hypotlen = StrictMath.hypot(positive_Infinity,                                    negative_Infinity);        System.out.println("Hypotenuse length = " + hypotlen);    }}
Output:
Hypotenuse length = NaN
Hypotenuse length = Infinity


Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up