<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Math 類中的 Java 精確算術運算支持 > 原文: [https://howtodoinjava.com/java8/java-8-exact-airthmetic-operations-supported-in-math-class/](https://howtodoinjava.com/java8/java-8-exact-airthmetic-operations-supported-in-math-class/) Java 8 為 Java 開發人員帶來了許多很棒的特性。 我已經在[比較器更改](//howtodoinjava.com/java8/using-comparator-becomes-easier-with-lambda-expressions-java-8/ "Using Comparator becomes easier with lambda expressions [Java 8]"),[流示例](//howtodoinjava.com/java8/java-8-tutorial-streams-by-examples/ "Java 8 Tutorial: Streams by Examples"),[內部與外部迭代](//howtodoinjava.com/java8/java-8-tutorial-internal-vs-external-iteration/ "Java 8 Tutorial: Internal vs. External Iteration"),[謂詞](//howtodoinjava.com/2014/04/04/how-to-use-predicate-in-java-8/ "How to use Predicate in java 8"),[函數式接口](//howtodoinjava.com/java8/functional-interface-tutorial/ "Functional interface tutorial"),[默認方法](//howtodoinjava.com/java8/default-methods-in-java-8/ "Default methods in java 8"), [lambda 表達式](//howtodoinjava.com/java8/complete-lambda-expressions-tutorial-in-java/ "Complete lambda expressions tutorial")和[日期和時間 API 更改](//howtodoinjava.com/java8/date-and-time-api-changes-in-java-8-lambda/ "Date and Time API changes in Java 8 (Project Kenai)")。 以上所有更改都與 lambda 表達式有關,lambda 表達式是最受關注的獲取者,也是改變游戲規則的人。 除了上述更改之外,我們還獲得了非 lambda 表達式更改。 我已經在上一篇文章中討論過[`String.join()`](//howtodoinjava.com/2014/05/02/java-8-string-join-csv-example/ "Java 8: String join (CSV) example")方法。 在本文中,我將討論在`Math類中進行的**更改**,以支持精確的算術。 ```java Sections in this post: 1) (add|substract|multiply|increment|decrement|negate)Exact methods 2) floorMod and floorDiv methods 3) toIntExact method 4) nextDown method ``` 讓我們一一討論。 ## 1)加減乘除和求反的精確方法 `Math`類提供了這些新方法,每次操作結果溢出到最大限制時,這些方法都會引發`java.lang.ArithmeticException`異常。 以上所有方法都將參數作為`int`或`long`原始類型。 例如考慮乘以`100000 * 100000`。通常,這樣做會“無聲地”給您帶來錯誤的回報,并且在應用程序運行時,您有責任每次檢查最大限制以避免錯誤的計算。 在 Java 8 中,`multiPlyExact`方法將為您完成此工作,并且如果結果超出最大限制,則將引發異常。 ```java //Normal multiplication System.out.println( 100000 * 100000 ); //Using multiPlyExact System.out.println( Math.multiplyExact( 100000 , 100000 )); Output: 1410065408 //Incorrect result Exception in thread "main" java.lang.ArithmeticException: integer overflow at java.lang.Math.multiplyExact(Math.java:867) at java8features.MathematicalFuctions.main(MathematicalFuctions.java:8) ``` 其他操作也會發生相同的情況。 例如添加操作與`addExact` ```java //Normal add operation System.out.println( Integer.MAX_VALUE + Integer.MAX_VALUE ); //Using addExact System.out.println( Math.addExact( Integer.MAX_VALUE , Integer.MAX_VALUE )); Output: -2 //incorrect result Exception in thread "main" java.lang.ArithmeticException: integer overflow at java.lang.Math.addExact(Math.java:790) at java8features.MathematicalFuctions.main(MathematicalFuctions.java:11) ``` ## 2)`floorMod`和`floorDiv`方法 Java 8 已經解決了很長的整數余數問題。 大家都知道`n % 2`是 **i)** 0:如果數字是偶數 **ii)** 1:如果是奇數 如果數字為負怎么辦。 上面的表達式可以/不能返回 -1。 如果事實為負數,結果對我來說是不可預測的。 ```java System.out.println( 10 % 2 ); System.out.println( 11 % 2 ); System.out.println( -15 % 2 ); System.out.println( -16 % 2 ); Output: 0 1 -1 0 ``` 現在,使用 Java 8 附帶的精確數學執行上述取模操作。 ```java System.out.println( Math.floorMod(10 , 2) ); System.out.println( Math.floorMod(11 , 2) ); System.out.println( Math.floorMod(-15 , 2) ); System.out.println( Math.floorMod(-16 , 2) ); Output: 0 1 1 0 ``` 類似地,另一個問題可能是**獲取時鐘**的時針位置。 假設當前時間是 10 點。 您已經調整了 n 小時,現在想獲得排名。 公式很簡單: `Current Position = (position + adjustment) % 12` 如果將(位置+調整)計算為位置編號,則效果很好。 但是,如果時針逆時針移動,從而(位置+調整)變為負數,該怎么辦。 讓我們檢查。 ```java System.out.println( (10+3) % 12 ); System.out.println( (5+6) % 12 ); System.out.println( (10-27) % 12 ); Output: 1 11 -5 //This is incorrect ``` 現在,使用精確的浮點方法`floorMod`。 ```java System.out.println( Math.floorMod(10+3 , 12) ); System.out.println( Math.floorMod(5+6 , 12) ); System.out.println( Math.floorMod(10-27 , 12) ); Output: 1 11 7 //This is correct ``` 在`floorDiv()`方法中也進行了類似的更改。 ## 3)`toIntExact`方法 當您嘗試為`int`類型變量分配一個`long`值時,此方法很方便。 雖然這種情況并非易事,但在極少數情況下可能會需要。 如果`long`值大于最大`int`值,則正常的`long`到`int`轉換會導致數據錯誤。 如果發生類似這種情況,`toIntExact()`方法將引發異常。 ```java System.out.println( Long.MAX_VALUE ); System.out.println( (int)Long.MAX_VALUE ); System.out.println( Math.toIntExact(10_00_00_000) ); System.out.println( Math.toIntExact(Long.MAX_VALUE) ); Output: 9223372036854775807 -1 100000000 Exception in thread "main" java.lang.ArithmeticException: integer overflow at java.lang.Math.toIntExact(Math.java:1011) at java8features.MathematicalFuctions.main(MathematicalFuctions.java:46) ``` ## 4)`nextDown`方法 這也是 Java 8 套件中的一個明顯新增特性。 當您需要返回小于 n 的數字時,這非常有用。 您計算的返回數字恰好是 n。 然后,您可以使用此方法查找最接近 n(但仍小于 n)的數字。 ```java System.out.println( Math.nextDown(100) ); System.out.println( Math.nextDown(100.365) ); Output: 99.99999 100.36499999999998 ``` 請注意,自 Java 6 起,`Math.nextUp()`已經存在。在 Java 8 中僅添加了`Math.nextDown()`。 就是這樣。 希望您喜歡閱讀。 您的想法和評論總是受到歡迎。 **學習愉快!**
                  <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>

                              哎呀哎呀视频在线观看