java.math.BigInteger.pow(int exponent)方法用于计算BigInteger的加幂,以其他形式传递给指数等于(this)的数字的幂exponent。此方法对当前BigInteger执行操作,通过该操作调用此方法并将指数作为参数传递。
用法:
public BigInteger pow(int exponent)
参数:此方法接受参数指数,该指数是应将BigInteger提升到的指数。返回:此方法返回一个BigInteger,它等于(this)exponent。
Exception:
参数指数必须为正数(指数> = 0),否则为ArithmeticException被抛出。
Examples:
Input: BigInteger1=321456, exponenet=5 Output: 3432477361331488865859403776 Explanation: BigInteger1.pow(exponent)=3432477361331488865859403776. 321456^5=3432477361331488865859403776 Input: BigInteger1=45321, exponenet=3 Output: 93089018611161 Explanation: BigInteger1.pow(exponent)=93089018611161. 321456^5=93089018611161
下面的程序说明BigInteger类的pow()方法
范例1:
// Java program to demonstrate
// pow() method of BigInteger
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
// Creating BigInteger object
BigInteger b1;
b1 = new BigInteger("321456");
int exponent = 5;
// apply pow() method
BigInteger result = b1.pow(exponent);
// print result
System.out.println("Result of pow operation between BigInteger "
+ b1 + " and exponent " + exponent + " equal to " + result);
}
}
输出:
Result of pow operation between BigInteger 321456 and exponent 5 equal
to 3432477361331488865859403776
范例2:
// Java program to demonstrate
// pow() method of BigInteger
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
// Creating BigInteger object
BigInteger b1;
b1 = new BigInteger("12346");
int exponent = 6;
// apply pow() method
BigInteger result = b1.pow(exponent);
// print result
System.out.println("Result of pow operation between BigInteger "
+ b1 + " and exponent " + exponent + " equal to " + result);
}
}
输出:
Result of pow operation between BigInteger 41432345678 and exponent 6 equal to 5058679076487529899393537031261743031889730764186441745527485504
范例3:
程序在作为参数传递的指数小于零时显示异常。
// Java program to demonstrate
// pow() method of BigInteger
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
// Creating BigInteger object
BigInteger b1;
b1 = new BigInteger("76543");
int exponent = -17;
try {
// apply pow() method
BigInteger result = b1.pow(exponent);
// print result
System.out.println("Result of pow operation between " + b1
+ " and " + exponent + " equal to " + result);
}
catch (Exception e) {
System.out.println(e);
}
}
}
输出:
java.lang.ArithmeticException: Negative exponent
参考:https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigInteger.html#pow(int)
相关用法
- Java BigInteger or()用法及代码示例
- Java BigInteger and()用法及代码示例
- Java BigInteger not()用法及代码示例
- Java BigInteger xor()用法及代码示例
- Java BigInteger abs()用法及代码示例
- Java BigInteger mod()用法及代码示例
- Java BigInteger valueOf()用法及代码示例
- Java BigInteger toByteArray()用法及代码示例
- Java BigInteger longValue()用法及代码示例
- Java BigInteger intValue()用法及代码示例
- Java BigInteger floatValue()用法及代码示例
- Java BigInteger doubleValue()用法及代码示例
- Java BigInteger remainder()用法及代码示例
- Java BigInteger toString()用法及代码示例
- Java BigInteger add()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 BigInteger pow() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。