java中的nath.sqrt,Java StrictMath sqrt()方法与示例

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

sqrt()方法用于查找方法中给定参数的平方根。在这里,“ sqrt”代表平方根

sqrt()方法是静态方法,因此可以使用类名称进行访问,如果尝试使用类对象访问该方法,则不会收到任何错误。

sqrt()方法在查找给定数字的平方根时不会引发任何异常。

语法:public static double sqrt(double d);

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

返回值:

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

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

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

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

示例//Java程序演示sqrt(double d)的示例

//StrictMath类的方法。

public class Sqrt {

public static void main(String[] args) {

//变量声明

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;

//显示d1,d2,d3,d4,d5和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));

//在这里,我们将得到(0.0),因为我们正在传递参数

//(0.0),因此平方根相同

System.out.println("StrictMath.sqrt(d2): " + StrictMath.sqrt(d2));

//在这里,我们将获得(NaN),因为我们正在传递参数

//(-7.0 / 0.0),因此平方根是(-Infinity)

System.out.println("StrictMath.sqrt(d3): " + StrictMath.sqrt(d3));

//在这里,我们将得到(Infinity),因为我们正在传递

//参数(7.0 / 0.0),因此平方根是(Infinity)

System.out.println("StrictMath.sqrt(d4): " + StrictMath.sqrt(d4));

//在这里,我们将得到(给定参数的平方根),因为

//我们正在传递参数(1000.0)

System.out.println("StrictMath.sqrt(d5): " + StrictMath.sqrt(d5));

//在这里,我们将获得(NaN),因为我们正在传递参数

//(-1000.0)

System.out.println("StrictMath.sqrt(d6): " + StrictMath.sqrt(d6));

}

}

输出结果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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值