Die Java Math hypot () -Methode berechnet die Quadratwurzel von x2 + y2 (dh Hypotenuse) und gibt sie zurück.
Die Syntax der hypot()
Methode lautet:
Math.hypot(double x, double y)
Hinweis : Die hypot()
Methode ist eine statische Methode. Daher können wir die Methode direkt unter Verwendung des Klassennamens aufrufen Math
.
hypot () Parameter
- x, y - Argumente vom Typ Double
hypot () Rückgabewerte
- gibt Math.sqrt zurück (x 2 + y 2 )
Der zurückgegebene Wert sollte im Bereich des double
Datentyps liegen.
Hinweis : Die Math.sqrt()
Methode gibt die Quadratwurzel der angegebenen Argumente zurück. Weitere Informationen finden Sie unter Java Math.sqrt ().
Beispiel 1: Java Math.hypot ()
class Main ( public static void main(String() args) ( // create variables double x = 4.0; double y = 3.0; //compute Math.hypot() System.out.println(Math.hypot(x, y)); // 5.0 ) )
Beispiel 2: Satz von Pythagoras mit Math.hypot ()
class Main ( public static void main(String() args) ( // sides of triangle double side1 = 6.0; double side2 = 8.0; // According to Pythagoras Theorem // hypotenuse = (side1)2 + (side2)2 double hypotenuse1 = (side1) *(side1) + (side2) * (side2); System.out.println(Math.sqrt(hypotenuse1)); // prints 10.0 // Compute Hypotenuse using Math.hypot() // Math.hypot() gives √((side1)2 + (side2)2) double hypotenuse2 = Math.hypot(side1, side2); System.out.println(hypotenuse2); // prints 10.0 ) )
Im obigen Beispiel haben wir die Math.hypot()
Methode und den Pythagoras-Satz verwendet, um die Hypotenuse eines Dreiecks zu berechnen.