Metode Java Math hypot () menghitung akar kuadrat dari x2 + y2 (yaitu hipotenusa) dan mengembalikannya.
Sintaks dari hypot()
metode ini adalah:
Math.hypot(double x, double y)
Catatan : hypot()
Metode ini adalah metode statis. Karenanya, kita dapat memanggil metode secara langsung menggunakan nama kelas Math
.
Parameter hypot ()
- x, y - argumen tipe ganda
hypot () Mengembalikan Nilai
- mengembalikan Math.sqrt (x 2 + y 2 )
Nilai yang dikembalikan harus berada dalam kisaran double
tipe data.
Catatan : Math.sqrt()
Metode ini mengembalikan akar kuadrat dari argumen yang ditentukan. Untuk mempelajari lebih lanjut, kunjungi Java Math.sqrt ().
Contoh 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 ) )
Contoh 2: Teorema Pythagoras Menggunakan 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 ) )
Dalam contoh di atas, kami telah menggunakan Math.hypot()
metode dan Teorema Pythagoras untuk menghitung sisi miring segitiga.