<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 12-數學運算 [原文鏈接](http://code.google.com/p/guava-libraries/wiki/MathExplained) [譯文鏈接](http://ifeve.com/google-guava-math) **譯者:**沈義揚 ## 范例 ``` 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, int%29) | [`LongMath.checkedAdd`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#checkedAdd%28long, long%29) | |:--- |:--- |:--- |:--- | | [`IntMath.checkedSubtract`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html#checkedSubtract%28int, int%29) | [`LongMath.checkedSubtract`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#checkedSubtract%28long, long%29) | | [`IntMath.checkedMultiply`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html#checkedMultiply%28int, int%29) | [`LongMath.checkedMultiply`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#checkedMultiply%28long, long%29) | | [`IntMath.checkedPow`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html#checkedPow%28int, int%29) | [`LongMath.checkedPow`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#checkedPow%28long, long%29) | ``` 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, int, java.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, long, java.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, java.math.BigInteger, java.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, java.math.RoundingMode%29) | [`log2(long, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#log2%28long, java.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, java.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, java.math.RoundingMode%29) | [`log10(long, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#log10%28long, java.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, java.math.RoundingMode%29) | | 平方根 | [`sqrt(int, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/IntMath.html#sqrt%28int, java.math.RoundingMode%29) | [`sqrt(long, RoundingMode)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#sqrt%28long, java.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, java.math.RoundingMode%29) | ``` // 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, int%29) | [`gcd(long, long)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#gcd%28long, long%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, int%29) | [`mod(long, long)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#mod%28long, long%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, int%29) | [`pow(long, int)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#pow%28long, int%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, int%29) | [`binomial(int, int)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/LongMath.html#binomial%28int, int%29) | [`binomial(int, int)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/math/BigIntegerMath.html#binomial%28int, int%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, java.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, java.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, java.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, java.math.RoundingMode%29) | 2的浮點對數,并且舍入為int,比JDK的Math.log(double) 更快 |
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看