当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Math.sqrt()用法及代码示例


java.lang.Math.sqrt() 用于返回数字的平方根。

用法

public static double sqrt(double x)

参数

x= a value

返回

This method returns the square root of x.
  • 如果参数是双正值,则此方法将返回给定值的平方根。
  • 如果参数为 NaN 或小于零,则此方法将返回 NaN。
  • 如果参数为正无穷大,则此方法将返回正无穷大。
  • 如果参数为正零或负零,此方法将返回结果为零,符号相同。

例子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));
    }
}

输出:

9.0

例子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));
    }
}

输出:

NaN

例子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));
    }
}

输出:

NaN

示例 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));
    }
}

输出:

Infinity

例 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));
    }
}

输出:

0.0






相关用法


注:本文由纯净天空筛选整理自 Java Math.sqrt() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。