English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Java Math Mathematische Methoden
Java Math sqrt()方法返回指定数字的平方根。
sqrt()方法的语法为:
Math.sqrt(double num)
注意:sqrt()是静态方法。因此,我们可以使用类名访问该方法。
num -要计算平方根的数字
返回指定数字的平方根
如果参数小于0或NaN,则返回NaN
注意:此方法始终返回正数并正确舍入。
class Main { public static void main(String[] args) { //Erstellen Sie eine Double-Variable double value1 = Double.POSITIVE_INFINITY; double value2 = 25.0; double value3 = -16; double value4 = 0.0; //Quadratwurzel von Unendlichkeit System.out.println(Math.sqrt(value1)); // Infinite //Quadratwurzel von Positiv System.out.println(Math.sqrt(value2)); // 5.0 //Quadratwurzel von Negativ System.out.println(Math.sqrt(value3)); // NaN //Quadratwurzel von Null System.out.println(Math.sqrt(value4)); // 0.0 } }
In diesem Beispiel haben wir die Math.sqrt()-Methode verwendet, um die Quadratwurzeln von Unendlichkeit, positiven, negativen und Null zu berechnen.
Hier wird Double.POSITIVE_INFINITY verwendet, um im Programm positive Unendlichkeit zu implementieren.
Wenn wir int-Werte an die sqrt()-Methode übergeben, wandelt sie die int-Werte automatisch in double-Werte um.
int a = 36; Math.sqrt(a); // Zurück 6.0