Guava BigIntegerMath類的ceilingPowerOfTwo(BigInteger x)方法返回大於或等於x的2的最小冪。這等效於BigInteger.valueOf(2).pow(log2(x,CEILING))。
用法:
public static BigInteger ceilingPowerOfTwo(BigInteger x)
參數:此方法將數字x作為其最大冪為2的參數。
返回值:此方法返回給定數字x的2的上限功率。
異常:如果x <= 0,則此方法引發IllegalArgumentException。
以下示例說明了BifIntegerMath.ceilingPowerOfTwo()方法:
範例1:
// Java code to show implementation of
// ceilingPowerOfTwo(BigInteger x) 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 n1 = BigInteger.valueOf(25);
// Using ceilingPowerOfTwo(BigInteger x) method of
// Guava's BigIntegerMath class
BigInteger ans = BigIntegerMath.ceilingPowerOfTwo(n1);
System.out.println("Smallest power of 2 greater "
+ "than or equal to "
+ n1 + " is:" + ans);
BigInteger n2 = BigInteger.valueOf(65);
// Using ceilingPowerOfTwo(BigInteger x) method of
// Guava's BigIntegerMath class
BigInteger ans1 = BigIntegerMath.ceilingPowerOfTwo(n2);
System.out.println("Smallest power of 2 greater "
+ "than or equal to "
+ n2 + " is:" + ans1);
}
}
輸出:
Smallest power of 2 greater than or equal to 25 is:32 Smallest power of 2 greater than or equal to 65 is:128
範例2:
// Java code to show implementation of
// ceilingPowerOfTwo(BigInteger x) 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 n = BigInteger.valueOf(0);
// Using ceilingPowerOfTwo(BigInteger x) method of
// Guava's BigIntegerMath class
// This should raise "IllegalArgumentException"
// as n is <= 0
BigInteger ans = BigIntegerMath.ceilingPowerOfTwo(n);
System.out.println("Smallest power of 2 greater "
+ "than or equal to n is:" + ans);
}
catch (Exception e) {
System.out.println("Exception:" + e);
}
}
}
輸出:
Exception:java.lang.IllegalArgumentException:x (0) must be > 0
相關用法
- Java Guava BigIntegerMath log10()用法及代碼示例
- Java Guava BigIntegerMath factorial()用法及代碼示例
- Java Guava BigIntegerMath isPowerOfTwo()用法及代碼示例
- Java Guava BigIntegerMath floorPowerOfTwo()用法及代碼示例
- Java Guava BigIntegerMath divide()用法及代碼示例
- Java Guava BigIntegerMath log2()用法及代碼示例
- Java Guava BigIntegerMath binomial()用法及代碼示例
- Java Guava BigIntegerMath sqrt()用法及代碼示例
- Java Guava IntMath ceilingPowerOfTwo()用法及代碼示例
- Java Guava Ints max()用法及代碼示例
- Java Guava Ints min()用法及代碼示例
- Java Guava Ints contains()用法及代碼示例
- Java Guava Sets union()用法及代碼示例
- Java Guava Ints concat()用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 BigIntegerMath ceilingPowerOfTwo() function | Guava | Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。