JAVA数学大整数。sqrt() 是一个内置函数 在JavaSE9和JDK9中添加 返回应用sqrt()方法的BigInteger的平方根的BigInteger值。它与楼层(sqrt(n))相同,其中n是一个数字。如果实平方根不能表示为整数值,则该平方根小于实平方根。
null
语法:
public BigInteger sqrt()
参数: 该方法不采用任何参数。 返回值: 方法返回此BigInteger的整数平方根。 例外情况: 如果BigInteger为负,该方法将抛出算术异常。
例子:
Input: 234876543456Output: 484640Explanation: 122 is given as input which is the bigInteger.The square root of 122 is 11.04536whose BigInteger equivalent is 11 and using sqrt()method of BigInteger class we can getSquare root of any BigInteger.Input: 122Output: 11
下面的程序演示了BigInteger类的sqrt()方法:
项目1: 展示了sqrt()方法在31739平方根计算中的应用。
JAVA
// Please run this program in JDK 9 or JDK 10 // Java program to demonstrate sqrt() method import java.math.*; public class GFG { public static void main(String[] args) { // Creating a BigInteger object BigInteger big, squareRoot; big = new BigInteger( "31739" ); // calculate square root o bigInteger // using sqrt() method squareRoot = big.sqrt(); // print result System.out.println( "Square root value of BigInteger " + big + " is " + squareRoot); } } |
输出:
Square root value of BigInteger 31739 is 178
项目2: 显示异常是由sqrt()方法引发的。
JAVA
//Please run this program in JDK 9 or JDK 10 // Java program to demonstrate sqrt() method //and exception thrown by it import java.math.*; public class GFG { public static void main(String[] args) { // Creating a BigInteger object BigInteger big,squareRoot= null ; big = new BigInteger( "-2345" ); //calculate square root o bigInteger //using sqrt() method try { squareRoot = big.sqrt(); } catch (Exception e) { e.printStackTrace(); } // print result System.out.println( "Square root value of BigInteger " + big + " is " +squareRoot); } } |
输出:
java.lang.ArithmeticException: Negative BigInteger at java.base/java.math.BigInteger.sqrt(Unknown Source) at GFG.main(GFG.java:19)Square root value of BigInteger -2345 is null
参考: https://docs.oracle.com/javase/9/docs/api/java/math/BigInteger.html#sqrt–
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END