Java Math hypot () -menetelmä laskee x2 + y2: n (ts. Hypotenuus) neliöjuuren ja palauttaa sen.
Menetelmän syntaksi hypot()
on:
Math.hypot(double x, double y)
Huomaa : hypot()
Menetelmä on staattinen menetelmä. Siksi voimme kutsua menetelmää suoraan luokan nimellä Math
.
hypot () -parametrit
- x, y - kaksinkertaisen tyyppiset argumentit
hypot () Palautusarvot
- palauttaa Math.sqrt (x 2 + y 2 )
Palautettavan arvon tulisi olla double
tietotyypin alueella.
Huomaa : Math.sqrt()
Menetelmä palauttaa määritettyjen argumenttien neliöjuuren. Lisätietoja on osoitteessa Java Math.sqrt ().
Esimerkki 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 ) )
Esimerkki 2: Pythagoras-lause lauseen Math.hypot () avulla
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 ) )
Yllä olevassa esimerkissä olemme käyttäneet Math.hypot()
menetelmää ja Pythagoras-teemaa laskemaan kolmion hypotenuusin.