Metoden Java Math hypot () beräknar kvadratroten av x2 + y2 (dvs. hypotenus) och returnerar den.
Syntaxen för hypot()
metoden är:
Math.hypot(double x, double y)
Obs : Den hypot()
metoden är en statisk metod. Därför kan vi ringa metoden direkt med hjälp av klassnamnet Math
.
hypot () Parametrar
- x, y - argument av dubbel typ
hypot () Returvärden
- returnerar Math.sqrt (x 2 + y 2 )
Det returnerade värdet bör ligga inom datatypens intervall double
.
Obs : Math.sqrt()
Metoden returnerar kvadratroten för angivna argument. För att lära dig mer, besök Java Math.sqrt ().
Exempel 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 ) )
Exempel 2: Pythagoras-satsen med 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 ) )
I exemplet ovan har vi använt Math.hypot()
metoden och Pythagoras teorem för att beräkna hypotenusen i en triangel.