您好,欢迎来到化拓教育网。
搜索
您的当前位置:首页在java中sqrt的用法_Java sqrt()用法及代码示例

在java中sqrt的用法_Java sqrt()用法及代码示例

来源:化拓教育网

java.lang.Math.sqrt()返回作为参数传递给它的double类型值的平方根。如果参数为NaN或负数,则结果为NaN。如果参数为正无穷大,则结果为正无穷大。如果传递的参数为正零或负零,则结果将与参数相同。

用法:

public static double sqrt(double a)

Parameter:

a:the value whose square root is to be returned.

Return:

This method returns the positive square root value of

the argument passed to it.

例子1:显示java.lang.Math.sqrt()方法的用法。

// Java program to demonstrate working

// of java.lang.Math.sqrt() method

import java.lang.Math;

class Gfg {

// driver code

public static void main(String args[])

{

double a = 30;

System.out.println(Math.sqrt(a));

a = 45;

System.out.println(Math.sqrt(a));

a = 60;

System.out.println(Math.sqrt(a));

a = 90;

System.out.println(Math.sqrt(a));

}

}

输出:

5.477225575051661

6.708203932499369

7.745966692414834

9.486832980505138

例子2:当参数为NaN或+ infinity时,显示java.lang.Math.sqrt()方法的用法。

// Java program to demonstrate working

// of java.lang.Math.sqrt() method

import java.lang.Math; // importing java.lang package

public class GFG {

public static void main(String[] args)

{

double positiveInfinity = Double.POSITIVE_INFINITY;

double negativeVal = -5;

double nan = Double.NaN;

double result;

// Here argument is negative,

// output will be NaN

result = Math.sqrt(negativeVal);

System.out.println(result);

// Here argument is positive infinity,

// output will also positive infinity

result = Math.sqrt(positiveInfinity);

System.out.println(result);

// Here argument is NaN, output will be NaN

result = Math.sqrt(nan);

System.out.println(result);

}

}

输出:

NaN

Infinity

NaN

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务