BigIntegerMath提供BigInteger的实用方法。
以下是com.google.common.math.BigIntegerMath类的声明:
@GwtCompatible(emulated=true) public final class BigIntegerMath extends Object
| S.N. | 方法及说明 |
|---|---|
| 1 |
static BigInteger binomial(int n, int k) 返回n选择k,也被称为n和k的二项式系数,即n! / (k! (n - k)!)。 |
| 2 |
static BigInteger divide(BigInteger p, BigInteger q, RoundingMode mode) 返回除以p由q,使用指定的RoundingMode四舍五入的结果。 |
| 3 |
static BigInteger factorial(int n) 返回n个!,即,在第一n个正整数的乘积,或1如果n== 0。 |
| 4 |
static boolean isPowerOfTwo(BigInteger x) 返回true,如果x代表两个幂。 |
| 5 |
static int log10(BigInteger x, RoundingMode mode) 返回基数为10的对数x,根据指定的舍入模式范围。 |
| 6 |
static int log2(BigInteger x, RoundingMode mode) 返回基数为2-对数x,根据指定的舍入模式圆形。 |
| 7 |
static BigInteger sqrt(BigInteger x, RoundingMode mode) 返回x的平方根,大概指定的舍入模式。 |
这个类继承了以下类方法:
java.lang.Object
使用所选择的任何编辑器创建下面的java程序 C:/> Guava
GuavaTester.javaimport java.math.BigInteger;
import java.math.RoundingMode;
import com.google.common.math.BigIntegerMath;
public class GuavaTester {
public static void main(String args[]){
GuavaTester tester = new GuavaTester();
tester.testBigIntegerMath();
}
private void testBigIntegerMath(){
System.out.println(BigIntegerMath.divide(BigInteger.TEN, new BigInteger("2"), RoundingMode.UNNECESSARY));
try{
//exception will be thrown as 100 is not completely divisible by 3 thus rounding
// is required, and RoundingMode is set as UNNESSARY
System.out.println(BigIntegerMath.divide(BigInteger.TEN, new BigInteger("3"), RoundingMode.UNNECESSARY));
}catch(ArithmeticException e){
System.out.println("Error: " + e.getMessage());
}
System.out.println("Log2(2): "+BigIntegerMath.log2(new BigInteger("2"), RoundingMode.HALF_EVEN));
System.out.println("Log10(10): "+BigIntegerMath.log10(BigInteger.TEN, RoundingMode.HALF_EVEN));
System.out.println("sqrt(100): "+BigIntegerMath.sqrt(BigInteger.TEN.multiply(BigInteger.TEN), RoundingMode.HALF_EVEN));
System.out.println("factorial(5): "+BigIntegerMath.factorial(5));
}
}
使用javac编译器编译如下类
C:\Guava>javac GuavaTester.java
现在运行GuavaTester看到的结果
C:\Guava>java GuavaTester
看到结果。
5 Error: Rounding necessary Log2(2): 1 Log10(10): 1 sqrt(100): 10 factorial(5): 120