日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jdk 取整数_JDK 15中的确切绝对整数

發布時間:2023/12/3 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jdk 取整数_JDK 15中的确切绝对整数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

jdk 取整數

JDK 15 Early Access Build b18向Math和StrictMath類引入了新方法,這些方法將在提供的值超出方法所支持的范圍時拋出ArithmeticException ,而不會溢出。 這些方法為Java中的“絕對值”概念帶來了Math.addExact , Math.subtractExact和Math.multiplyExact之類的方法帶來的基本算術功能。

在JDK 15之前, Integer.MIN_VALUE和Long.MIN_VALUE使相應的方法Math.abs和StrictMath.abs返回相同的負數,如MIN_VALUE可能的最大負數所表示。 此行為在Javadoc文檔中針對受影響的方法進行了描述,并通過以下所示的代碼進行了演示( 可在GitHub上找到 ):

演示JDK之前的15種絕對值方法

/** * Demonstrates "absExact" methods added to {@link Math} * and {@link StrictMath} with JDK 15 Early Access Build b18 * (JDK-8241374: https://bugs.openjdk.java.net/browse/JDK-8241374 ). */ public class AbsoluteExactness { public void demonstrateMathAbsInteger( final int integer) { out.println( "Math.abs(" + integer + "): " + Math.abs(integer)); } public void longNumber) demonstrateMathAbsLong( final long longNumber) { out.println( "Math.abs(" + longNumber + "L): " + Math.abs(longNumber)); } public void demonstrateStrictMathAbsInteger( final int integer) { out.println( "StrictMath.abs(" + integer + "): " + StrictMath.abs(integer)); } public void longNumber) demonstrateStrictMathAbsLong( final long longNumber) { out.println( "StrictMath.abs(" + longNumber + "L): " + StrictMath.abs(longNumber)); } public static void main( final String[] arguments) { final AbsoluteExactness instance = new AbsoluteExactness(); // Demonstrate pre-JDK 15 Math/StrictMath "abs" functions on minimum values. instance.demonstrateMathAbsInteger(Integer.MIN_VALUE+ 1 ); instance.demonstrateMathAbsInteger(Integer.MIN_VALUE); instance.demonstrateMathAbsLong(Long.MIN_VALUE+ 1 ); instance.demonstrateMathAbsLong(Long.MIN_VALUE); instance.demonstrateStrictMathAbsInteger(Integer.MIN_VALUE+ 1 ); instance.demonstrateStrictMathAbsInteger(Integer.MIN_VALUE); instance.demonstrateStrictMathAbsLong(Long.MIN_VALUE+ 1 ); instance.demonstrateStrictMathAbsLong(Long.MIN_VALUE); } }

執行以上代碼后,將寫入以下輸出:

Math.abs(- 2147483647 ): 2147483647 Math.abs(- 2147483648 ): - 2147483648 Math.abs(-9223372036854775807L): 9223372036854775807 Math.abs(-9223372036854775808L): - 9223372036854775808 StrictMath.abs(- 2147483647 ): 2147483647 StrictMath.abs(- 2147483648 ): - 2147483648 StrictMath.abs(-9223372036854775807L): 9223372036854775807 StrictMath.abs(-9223372036854775808L): - 9223372036854775808

此輸出表明int和long范圍內的最大負允許值導致從Math和StrictMath上的適當abs方法返回相同的值。

JDK 15 Early Access Build b18引入了absExact方法,這種方法在這種情況下拋出ArithmeticException而不是返回負值。 以下代碼( 在GitHub上也有 )證明了這一點:

演示JDK 15引入的絕對方法

public class AbsoluteExactness { public void demonstrateMathAbsExactInteger( final int integer) { try { out.println( "Math.absExact(" + integer + "): " + Math.absExact(integer)); } catch (ArithmeticException exception) { err.println( "Math.absExact(" + integer + "): " + exception); } } public void longNumber) demonstrateMathAbsExactLong( final long longNumber) { try { out.println( "Math.absExact(" + longNumber + "L): " + Math.absExact(longNumber)); } catch (ArithmeticException exception) { err.println( "Math.absExact(" + longNumber + "L): " + exception); } } public void demonstrateStrictMathAbsExactInteger( final int integer) { try { out.println( "StrictMath.absExact(" + integer + "): " + StrictMath.absExact(integer)); } catch (ArithmeticException exception) { err.println( "StrictMath.absExact(" + integer + "):" + exception); } } public void longNumber) demonstrateStrictMathAbsExactLong( final long longNumber) { try { out.println( "StrictMath.absExact(" + longNumber + "L): " + StrictMath.absExact(longNumber)); } catch (ArithmeticException exception) { err.println( "StrictMath.absExact(" + longNumber + "L): " + exception); } } public static void main( final String[] arguments) { final AbsoluteExactness instance = new AbsoluteExactness(); // Demonstrate JDK 15-introduced Math/StrictMath "absExact" functions // on minimum values. instance.demonstrateMathAbsExactInteger(Integer.MIN_VALUE+ 1 ); instance.demonstrateMathAbsExactInteger(Integer.MIN_VALUE); instance.demonstrateMathAbsExactLong(Long.MIN_VALUE+ 1 ); instance.demonstrateMathAbsExactLong(Long.MIN_VALUE); instance.demonstrateStrictMathAbsExactInteger(Integer.MIN_VALUE+ 1 ); instance.demonstrateStrictMathAbsExactInteger(Integer.MIN_VALUE); instance.demonstrateStrictMathAbsExactLong(Long.MIN_VALUE+ 1 ); instance.demonstrateStrictMathAbsExactLong(Long.MIN_VALUE); }

接下來顯示此代碼的輸出,并演示將MIN_VALUE傳遞給absExact方法時引發的清除異常消息。

Math.absExact(- 2147483647 ): 2147483647 Math.absExact(- 2147483648 ): java.lang.ArithmeticException: Overflow to represent absolute value of Integer.MIN_VALUE Math.absExact(-9223372036854775807L): 9223372036854775807 Math.absExact(-9223372036854775808L): java.lang.ArithmeticException: Overflow to represent absolute value of Long.MIN_VALUE StrictMath.absExact(- 2147483647 ): 2147483647 StrictMath.absExact(- 2147483648 ):java.lang.ArithmeticException: Overflow to represent absolute value of Integer.MIN_VALUE StrictMath.absExact(-9223372036854775807L): 9223372036854775807 StrictMath.absExact(-9223372036854775808L): java.lang.ArithmeticException: Overflow to represent absolute value of Long.MIN_VALUE

我發現,通常對于意外的邊緣情況拋出異常要比返回“某物”更好,這需要我閱讀Javadoc來了解該情況是什么以及在該情況下返回了什么。 該異常使我們很明顯地遇到了邊緣情況,而不是發現從絕對值函數調用返回的負數僅在某個時間以后才實現,并且在代碼中“下游”。 如果沒有其他問題,那么僅使用Math.absExact和StrictMath.absExact方法就可以向Java開發人員暗示,在使用Java的數學庫來計算絕對值時要考慮一些“非精確”的可能性,并且這種實現可能導致閱讀Javadoc找出那些不確切的情況。

翻譯自: https://www.javacodegeeks.com/2020/05/exact-absolute-integral-numbers-in-jdk-15.html

jdk 取整數

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的jdk 取整数_JDK 15中的确切绝对整数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。