java8 Math新增方法 java8 Math新增方法介绍

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

java8 Math新增方法 java8 Math新增方法介绍

neweastsun   2021-02-03 我要评论
想了解java8 Math新增方法介绍的相关内容吗,neweastsun在本文为您仔细讲解java8 Math新增方法的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java8,Math,新增方法,下面大家一起来学习吧。

通常都认为java8新功能主要包括函数式编程及lambda表达式。然而,除了那些大的特点之外,还有其他的,影响力小却很有趣,大多时候不为人所知,甚至不太被人评论。

本文我们列举java.lang.Math类中新增的方法,并给一些小的示例来说明。

*exact() 方法

首先看一组扩展已经存在的常用算术操作方法,从名称及可以知其意,处理实现原有功能外,还增加了当结果溢出时抛出异常。这些方法可以使用integer和long类型作为参数。

addExact()

返回两个参数之和,结果溢出时抛出ArithmeticException 异常:

Math.addExact(100, 50); // returns 150

Math.addExact(Integer.MAX_VALUE, 1); // throws ArithmeticException

substractExact()方法

返回两个参数之差,结果溢出时抛出ArithmeticException 异常:

Math.subtractExact(100, 50); // returns 50

Math.subtractExact(Long.MIN_VALUE, 1); // throws ArithmeticException

incrementExact()方法

返回参数值加一,结果溢出时抛出ArithmeticException 异常:

Math.incrementExact(100); // returns 101

Math.incrementExact(Integer.MAX_VALUE); // throws ArithmeticException

decrementExact()方法

返回参数值减一,结果溢出时抛出ArithmeticException 异常:

Math.decrementExact(100); // returns 99

Math.decrementExact(Long.MIN_VALUE); // throws ArithmeticException

multiplyExact()方法

返回两个参数之积,结果溢出时抛出ArithmeticException 异常:

Math.multiplyExact(100, 5); // returns 500

Math.multiplyExact(Long.MAX_VALUE, 2); // throws ArithmeticException

negateExact()方法

改变参数符号,结果溢出时抛出ArithmeticException 异常。我们来看看值在内存中的表示,并理解为什么会溢出,因为并不像其他exact方法那么直观看出来:

Math.negateExact(100); // returns -100

Math.negateExact(Integer.MIN_VALUE); // throws ArithmeticException

第二个示例需要解释下,因为不能一眼看出来:溢出是因为Integer.MIN_VALUE 是 −2.147.483.648,而Integer.MAX_VALUE 是 2.147.483.647,所以返回值超出整数范围。

其他方法

floorDiv()

第一个参数除以第二参数,然后针对结果执行floor操作,返回小于或等于商的整数:

Math.floorDiv(7, 2)); // returns 3

商为 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)); // returns 2

我们看到 , modDiv() 方法两个参数为正数,和 % 操作符效果一样。让看看另一个不同示例:

Math.modDiv(-5, 3)); // returns 1

结果为 1 而不是 2 ,因为floorDiv(-5, 3) 是 -2 ,而不是 -1.

nextDown()方法

返回参数直接较低的值(支持 float 或 double 参数):

float f = Math.nextDown(3); // returns 2.9999998

double d = Math.nextDown(3); // returns 2.999999761581421

总结

我们描述了java8中java.lang.Math类中所有新的方法,并通过示例给与解释说明。

补充知识:math类常用方法详细总结

说明:Math 类位于 java.lang 包中,主要提供了一些常用的数学函数和计算

备注:一些在工程计算中才用的方法此处不做介绍

一、取整运算

1、Math.ceil(double x)

释义:向上取整,返回大于该值的最近 double 值

System.out.println(Math.ceil(1.23)); // 2.0

System.out.println(Math.ceil(-1.23)); // -1.0

2、Math.floor(double x)

释义:向下取整,返回小于该值的最近 double 值

System.out.println(Math.floor(1.23)); // 1.0

System.out.println(Math.floor(-1.23)); // -2.0

3、Math.round(double x)

释义:四舍五入取整,参数类型: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(int x)

释义:计算绝对值,参数类型:int、long、float、double

System.out.println(Math.abs(-1)); // 1

2、Math.negateExact(int x)

释义:计算相反值,参数类型:int、long

System.out.println(Math.negateExact(-1)); // 1

四、大小运算

1、Math.max(int x, int y)

释义:获取两个参数中的最大值,参数类型:int、long、float、double

System.out.println(Math.max(1, 2)); // 2

2、Math.min(int x, int y)

释义:获取两个参数中的最小值,参数类型:int、long、float、double

System.out.println(Math.min(1, 2)); // 1

3、Math.rint(double x)

释义:获取与参数最接近的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

以上这篇java8 Math新增方法介绍就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们