Open In App

StrictMath hypot() Method in Java

Last Updated : 13 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 $\sqrt{x^2+y^2}$
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. $\sqrt{num1^2+num2^2}$  . 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 $\sqrt{num1^2+num2^2}$  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

// Java program to illustrate the
// Java.lang.StrictMath.hypot() Method
import 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

// Java program to illustrate the
// Java.lang.StrictMath.hypot() Method
import 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

// Java program to illustrate the
// Java.lang.StrictMath.hypot() Method
import 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

 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads