Java Math hüpoti () meetod arvutab x2 + y2 (st hüpotenuusi) ruutjuure ja tagastab selle.
Meetodi süntaks hypot()
on:
Math.hypot(double x, double y)
Märkus : hypot()
meetod on staatiline meetod. Seega saame meetodile helistada otse, kasutades klassi nime Math
.
hüpot () parameetrid
- x, y - topelttüüpi argumendid
hüpot () Tagastusväärtused
- tagastab Math.sqrt (x 2 + y 2 )
Tagastatav väärtus peaks jääma double
andmetüübi vahemikku .
Märkus . Math.sqrt()
Meetod tagastab määratud argumentide ruutjuure. Lisateabe saamiseks külastage Java Math.sqrt ().
Näide 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 ) )
Näide 2: Pythagorase teoreem Math.hypot () abil
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 ) )
Ülaltoodud näites oleme Math.hypot()
kolmnurga hüpotenuusi arvutamiseks kasutanud meetodit ja Pythagorase teoreemi.