Metoda Java Math hypot () izračunava kvadratni korijen x2 + y2 (tj. Hipotenuze) i vraća ga.
Sintaksa hypot()
metode je:
Math.hypot(double x, double y)
Napomena : hypot()
Metoda je statička metoda. Stoga metodu možemo pozvati izravno koristeći naziv klase Math
.
hipot () parametri
- x, y - argumenti dvostrukog tipa
hypot () Povratne vrijednosti
- vraća Math.sqrt (x 2 + y 2 )
Vraćena vrijednost trebala bi biti u rasponu vrste double
podataka.
Napomena : Math.sqrt()
Metoda vraća kvadratni korijen navedenih argumenata. Da biste saznali više, posjetite Java Math.sqrt ().
Primjer 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 ) )
Primjer 2: Pitagorin teorem pomoću 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 ) )
U gornjem primjeru koristili smo Math.hypot()
metodu i Pitagorin teorem za izračunavanje hipotenuze trokuta.