BigIntegerMath提供BigInteger的實(shí)用方法。
以下是com.google.common.math.BigIntegerMath類的聲明:
@GwtCompatible(emulated=true)
public final class BigIntegerMath
? ?extends Object
方法

繼承的方法
這個(gè)類繼承了以下類方法:java.lang.Object
使用所選擇的任何編輯器創(chuàng)建下面的java程序 C:/> Guava
GuavaTester.java
import 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));
}
}
驗(yàn)證結(jié)果
使用javac編譯器編譯如下類
C:\Guava>javac GuavaTester.java
現(xiàn)在運(yùn)行GuavaTester看到的結(jié)果
C:\Guava>java GuavaTester
看到結(jié)果
5
Error: Rounding necessary
Log2(2): 1
Log10(10): 1
sqrt(100): 10
factorial(5): 120
轉(zhuǎn)載自并發(fā)編程網(wǎng)-ifeve.com