📜  BigIntegerMath sqrt()函数|番石榴 |Java

📅  最后修改于: 2022-05-13 01:55:07.926000             🧑  作者: Mango

BigIntegerMath sqrt()函数|番石榴 |Java

Guava 的 BigIntegerMath 类的方法sqrt(BigInteger x, RoundingMode mode)返回 x 的平方根,用指定的舍入模式四舍五入。

句法:

public static BigInteger sqrt(BigInteger x, RoundingMode mode)

参数:此方法采用以下参数:

  • x :要找到其平方根的 BigInteger 数。
  • mode :计算平方根的舍入模式。

返回值:此方法返回 x 的平方根,以指定的舍入方式进行舍入。

异常:此方法引发以下异常:

  • IllegalArgumentException:如果 x < 0。
  • ArithmeticException:如果模式是 RoundingMode.UNNECESSARY 并且 sqrt(x) 不是整数。

枚举舍入模式

Enum ConstantDescription
CEILINGRounding mode to round towards positive infinity.
DOWNRounding mode to round towards zero.
FLOORRounding mode to round towards negative infinity.
HALF_DOWNRounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round down.
HALF_EVENRounding mode to round towards the “nearest neighbor” unless both neighbors are equidistant, in which case, round towards the even neighbor.
HALF_UPRounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round up.
UNNECESSARYRounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.
UPRounding mode to round away from zero.

下面的示例说明了 BigIntegerMath.sqrt() 方法:

示例 1:

// Java code to show implementation of
// sqrt(BigInteger x, RoundingMode mode) method
// of Guava's BigIntegerMath class
  
import java.math.*;
import com.google.common.math.BigIntegerMath;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
        BigInteger x1 = BigInteger.valueOf(226);
  
        // Using sqrt(BigInteger x, RoundingMode mode)
        // method of Guava's BigIntegerMath class
        // The RoundingMode HALF_EVEN rounds towards
        // the nearest neighbor unless both neighbors
        // are equidistant, in which case, round towards
        // the even neighbor.
        BigInteger ans1 = BigIntegerMath
                              .sqrt(x1,
                                    RoundingMode.HALF_EVEN);
  
        System.out.println("Square root of " + x1
                           + "with HALF_EVEN rounding mode is: "
                           + ans1);
  
        BigInteger x2 = BigInteger.valueOf(154);
  
        // Using sqrt(BigInteger x, RoundingMode mode)
        // method of Guava's BigIntegerMath class
        // The RoundingMode FLOOR rounds towards
        // negative infinity.
        BigInteger ans2 = BigIntegerMath
                              .sqrt(x2,
                                    RoundingMode.FLOOR);
  
        System.out.println("Square root of " + x2
                           + "with FLOOR rounding mode is: "
                           + ans2);
    }
}
输出:
Square root of 226with HALF_EVEN rounding mode is: 15
Square root of 154with FLOOR rounding mode is: 12

示例 2:

// Java code to show implementation of
// sqrt(BigInteger x, RoundingMode mode) method
// of Guava's BigIntegerMath class
  
import java.math.*;
import com.google.common.math.BigIntegerMath;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
  
        try {
            BigInteger x1 = BigInteger.valueOf(-65);
  
            // Using sqrt(BigInteger x, RoundingMode mode)
            // method of Guava's BigIntegerMath class
            // The RoundingMode HALF_EVEN rounds towards
            // the nearest neighbor unless both neighbors
            // are equidistant, in which case, round towards
            // the even neighbor.
            BigInteger ans1 = BigIntegerMath
                                  .sqrt(x1,
                                        RoundingMode.HALF_EVEN);
  
            // This should throw "IllegalArgumentException"
            // as x1 < 0
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}
输出:
Exception: java.lang.IllegalArgumentException: x (-65) must be >= 0

参考: https://google.github.io/guava/releases/21.0/api/docs/com/google/common/math/BigIntegerMath.html#sqrt-java.math.BigInteger-java.math.RoundingMode-