Guava BigIntegerMath類的方法sqrt(BigInteger x,RoundingMode模式)返回x的平方根,並以指定的舍入模式進行四舍五入。
用法:
public static BigInteger sqrt(BigInteger x, RoundingMode mode)
參數:此方法采用以下參數:
- x:要找到其平方根的BigInteger編號。
- mode:用於計算平方根的舍入模式。
返回值:此方法返回x的平方根,並使用指定的舍入模式進行舍入。
異常:此方法引發以下異常:
- IllegalArgumentException:如果x <0。
- ArithmeticException:如果mode為RoundingMode.UNNECESSARY且sqrt(x)不是整數。
Enum RoundingMode
枚舉常量 | 描述 |
---|---|
CEILING | 舍入模式向正無窮大舍入。 |
DOWN | 舍入模式向零舍入。 |
FLOOR | 舍入模式向負無窮大舍入。 |
HALF_DOWN | 除非兩個鄰居等距,否則舍入模式將朝“nearest neighbor”舍入。 |
HALF_EVEN | 舍入模式向“nearest neighbor”舍入,除非兩個鄰居都等距,在這種情況下,向偶數鄰居舍入。 |
HALF_UP | 除非兩個鄰居等距,否則舍入模式將朝“nearest neighbor”舍入。 |
UNNECESSARY | 舍入模式可以斷言所請求的操作具有準確的結果,因此不需要舍入。 |
UP | 舍入模式從零舍入。 |
以下示例說明了BifIntegerMath.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
相關用法
- Java Guava BigIntegerMath log2()用法及代碼示例
- Java Guava BigIntegerMath divide()用法及代碼示例
- Java Guava BigIntegerMath log10()用法及代碼示例
- Java Guava BigIntegerMath ceilingPowerOfTwo()用法及代碼示例
- Java Guava BigIntegerMath binomial()用法及代碼示例
- Java Guava BigIntegerMath floorPowerOfTwo()用法及代碼示例
- Java Guava BigIntegerMath isPowerOfTwo()用法及代碼示例
- Java Guava BigIntegerMath factorial()用法及代碼示例
- Java Guava IntMath sqrt(int x, RoundingMode mode)用法及代碼示例
- Java Guava LongMath sqrt(long x, RoundingMode mode)用法及代碼示例
- Java Guava Ints min()用法及代碼示例
- Java Guava Ints max()用法及代碼示例
- Java Guava Ints contains()用法及代碼示例
- Java Guava Sets powerSet()用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 BigIntegerMath sqrt() function | Guava | Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。