strictmath_Java StrictMath sqrt()方法与示例

strictmath

StrictMath类sqrt()方法 (StrictMath Class sqrt() method)

  • sqrt() Method is available in java.lang package.

    sqrt()方法在java.lang包中可用。

  • sqrt() Method is used to find the square root of the given parameter in the method. Here, "sqrt" stands for square root

  • sqrt() Method is a static method so it is accessible with the class name and if we try to access the method with the class object then we will not get any error.
  • sqrt() Method does not throw any exception at the time of finding the square root of the given number.
  • sqrt()方法用于查找方法中给定参数的平方根。 在这里, “ sqrt”代表平方根

  • sqrt()方法是静态方法,因此可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会收到任何错误。
  • sqrt()方法在查找给定数字的平方根时不会引发任何异常。

Syntax:

句法:

    public static double sqrt(double d);

Parameter(s):

参数:

  • double d – represents for which we have to find the square root.

    double d –代表我们必须找到其平方根。

Return value:

返回值:

The return type of the method is double, it returns the square root of the given parameter.

该方法的返回类型为double ,它返回给定参数的平方根。

Note:

注意:

  • If we pass NaN, the method returns the same (i.e. NaN).

    如果我们通过NaN,则该方法返回相同的值(即NaN)。

  • If we pass zero, the method returns the same value with the same sign.

    如果传递零,则该方法返回具有相同符号的相同值。

  • If we pass an argument positive infinity, the method returns the same.

    如果我们传递参数正无穷大,则该方法将返回相同的结果。

Example:

例:

// Java program to demonstrate the example of sqrt(double d) 
// method of StrictMath class.

public class Sqrt {
    public static void main(String[] args) {
        // variable declarations
        double d1 = -0.0;
        double d2 = 0.0;
        double d3 = -7.0 / 0.0;
        double d4 = 7.0 / 0.0;
        double d5 = 1000.0;
        double d6 = -1000.0;

        // Display previous value of d1,d2,d3,d4,d5 and d6
        System.out.println("d1: " + d1);
        System.out.println("d2: " + d2);
        System.out.println("d3: " + d3);
        System.out.println("d4: " + d4);
        System.out.println("d5: " + d5);
        System.out.println("d6: " + d6);


        /*Here , we will get (-0.0) because we are passing parameter (-0.0) */
        System.out.println("StrictMath.sqrt(d1): " + StrictMath.sqrt(d1));

        // Here , we will get (0.0) because we are passing parameter 
        // (0.0) so the square root is the same
        System.out.println("StrictMath.sqrt(d2): " + StrictMath.sqrt(d2));

        // Here , we will get (NaN) because we are passing parameter 
        // (-7.0/0.0) so the square root is (-Infinity)
        System.out.println("StrictMath.sqrt(d3): " + StrictMath.sqrt(d3));

        // Here , we will get (Infinity) because we are passing 
        // parameter (7.0/0.0) so the square root is (Infinity)
        System.out.println("StrictMath.sqrt(d4): " + StrictMath.sqrt(d4));


        // Here , we will get (square root of given argument) because 
        // we are passing parameter (1000.0) 
        System.out.println("StrictMath.sqrt(d5): " + StrictMath.sqrt(d5));

        // Here , we will get (NaN) because we are passing parameter 
        // (-1000.0) 
        System.out.println("StrictMath.sqrt(d6): " + StrictMath.sqrt(d6));
    }
}

Output

输出量

d1: -0.0
d2: 0.0
d3: -Infinity
d4: Infinity
d5: 1000.0
d6: -1000.0
StrictMath.sqrt(d1): -0.0
StrictMath.sqrt(d2): 0.0
StrictMath.sqrt(d3): NaN
StrictMath.sqrt(d4): Infinity
StrictMath.sqrt(d5): 31.622776601683793
StrictMath.sqrt(d6): NaN


翻译自: https://www.includehelp.com/java/strictmath-sqrt-method-with-example.aspx

strictmath

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值