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

jdk 取整数

JDK 15 Early Access Build b18MathStrictMath类引入了新方法,这些方法将在提供的值超出方法所支持的范围时抛出ArithmeticException ,而不会溢出。 这些方法为Java中的“绝对值”概念带来了Math.addExactMath.subtractExactMath.multiplyExact之类的方法带来的基本算术功能。

在JDK 15之前, Integer.MIN_VALUELong.MIN_VALUE使相应的方法Math.absStrictMath.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 

此输出表明intlong范围内的最大负允许值导致从MathStrictMath上的适当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.absExactStrictMath.absExact方法就可以向Java开发人员暗示,在使用Java的数学库来计算绝对值时要考虑一些“非精确”的可能性,并且这种实现可能导致阅读Javadoc找出那些不确切的情况。

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

jdk 取整数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值