Metoda Java Math.pow().
The java.lang.Math.pow() służy do zwracania wartości pierwszego argumentu podniesionej do potęgi drugiego argumentu. Typ zwracany przez metodę pow() to double.
Składnia
public static double pow(double a, double b)
Parametr
a= base b= exponent
Powrót
Ta metoda zwraca wartość a B
- Jeśli drugi argument jest dodatni lub ujemny Zero , ta metoda powróci 1,0 .
- Jeśli drugi argument nie jest liczbą (NaN) , ta metoda powróci NaN .
- Jeśli drugi argument to 1 , ta metoda zwróci wynik taki sam jak pierwszy argument .
Przykład 1
public class PowExample1 { public static void main(String[] args) { double x = 5; double y = 4; //returns 5 power of 4 i.e. 5*5*5*5 System.out.println(Math.pow(x, y)); } } Przetestuj teraz Wyjście:
625.0
Przykład 2
public class PowExample2 { public static void main(String[] args) { double x = 9.0; double y = -3; //return (9) power of -3 System.out.println(Math.pow(x, y)); } } Przetestuj teraz Wyjście:
0.0013717421124828531
Przykład 3
public class PowExample3 { public static void main(String[] args) { double x = -765; double y = 0.7; //return NaN System.out.println(Math.pow(x, y)); } } Przetestuj teraz Wyjście:
NaN
Przykład 4
public class PowExample4 { public static void main(String[] args) { double x = 27.2; double y = 1.0; // Second argument is 1 so output is 27.2 System.out.println(Math.pow(x, y)); } } Przetestuj teraz Wyjście:
3.5461138422596736