java8 Math新增方法介绍
通常都认为java8新功能主要包括函数式编程及lambda表达式。然而,除了那些大的特点之外,还有其他的,影响力小却很有趣,大多时候不为人所知,甚至不太被人评论。
本文我们列举java.lang.Math类中新增的方法,并给一些小的示例来说明。
*exact()方法
首先看一组扩展已经存在的常用算术操作方法,从名称及可以知其意,处理实现原有功能外,还增加了当结果溢出时抛出异常。这些方法可以使用integer和long类型作为参数。
addExact()
返回两个参数之和,结果溢出时抛出ArithmeticException异常:
Math.addExact(100,50);//returns150
Math.addExact(Integer.MAX_VALUE,1);//throwsArithmeticException
substractExact()方法
返回两个参数之差,结果溢出时抛出ArithmeticException异常:
Math.subtractExact(100,50);//returns50
Math.subtractExact(Long.MIN_VALUE,1);//throwsArithmeticException
incrementExact()方法
返回参数值加一,结果溢出时抛出ArithmeticException异常:
Math.incrementExact(100);//returns101
Math.incrementExact(Integer.MAX_VALUE);//throwsArithmeticException
decrementExact()方法
返回参数值减一,结果溢出时抛出ArithmeticException异常:
Math.decrementExact(100);//returns99
Math.decrementExact(Long.MIN_VALUE);//throwsArithmeticException
multiplyExact()方法
返回两个参数之积,结果溢出时抛出ArithmeticException异常:
Math.multiplyExact(100,5);//returns500
Math.multiplyExact(Long.MAX_VALUE,2);//throwsArithmeticException
negateExact()方法
改变参数符号,结果溢出时抛出ArithmeticException异常。我们来看看值在内存中的表示,并理解为什么会溢出,因为并不像其他exact方法那么直观看出来:
Math.negateExact(100);//returns-100
Math.negateExact(Integer.MIN_VALUE);//throwsArithmeticException
第二个示例需要解释下,因为不能一眼看出来:溢出是因为Integer.MIN_VALUE是−2.147.483.648,而Integer.MAX_VALUE是2.147.483.647,所以返回值超出整数范围。
其他方法
floorDiv()
第一个参数除以第二参数,然后针对结果执行floor操作,返回小于或等于商的整数:
Math.floorDiv(7,2));//returns3
商为3.5,所以floor(3.5)==3.
让我们看另一个示例:
Math.floorDiv(-7,2));//returns-4
商为-3.5,所以floor(-3.5)==-4.
modDiv()方法
该方法与前面floorDiv()方法类似,但在模数或余数上应用floor()操作,而不是商:
Math.modDiv(5,3));//returns2
我们看到,modDiv()方法两个参数为正数,和%操作符效果一样。让看看另一个不同示例:
Math.modDiv(-5,3));//returns1
结果为1而不是2,因为floorDiv(-5,3)是-2,而不是-1.
nextDown()方法
返回参数直接较低的值(支持float或double参数):
floatf=Math.nextDown(3);//returns2.9999998
doubled=Math.nextDown(3);//returns2.999999761581421
总结
我们描述了java8中java.lang.Math类中所有新的方法,并通过示例给与解释说明。
补充知识:math类常用方法详细总结
说明:Math类位于java.lang包中,主要提供了一些常用的数学函数和计算
备注:一些在工程计算中才用的方法此处不做介绍
一、取整运算
1、Math.ceil(doublex)
释义:向上取整,返回大于该值的最近double值
System.out.println(Math.ceil(1.23));//2.0
System.out.println(Math.ceil(-1.23));//-1.0
2、Math.floor(doublex)
释义:向下取整,返回小于该值的最近double值
System.out.println(Math.floor(1.23));//1.0
System.out.println(Math.floor(-1.23));//-2.0
3、Math.round(doublex)
释义:四舍五入取整,参数类型:double、float
System.out.println(Math.round(1.43));//1 System.out.println(Math.round(1.53));//2 System.out.println(Math.round(-1.43));//-1 System.out.println(Math.round(-1.53));//-2
二、随机运算
Math.random()
释义:内部调用了Random.nextDouble()方法,生成一个伪均匀分布在[0,1)之间的double值
System.out.println((int)(Math.random()*10+1));//生成范围再[1,11)之间的伪随机数
三、符号运算
1、Math.abs(intx)
释义:计算绝对值,参数类型:int、long、float、double
System.out.println(Math.abs(-1));//1
2、Math.negateExact(intx)
释义:计算相反值,参数类型:int、long
System.out.println(Math.negateExact(-1));//1
四、大小运算
1、Math.max(intx,inty)
释义:获取两个参数中的最大值,参数类型:int、long、float、double
System.out.println(Math.max(1,2));//2
2、Math.min(intx,inty)
释义:获取两个参数中的最小值,参数类型:int、long、float、double
System.out.println(Math.min(1,2));//1
3、Math.rint(doublex)
释义:获取与参数最接近的double值
System.out.println(Math.rint(1.4));//1.0 System.out.println(Math.rint(1.5));//2.0 System.out.println(Math.rint(-1.4));//-1.0 System.out.println(Math.rint(-1.5));//-2.0
以上这篇java8Math新增方法介绍就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。