예제가 포함된 Java sqrt() 메서드

java.lang.Math.sqrt()는 인수로 전달된 double 유형 값의 제곱근을 반환합니다. 인수가 NaN이거나 음수이면 결과는 NaN입니다. 인수가 양의 무한대이면 결과는 양의 무한대입니다. 전달된 인수가 양수 0 또는 음수 0이면 결과는 인수의 결과와 동일합니다.

통사론 :

 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 : 작업하는 모습을 보여주기 위해 java.lang.Math.sqrt() 인수가 NaN 또는 +무한대인 경우의 메서드입니다.




// 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