Metoda Java Math.sqrt().

The java.lang.Math.sqrt() służy do zwracania pierwiastka kwadratowego z liczby.

Składnia

 public static double sqrt(double x)  

Parametr

 x= a value  

Powrót

 This method returns the square root of x.  
  • Jeżeli argumentem jest dodatnia wartość double, ta metoda zwróci pierwiastek kwadratowy z danej wartości.
  • Jeśli argumentem jest NaN lub mniej niż zero, ta metoda zwróci NaN .
  • Jeśli argument jest pozytywny nieskończoność , ta metoda zwróci wartość dodatnią Nieskończoność .
  • Jeśli argument jest pozytywny lub negatywny Zero , ta metoda zwróci wynik jako Zero z tym samym znakiem.

Przykład 1

 public class SqrtExample1 { public static void main(String[] args) { double x = 81.0; // Input positive value, Output square root of x System.out.println(Math.sqrt(x)); } }  
Przetestuj teraz

Wyjście:

 9.0  

Przykład 2

 public class SqrtExample2 { public static void main(String[] args) { double x = -81.78; // Input negative value, Output NaN System.out.println(Math.sqrt(x)); } }  
Przetestuj teraz

Wyjście:

 NaN  

Przykład 3

 public class SqrtExample3 { public static void main(String[] args) { double x = 0.0/0; // Input NaN, Output NaN System.out.println(Math.sqrt(x)); } }  
Przetestuj teraz

Wyjście:

 NaN  

Przykład 4

 public class SqrtExample4 { public static void main(String[] args) { double x = 1.0/0; // Input positive infinity, Output positive infinity System.out.println(Math.sqrt(x)); } }  
Przetestuj teraz

Wyjście:

 Infinity  

Przykład 5

 public class SqrtExample5 { public static void main(String[] args) { double x = 0.0; // Input positive Zero, Output positive zero System.out.println(Math.cbrt(x)); } }  
Przetestuj teraz

Wyjście:

 0.0