1. 程式人生 > >java中biginteger和bigdecimal在大數計算中的使用

java中biginteger和bigdecimal在大數計算中的使用

java中的BigInteger和BigIntegerDecimal
當我們在做Acm的大數題時,我們會發現int,double,表示的範圍有限,不能夠滿足要求,對於c/c++而言,我們就只能採用陣列模擬的方法來實現高精度大數的操作。然而java的jdk1.5後就可以使用math包中的BigInteger和BigDecimal來幫助我們解決高精度大數和小數的問題。

1 BigInteger高精度整數的使用。下面給出一些BigInteger的函式方法
ps:參考http://www.apihome.cn/api/java/BigInteger.html具體方法請開啟連結
 BigInteger abs()
          返回其值是此 BigInteger 的絕對值的 BigInteger。
 BigInteger add(BigInteger val)
          返回其值為 (this + val) 的 BigInteger。
 BigInteger and(BigInteger val)
          返回其值為 (this & val) 的 BigInteger。
 BigInteger andNot(BigInteger val)
          返回其值為 (this & ~val) 的 BigInteger。
 int bitCount()
          返回此 BigInteger 的二進位制補碼錶示形式中與符號不同的位的數量。
 int bitLength()
          返回此 BigInteger 的最小的二進位制補碼錶示形式的位數,不包括 符號位。
 BigInteger clearBit(int n)
          返回其值與清除了指定位的此 BigInteger 等效的 BigInteger。
 int compareTo(BigInteger val)
          將此 BigInteger 與指定的 BigInteger 進行比較。
 BigInteger divide(BigInteger val)
          返回其值為 (this / val) 的 BigInteger。
 BigInteger[] divideAndRemainder(BigInteger val)
          返回包含 (this / val) 後跟 (this % val) 的兩個 BigInteger 的陣列。
 double doubleValue()
          將此 BigInteger 轉換為 double
 boolean equals(Object x)
          比較此 BigInteger 與指定的 Object 的相等性。
 BigInteger flipBit(int n)
          返回其值與對此 BigInteger 進行指定位翻轉後的值等效的 BigInteger。
 float floatValue()
          將此 BigInteger 轉換為 float
 BigInteger gcd(BigInteger val)
          返回一個 BigInteger,其值是 abs(this)abs(val) 的最大公約數。
 int getLowestSetBit()
          返回此 BigInteger 最右端(最低位)1 位元的索引(即從此位元組的右端開始到本位元組中最右端 1 位元之間的 0 位元的位數)。
 int hashCode()
          返回此 BigInteger 的雜湊碼。
 int intValue()
          將此 BigInteger 轉換為 int
 boolean isProbablePrime(int certainty)
          如果此 BigInteger 可能為素數,則返回 true,如果它一定為合數,則返回 false
 long longValue()
          將此 BigInteger 轉換為 long
 BigInteger max(BigInteger val)
          返回此 BigInteger 和 val 的最大值。
 BigInteger min(BigInteger val)
          返回此 BigInteger 和 val 的最小值。
 BigInteger mod(BigInteger m)
          返回其值為 (this mod m) 的 BigInteger。
 BigInteger modInverse(BigInteger m)
          返回其值為 (this-1 mod m) 的 BigInteger。
 BigInteger modPow(BigInteger exponent, BigInteger m)
          返回其值為 (thisexponent mod m) 的 BigInteger。
 BigInteger multiply(BigInteger val)
          返回其值為 (this * val) 的 BigInteger。
 BigInteger negate()
          返回其值是 (-this) 的 BigInteger。
 BigInteger nextProbablePrime()
          返回大於此 BigInteger 的可能為素數的第一個整數。
 BigInteger not()
          返回其值為 (~this) 的 BigInteger。
 BigInteger or(BigInteger val)
          返回其值為 (this | val) 的 BigInteger。
 BigInteger pow(int exponent)
          返回其值為 (thisexponent) 的 BigInteger。
static BigInteger probablePrime(int bitLength, Random rnd)
          返回有可能是素數的、具有指定長度的正 BigInteger。
 BigInteger remainder(BigInteger val)
          返回其值為 (this % val) 的 BigInteger。
 BigInteger setBit(int n)
          返回其值與設定了指定位的此 BigInteger 等效的 BigInteger。
 BigInteger shiftLeft(int n)
          返回其值為 (this << n) 的 BigInteger。
 BigInteger shiftRight(int n)
          返回其值為 (this >> n) 的 BigInteger。
 int signum()
          返回此 BigInteger 的正負號函式。
 BigInteger subtract(BigInteger val)
          返回其值為 (this - val) 的 BigInteger。
 boolean testBit(int n)
          當且僅當設定了指定的位時,返回 true
 byte[] toByteArray()
          返回一個 byte 陣列,該陣列包含此 BigInteger 的二進位制補碼錶示形式。
 String toString()
          返回此 BigInteger 的十進位制字串表示形式。
 String toString(int radix)
          返回此 BigInteger 的給定基數的字串表示形式。
static BigInteger valueOf(long val)
          返回其值等於指定 long 的值的 BigInteger。
 BigInteger xor(BigInteger val)
          返回其值為 (this ^ val) 的 BigInteger。
