Java 常用数学方法

Java  Match.sqrt()方法用于求某个数的算术平方根。

语法

语法如下:
public static double sqrt(double a) 

    参数

    • a:指定要求平方根的数

    返回值

    返回a的算术平方根。

    如果参数小于0或NaN,则返回NaN。

    例子

    public class MathSqrtExample {
        public static void main(String[] args) {
            System.out.println("2的算术平方根为:"+Math.sqrt(2));
            System.out.println("9的算术平方根为:"+Math.sqrt(9));
            System.out.println("-9的算术平方根为:"+Math.sqrt(-9));//负数没有
    
        }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    输出:

    2的算术平方根为:1.4142135623730951
    9的算术平方根为:3.0
    -9的算术平方根为:NaN

    负数没有算术平方根。