Java Math hipots ()

Java Math hipotēkas () metode aprēķina x2 + y2 (ti, hipotenūza) kvadrātsakni un to atgriež.

Metodes sintakse hypot()ir šāda:

 Math.hypot(double x, double y)

Piezīme . hypot()Metode ir statiska. Tādējādi mēs varam izsaukt metodi tieši, izmantojot klases nosaukumu Math.

hipot () parametri

  • x, y - dubultā tipa argumenti

hipot () Atgriezties vērtības

  • atgriež Math.sqrt (x 2 + y 2 )

Atgrieztajai vērtībai jābūt doubledatu veida diapazonā .

Piezīme . Math.sqrt()Metode atgriež norādīto argumentu kvadrātsakni. Lai uzzinātu vairāk, apmeklējiet Java Math.sqrt ().

1. piemērs: 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 ) )

2. piemērs: Pitagora teorēma, izmantojot 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 ) )

Iepriekš minētajā piemērā mēs izmantojām Math.hypot()metodi un Pitagora teorēmu, lai aprēķinātu trijstūra hipotenūzu.

Interesanti raksti...