我們可以利用BigInteger中的方法實現賦值,加減乘除,取模等各種運算,java中並沒有過載運算子,所以不可以使用運算操作符,要使用函式。BigInteger還提供了三個常量
BigInteger.one  大數1
BigInteger.zero 大數0
BigInteger.ten 大數10
下面是是實現BigInteger基本功能的程式碼
import java.util.*;
import java.math.*;
public class jichu {
	public static void main(String[] args) {
		Scanner cin=new Scanner(System.in);
		BigInteger a,b,c;
		while(cin.hasNextBigInteger())
		{
		a=cin.nextBigInteger();
		b=cin.nextBigInteger();
		c=a.add(b);//計算a+b
		System.out.println(c);
		c=a.multiply(b);//計算a*b
		System.out.println(c);
		c=a.subtract(b);//計算a-b
		System.out.println(c);
		c=a.divide(b);//計算a/b
		System.out.println(c);
		c=a.mod(b);//計算a%b
		System.out.println(c);
		}
cin.close();
	}

}
就是對java中類的方法的使用,細心認真就可以
2BigDecimal的使用
BigDecimal的一些基本方法,具體參考http://www.apihome.cn/api/java/BigDecimal.html

BigDecimal abs()
          返回 BigDecimal,其值為此 BigDecimal 的絕對值,其標度為 this.scale()
 BigDecimal abs(MathContext mc)
          返回其值為此 BigDecimal 絕對值的 BigDecimal(根據上下文設定進行舍入)。
 BigDecimal add(BigDecimal augend)
          返回一個 BigDecimal,其值為 (this + augend),其標度為 max(this.scale(), augend.scale())
 BigDecimal add(BigDecimal augend, MathContext mc)
          返回其值為 (this + augend)BigDecimal(根據上下文設定進行舍入)。
 byte byteValueExact()
          將此 BigDecimal 轉換為 byte,以檢查丟失的資訊。
 int compareTo(BigDecimal val)
          將此 BigDecimal 與指定的 BigDecimal 比較。
 BigDecimal divide(BigDecimal divisor)
          返回一個 BigDecimal,其值為 (this / divisor),其首選標度為 (this.scale() - divisor.scale());如果無法表示準確的商值(因為它有無窮的十進位制擴充套件),則丟擲ArithmeticException
 BigDecimal divide(BigDecimal divisor, int roundingMode)
          返回一個 BigDecimal,其值為 (this / divisor),其標度為 this.scale()
 BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)
          返回一個 BigDecimal,其值為 (this / divisor),其標度為指定標度。
 BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
          返回一個 BigDecimal,其值為 (this / divisor),其標度為指定標度。
 BigDecimal divide(BigDecimal divisor, MathContext mc)
          返回其值為 (this / divisor)BigDecimal(根據上下文設定進行舍入)。
 BigDecimal divide(BigDecimal divisor, RoundingMode roundingMode)
          返回一個 BigDecimal,其值為 (this / divisor),其標度為 this.scale()
 BigDecimal[] divideAndRemainder(BigDecimal divisor)
          返回由兩個元素組成的 BigDecimal 陣列,該陣列包含 divideToIntegralValue 的結果,後跟對兩個運算元計算所得到的remainder
 BigDecimal[] divideAndRemainder(BigDecimal divisor, MathContext mc)
          返回由兩個元素組成的 BigDecimal 陣列,該陣列包含 divideToIntegralValue 的結果,後跟根據上下文設定對兩個運算元進行舍入計算所得到的remainder 的結果。
 BigDecimal divideToIntegralValue(BigDecimal divisor)
          返回 BigDecimal,其值為向下舍入所得商值 (this / divisor) 的整數部分。
 BigDecimal divideToIntegralValue(BigDecimal divisor, MathContext mc)
          返回 BigDecimal,其值為 (this / divisor) 的整數部分。
 double doubleValue()
          將此 BigDecimal 轉換為 double
 boolean equals(Object x)
          比較此 BigDecimal 與指定的 Object 的相等性。
 float floatValue()
          將此 BigDecimal 轉換為 float
 int hashCode()
          返回此 BigDecimal 的雜湊碼。
 int intValue()
          將此 BigDecimal 轉換為 int
 int intValueExact()
          將此 BigDecimal 轉換為 int,以檢查丟失的資訊。
 long longValue()
          將此 BigDecimal 轉換為 long
 long longValueExact()
          將此 BigDecimal 轉換為 long,以檢查丟失的資訊。
 BigDecimal max(BigDecimal val)
          返回此 BigDecimalval 的最大值。
 BigDecimal min(BigDecimal val)
          返回此 BigDecimalval 的最小值。
 BigDecimal movePointLeft(int n)
          返回一個 BigDecimal,它等效於將該值的小數點向左移動 n 位。
 BigDecimal movePointRight(int n)
          返回一個 BigDecimal,它等效於將該值的小數點向右移動 n 位。
 BigDecimal multiply(BigDecimal multiplicand)
          返回一個 BigDecimal,其值為 (this × multiplicand),其標度為 (this.scale() + multiplicand.scale())
 BigDecimal multiply(BigDecimal multiplicand, MathContext mc)
          返回其值為 (this × multiplicand)BigDecimal(根據上下文設定進行舍入)。
 BigDecimal negate()
          返回 BigDecimal,其值為 (-this),其標度為 this.scale()
 BigDecimal negate(MathContext mc)
          返回其值為 (-this)BigDecimal(根據上下文設定進行舍入)。
 BigDecimal plus()
          返回 BigDecimal,其值為 (+this),其標度為 this.scale()
 BigDecimal plus(MathContext mc)
          返回其值為 (+this)BigDecimal(根據上下文設定進行舍入)。
 BigDecimal pow(int n)
          返回其值為 (thisn)BigDecimal,準確計算該冪,使其具有無限精度。
 BigDecimal pow(int n, MathContext mc)
          返回其值為 (thisn)BigDecimal
 int precision()
          返回此 BigDecimal精度
 BigDecimal remainder(BigDecimal divisor)
          返回其值為 (this % divisor)BigDecimal
 BigDecimal remainder(BigDecimal divisor, MathContext mc)
          返回其值為 (this % divisor)BigDecimal(根據上下文設定進行舍入)。
 BigDecimal round(MathContext mc)
          返回根據 MathContext 設定進行舍入後的 BigDecimal
 int scale()
          返回此 BigDecimal標度
 BigDecimal scaleByPowerOfTen(int n)
          返回其數值等於 (this * 10n) 的 BigDecimal。
 BigDecimal setScale(int newScale)
          返回一個 BigDecimal,其標度為指定值,其值在數值上等於此 BigDecimal 的值。
 BigDecimal setScale(int newScale, int roundingMode)
          返回一個 BigDecimal,其標度為指定值,其非標度值通過此 BigDecimal 的非標度值乘以或除以十的適當次冪來確定,以維護其總值。
 BigDecimal setScale(int newScale, RoundingMode roundingMode)
          返回 BigDecimal,其標度為指定值,其非標度值通過此 BigDecimal 的非標度值乘以或除以十的適當次冪來確定,以維護其總值。
 short shortValueExact()
          將此 BigDecimal 轉換為 short,以檢查丟失的資訊。
 int signum()
          返回此 BigDecimal 的正負號函式。
 BigDecimal stripTrailingZeros()
          返回數值上等於此小數,但從該表示形式移除所有尾部零的 BigDecimal
 BigDecimal subtract(BigDecimal subtrahend)
          返回一個 BigDecimal,其值為 (this - subtrahend),其標度為 max(this.scale(), subtrahend.scale())
 BigDecimal subtract(BigDecimal subtrahend, MathContext mc)
          返回其值為 (this - subtrahend)BigDecimal(根據上下文設定進行舍入)。
 BigInteger toBigInteger()
          將此 BigDecimal 轉換為 BigInteger
 BigInteger toBigIntegerExact()
          將此 BigDecimal 轉換為 BigInteger,以檢查丟失的資訊。
 String toEngineeringString()
          返回此 BigDecimal 的字串表示形式,需要指數時,則使用工程計數法。
 String toPlainString()
          返回不帶指數字段的此 BigDecimal 的字串表示形式。
 String toString()
          返回此 BigDecimal 的字串表示形式,如果需要指數,則使用科學記數法。
 BigDecimal ulp()
          返回此 BigDecimal 的 ulp(最後一位的單位)的大小。
 BigInteger unscaledValue()
          返回其值為此 BigDecimal非標度值BigInteger
