CheckedPow(long b,long k)是Guava LongMath類的一種方法,該方法接受兩個參數b和k,並用於查找b的k-th冪。
用法:
public static long checkedPow(long b, long k)
參數:該方法接受兩個參數b和k。參數b稱為基數,它升為k-th的冪。
返回值:此方法返回b的k-th冪。
異常:如果b的k-th的冪在有符號長算法中溢出,則checkedPow(long b,long k)方法將引發ArithmeticException。
以下示例說明了上述方法的實現:
範例1:
// Java code to show implementation of
// checkedPow(long b, long k) method of
// Guava's LongMath class
import java.math.RoundingMode;
import com.google.common.math.LongMath;
class GFG {
// Driver code
public static void main(String args[])
{
long b1 = 5;
int k1 = 7;
// Using checkedPow(long b, long k) method
// of Guava's LongMath class
long ans1 = LongMath.checkedPow(b1, k1);
System.out.println(b1 + " to the "
+ k1 + "th power is:"
+ ans1);
long b2 = 19;
int k2 = 4;
// Using checkedPow(long b, long k) method
// of Guava's LongMath class
long ans2 = LongMath.checkedPow(b2, k2);
System.out.println(b2 + " to the " + k2
+ "th power is:"
+ ans2);
}
}
輸出:
5 to the 7th power is:78125 19 to the 4th power is:130321
範例2:
// Java code to show implementation of
// checkedPow(long b, long k) method of
// Guava's LongMath class
import java.math.RoundingMode;
import com.google.common.math.LongMath;
class GFG {
static long findPow(long b, int k)
{
try {
// Using checkedPow(long b, long k) method of
// Guava's LongMath class
// This should raise "ArithmeticException" as
// b to the kth power overflows in
// signed long arithmetic
long ans = LongMath.checkedPow(b, k);
// Return the answer
return ans;
}
catch (Exception e) {
System.out.println(e);
return -1;
}
}
// Driver code
public static void main(String args[])
{
long b = 20;
int k = 25;
try {
// Function calling
findPow(b, k);
}
catch (Exception e) {
System.out.println(e);
}
}
}
輸出:
java.lang.ArithmeticException:overflow
相關用法
- Java Guava Floats.max()用法及代碼示例
- Java Guava Booleans.contains()用法及代碼示例
- Java Guava Chars.max()用法及代碼示例
- Java Guava Chars.contains()用法及代碼示例
- Java Guava Doubles.min()用法及代碼示例
- Java Guava Doubles.contains()用法及代碼示例
- Java Guava Shorts.min()用法及代碼示例
- Java Guava Bytes.contains()用法及代碼示例
- Java Guava Longs.contains()用法及代碼示例
- Java Guava Floats.contains()用法及代碼示例
- Java Guava Longs.min()用法及代碼示例
- Java Guava Chars.min()用法及代碼示例
- Java Guava Shorts.max()用法及代碼示例
- Java Guava Doubles.max()用法及代碼示例
- Java Guava Longs.max()用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 Java Guava | LongMath.checkedPow(long b, int k) method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。