# 12-數學運算
# 12-數學運算
[原文鏈接](http://code.google.com/p/guava-libraries/wiki/MathExplained) [譯文鏈接](http://ifeve.com/google-guava-math) **譯者:**沈義揚
## 范例
```
<pre class="calibre11">```
int logFloor = LongMath.log2(n, FLOOR);
int mustNotOverflow = IntMath.checkedMultiply(x, y);
long quotient = LongMath.divide(knownMultipleOfThree, 3, RoundingMode.UNNECESSARY); // fail fast on non-multiple of 3
BigInteger nearestInteger = DoubleMath.roundToBigInteger(d, RoundingMode.HALF_EVEN);
BigInteger sideLength = BigIntegerMath.sqrt(area, CEILING);
```
```
## 為什么使用Guava Math
- Guava Math針對各種不常見的溢出情況都有充分的測試;對溢出語義,Guava文檔也有相應的說明;如果運算的溢出檢查不能通過,將導致快速失敗;
- Guava Math的性能經過了精心的設計和調優;雖然性能不可避免地依據具體硬件細節而有所差異,但Guava Math的速度通常可以與Apache Commons的MathUtils相比,在某些場景下甚至還有顯著提升;
- Guava Math在設計上考慮了可讀性和正確的編程習慣;IntMath.log2(x, CEILING) 所表達的含義,即使在快速閱讀時也是清晰明確的。而32-Integer.numberOfLeadingZeros(x – 1)對于閱讀者來說則不夠清晰。
*注意:Guava Math和GWT格外不兼容,這是因為Java和Java Script語言的運算溢出邏輯不一樣。*
## 整數運算
Guava Math主要處理三種整數類型:int、long和BigInteger。這三種類型的運算工具類分別叫做[IntMath](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html)、[LongMath](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html)和[BigIntegerMath](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/BigIntegerMath.html)。
### 有溢出檢查的運算
Guava Math提供了若干有溢出檢查的運算方法:結果溢出時,這些方法將快速失敗而不是忽略溢出
[`IntMath.checkedAdd`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html#checkedAdd%28int,%20int%29)[`LongMath.checkedAdd`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#checkedAdd%28long,%20long%29)[`IntMath.checkedSubtract`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html#checkedSubtract%28int,%20int%29)[`LongMath.checkedSubtract`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#checkedSubtract%28long,%20long%29)[`IntMath.checkedMultiply`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html#checkedMultiply%28int,%20int%29)[`LongMath.checkedMultiply`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#checkedMultiply%28long,%20long%29)[`IntMath.checkedPow`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html#checkedPow%28int,%20int%29)[`LongMath.checkedPow`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#checkedPow%28long,%20long%29)```
<pre class="calibre11">```
IntMath.checkedAdd(Integer.MAX_VALUE, Integer.MAX_VALUE); // throws ArithmeticException
```
```
## 實數運算
IntMath、LongMath和BigIntegerMath提供了很多實數運算的方法,并把最終運算結果舍入成整數。這些方法接受一個[java.math.RoundingMode](http://docs.oracle.com/javase/7/docs/api/java/math/RoundingMode.html)枚舉值作為舍入的模式:
- DOWN:向零方向舍入(去尾法)
- UP:遠離零方向舍入
- FLOOR:向負無限大方向舍入
- CEILING:向正無限大方向舍入
- UNNECESSARY:不需要舍入,如果用此模式進行舍入,應直接拋出ArithmeticException
- HALF\_UP:向最近的整數舍入,其中x.5遠離零方向舍入
- HALF\_DOWN:向最近的整數舍入,其中x.5向零方向舍入
- HALF\_EVEN:向最近的整數舍入,其中x.5向相鄰的偶數舍入
這些方法旨在提高代碼的可讀性,例如,divide(x, 3, CEILING) 即使在快速閱讀時也是清晰。此外,這些方法內部采用構建整數近似值再計算的實現,除了在構建sqrt(平方根)運算的初始近似值時有浮點運算,其他方法的運算全過程都是整數或位運算,因此性能上更好。
**運算****IntMath****LongMath****BigIntegerMath**除法[`divide(int, int, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html#divide%28int,%20int,%20java.math.RoundingMode%29)[`divide(long, long, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#divide%28long,%20long,%20java.math.RoundingMode%29)[`divide(BigInteger, BigInteger, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/BigIntegerMath.html#divide%28java.math.BigInteger,%20java.math.BigInteger,%20java.math.RoundingMode%29)2為底的對數[`log2(int, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html#log2%28int,%20java.math.RoundingMode%29)[`log2(long, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#log2%28long,%20java.math.RoundingMode%29)[`log2(BigInteger, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/BigIntegerMath.html#log2%28java.math.BigInteger,%20java.math.RoundingMode%29)10為底的對數[`log10(int, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html#log10%28int,%20java.math.RoundingMode%29)[`log10(long, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#log10%28long,%20java.math.RoundingMode%29)[`log10(BigInteger, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/BigIntegerMath.html#log10%28java.math.BigInteger,%20java.math.RoundingMode%29)平方根[`sqrt(int, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html#sqrt%28int,%20java.math.RoundingMode%29)[`sqrt(long, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#sqrt%28long,%20java.math.RoundingMode%29)[`sqrt(BigInteger, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/BigIntegerMath.html#sqrt%28java.math.BigInteger,%20java.math.RoundingMode%29)```
<pre class="calibre11">```
// returns 31622776601683793319988935444327185337195551393252
BigIntegerMath.sqrt(BigInteger.TEN.pow(99), RoundingMode.HALF_EVEN);
```
```
### 附加功能
Guava還另外提供了一些有用的運算函數
**運算****IntMath****LongMath****BigIntegerMath**\*****最大公約數[`gcd(int, int)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html#gcd%28int,%20int%29)[`gcd(long, long)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#gcd%28long,%20long%29)[`BigInteger.gcd(BigInteger)`](http://docs.oracle.com/javase/6/docs/api/java/math/BigInteger.html#gcd%28java.math.BigInteger%29)取模[`mod(int, int)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html#mod%28int,%20int%29)[`mod(long, long)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#mod%28long,%20long%29)[`BigInteger.mod(BigInteger)`](http://docs.oracle.com/javase/6/docs/api/java/math/BigInteger.html#mod%28java.math.BigInteger%29)取冪[`pow(int, int)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html#pow%28int,%20int%29)[`pow(long, int)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#pow%28long,%20int%29)[`BigInteger.pow(int)`](http://docs.oracle.com/javase/6/docs/api/java/math/BigInteger.html#pow%28int%29)是否2的冪[`isPowerOfTwo(int)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html#isPowerOfTwo%28int%29)[`isPowerOfTwo(long)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#isPowerOfTwo%28long%29)[`isPowerOfTwo(BigInteger)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/BigIntegerMath.html#isPowerOfTwo%28java.math.BigInteger%29)階乘\*[`factorial(int)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html#factorial%28int%29)[`factorial(int)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#factorial%28int%29)[`factorial(int)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/BigIntegerMath.html#factorial%28int%29)二項式系數\*[`binomial(int, int)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html#binomial%28int,%20int%29)[`binomial(int, int)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#binomial%28int,%20int%29)[`binomial(int, int)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/BigIntegerMath.html#binomial%28int,%20int%29)\*BigInteger的最大公約數和取模運算由JDK提供
\*階乘和二項式系數的運算結果如果溢出,則返回MAX\_VALUE
## 浮點數運算
JDK比較徹底地涵蓋了浮點數運算,但Guava在[DoubleMath](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/DoubleMath.html)類中也提供了一些有用的方法。
[`isMathematicalInteger(double)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/DoubleMath.html#isMathematicalInteger%28double%29)判斷該浮點數是不是一個整數[`roundToInt(double, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/DoubleMath.html#roundToInt%28double,%20java.math.RoundingMode%29)舍入為int;對無限小數、溢出拋出異常[`roundToLong(double, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/DoubleMath.html#roundToLong%28double,%20java.math.RoundingMode%29)舍入為long;對無限小數、溢出拋出異常[`roundToBigInteger(double, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/DoubleMath.html#roundToBigInteger%28double,%20java.math.RoundingMode%29)舍入為BigInteger;對無限小數拋出異常[`log2(double, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/DoubleMath.html#log2%28double,%20java.math.RoundingMode%29)2的浮點對數,并且舍入為int,比JDK的Math.log(double) 更快
- 介紹
- Google Guava官方教程(中文版)
- 1-基本工具
- 1.1-使用和避免null
- 1.2-前置條件
- 1.3-常見Object方法
- 1.4-排序: Guava強大的”流暢風格比較器”
- 1.5-Throwables:簡化異常和錯誤的傳播與檢查
- 2-集合
- 2.1-不可變集合
- 2.2-新集合類型
- 2.3-強大的集合工具類:java.util.Collections中未包含的集合工具
- 2.4-集合擴展工具類
- 3-緩存
- 4-函數式編程
- 5-并發
- 5.1-google Guava包的ListenableFuture解析
- 5.2-Google-Guava Concurrent包里的Service框架淺析
- 6-字符串處理:分割,連接,填充
- 7-原生類型
- 9-I/O
- 10-散列
- 11-事件總線
- 12-數學運算
- 13-反射