[Google Guava] 12-数学运算
原文鏈接?譯文鏈接?譯者:沈義揚(yáng)
范例
| 1 | int?logFloor = LongMath.log2(n, FLOOR); |
| 2 | int?mustNotOverflow = IntMath.checkedMultiply(x, y); |
| 3 | long?quotient = LongMath.divide(knownMultipleOfThree,?3, RoundingMode.UNNECESSARY);?// fail fast on non-multiple of 3 |
| 4 | BigInteger nearestInteger = DoubleMath.roundToBigInteger(d, RoundingMode.HALF_EVEN); |
| 5 | BigInteger sideLength = BigIntegerMath.sqrt(area, CEILING); |
為什么使用Guava Math
- Guava Math針對(duì)各種不常見(jiàn)的溢出情況都有充分的測(cè)試;對(duì)溢出語(yǔ)義,Guava文檔也有相應(yīng)的說(shuō)明;如果運(yùn)算的溢出檢查不能通過(guò),將導(dǎo)致快速失敗;
- Guava Math的性能經(jīng)過(guò)了精心的設(shè)計(jì)和調(diào)優(yōu);雖然性能不可避免地依據(jù)具體硬件細(xì)節(jié)而有所差異,但Guava Math的速度通常可以與Apache Commons的MathUtils相比,在某些場(chǎng)景下甚至還有顯著提升;
- Guava Math在設(shè)計(jì)上考慮了可讀性和正確的編程習(xí)慣;IntMath.log2(x, CEILING) 所表達(dá)的含義,即使在快速閱讀時(shí)也是清晰明確的。而32-Integer.numberOfLeadingZeros(x – 1)對(duì)于閱讀者來(lái)說(shuō)則不夠清晰。
注意:Guava Math和GWT格外不兼容,這是因?yàn)镴ava和Java Script語(yǔ)言的運(yùn)算溢出邏輯不一樣。
整數(shù)運(yùn)算
Guava Math主要處理三種整數(shù)類(lèi)型:int、long和BigInteger。這三種類(lèi)型的運(yùn)算工具類(lèi)分別叫做IntMath、LongMath和BigIntegerMath。
有溢出檢查的運(yùn)算
Guava Math提供了若干有溢出檢查的運(yùn)算方法:結(jié)果溢出時(shí),這些方法將快速失敗而不是忽略溢出
| IntMath.checkedAdd | LongMath.checkedAdd |
| IntMath.checkedSubtract | LongMath.checkedSubtract |
| IntMath.checkedMultiply | LongMath.checkedMultiply |
| IntMath.checkedPow | LongMath.checkedPow |
| 1 | IntMath.checkedAdd(Integer.MAX_VALUE, Integer.MAX_VALUE);?// throws ArithmeticException |
實(shí)數(shù)運(yùn)算
IntMath、LongMath和BigIntegerMath提供了很多實(shí)數(shù)運(yùn)算的方法,并把最終運(yùn)算結(jié)果舍入成整數(shù)。這些方法接受一個(gè)java.math.RoundingMode枚舉值作為舍入的模式:
- DOWN:向零方向舍入(去尾法)
- UP:遠(yuǎn)離零方向舍入
- FLOOR:向負(fù)無(wú)限大方向舍入
- CEILING:向正無(wú)限大方向舍入
- UNNECESSARY:不需要舍入,如果用此模式進(jìn)行舍入,應(yīng)直接拋出ArithmeticException
- HALF_UP:向最近的整數(shù)舍入,其中x.5遠(yuǎn)離零方向舍入
- HALF_DOWN:向最近的整數(shù)舍入,其中x.5向零方向舍入
- HALF_EVEN:向最近的整數(shù)舍入,其中x.5向相鄰的偶數(shù)舍入
這些方法旨在提高代碼的可讀性,例如,divide(x, 3, CEILING) 即使在快速閱讀時(shí)也是清晰。此外,這些方法內(nèi)部采用構(gòu)建整數(shù)近似值再計(jì)算的實(shí)現(xiàn),除了在構(gòu)建sqrt(平方根)運(yùn)算的初始近似值時(shí)有浮點(diǎn)運(yùn)算,其他方法的運(yùn)算全過(guò)程都是整數(shù)或位運(yùn)算,因此性能上更好。
| 運(yùn)算 | IntMath | LongMath | BigIntegerMath |
| 除法 | divide(int, int, RoundingMode) | divide(long, long, RoundingMode) | divide(BigInteger, BigInteger, RoundingMode) |
| 2為底的對(duì)數(shù) | log2(int, RoundingMode) | log2(long, RoundingMode) | log2(BigInteger, RoundingMode) |
| 10為底的對(duì)數(shù) | log10(int, RoundingMode) | log10(long, RoundingMode) | log10(BigInteger, RoundingMode) |
| 平方根 | sqrt(int, RoundingMode) | sqrt(long, RoundingMode) | sqrt(BigInteger, RoundingMode) |
| 1 | // returns 31622776601683793319988935444327185337195551393252 |
| 2 | BigIntegerMath.sqrt(BigInteger.TEN.pow(99), RoundingMode.HALF_EVEN); |
附加功能
Guava還另外提供了一些有用的運(yùn)算函數(shù)
| 運(yùn)算 | IntMath | LongMath | BigIntegerMath* |
| 最大公約數(shù) | gcd(int, int) | gcd(long, long) | BigInteger.gcd(BigInteger) |
| 取模 | mod(int, int) | mod(long, long) | BigInteger.mod(BigInteger) |
| 取冪 | pow(int, int) | pow(long, int) | BigInteger.pow(int) |
| 是否2的冪 | isPowerOfTwo(int) | isPowerOfTwo(long) | isPowerOfTwo(BigInteger) |
| 階乘* | factorial(int) | factorial(int) | factorial(int) |
| 二項(xiàng)式系數(shù)* | binomial(int, int) | binomial(int, int) | binomial(int, int) |
*BigInteger的最大公約數(shù)和取模運(yùn)算由JDK提供
*階乘和二項(xiàng)式系數(shù)的運(yùn)算結(jié)果如果溢出,則返回MAX_VALUE
浮點(diǎn)數(shù)運(yùn)算
JDK比較徹底地涵蓋了浮點(diǎn)數(shù)運(yùn)算,但Guava在DoubleMath類(lèi)中也提供了一些有用的方法。
| isMathematicalInteger(double) | 判斷該浮點(diǎn)數(shù)是不是一個(gè)整數(shù) |
| roundToInt(double, RoundingMode) | 舍入為int;對(duì)無(wú)限小數(shù)、溢出拋出異常 |
| roundToLong(double, RoundingMode) | 舍入為long;對(duì)無(wú)限小數(shù)、溢出拋出異常 |
| roundToBigInteger(double, RoundingMode) | 舍入為BigInteger;對(duì)無(wú)限小數(shù)拋出異常 |
| log2(double, RoundingMode) | 2的浮點(diǎn)對(duì)數(shù),并且舍入為int,比JDK的Math.log(double) 更快 |
原創(chuàng)文章,轉(zhuǎn)載請(qǐng)注明:?轉(zhuǎn)載自并發(fā)編程網(wǎng) – ifeve.com本文鏈接地址:?[Google Guava] 12-數(shù)學(xué)運(yùn)算
from:?http://ifeve.com/google-guava-math/
總結(jié)
以上是生活随笔為你收集整理的[Google Guava] 12-数学运算的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [Google Guava] 7-原生类
- 下一篇: [Google Guava] 排序: G