static BigDecimal valueOf(double val)
          使用 Double.toString(double) 方法提供的 double 規範的字串表示形式將 double 轉換為BigDecimal
static BigDecimal valueOf(long val)
          將 long 值轉換為具有零標度的 BigDecimal
static BigDecimal valueOf(long unscaledVal, int scale)
          將 long 非標度值和 int 標度轉換為 BigDecimal
  BIgDecimal和BigInteger的基本使用方法差不多,要注意消除尾0,和轉化為非指數型字串輸出
基本使用程式碼如下
import java.util.*;
import java.math.*;
public class jichu {
	public static void main(String[] args) {
		Scanner cin=new Scanner(System.in);
		BigDecimal a,b,c;
		while(cin.hasNextBigDecimal())
		{
		a=cin.nextBigDecimal();
		b=cin.nextBigDecimal();
		c=a.add(b);//計算a+b//0.000001  0.000009
		System.out.println(c);//0.000010
		System.out.println(c.stripTrailingZeros());//消尾0輸出//結果丟擲異常0.00001//可以的使用MathContext()控制輸出精度
		System.out.println(c.stripTrailingZeros().toPlainString());//消尾0轉化為非指數型字串輸出,等於沒舍入的完整精度  //0.00001
		c=a.multiply(b);//計算a*b
		System.out.println(c);
		c=a.subtract(b);//計算a-b
		System.out.println(c);
		c=a.divide(b, new MathContext(3));//計算a/控制保留三位有效數字
		System.out.println(c);
		
		}
cin.close();
	}

}

以上就是java大數的基本用法,遇到具體問題再進行總結