Java 8 Math New Methods – Java 8数学新方法

最后修改: 2018年 3月 18日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

Usually, when we think about the new features that came with version 8 of Java, functional programming and lambda expressions are first things that come to mind.

通常,当我们想到Java第8版带来的新功能时,首先想到的是函数式编程和lambda表达式。

Nevertheless, besides those big features there’re others, maybe having a smaller impact but also interesting and many times not really well known or even covered by any review.

然而,除了这些大的功能外,还有其他的功能,也许影响较小,但也很有趣,而且很多时候并不为人所知,甚至没有任何评论涉及。

In this tutorial, we’ll enumerate and give a little example of each of the new methods added to one of the core classes of the language: java.lang.Math.

在本教程中,我们将列举并举例说明添加到该语言的一个核心类中的每个新方法。java.lang.Math/a>

2. New *exact() Methods

2.新的*exact()方法

First, we have a group of new methods that extend some of the existing and most common arithmetic operations.

首先,我们有一组新的方法,扩展了一些现有的和最常见的算术运算。

As we’ll see, they’re quite self-explanatory, as they have exactly the same functionality than the methods they derive from but with the addition of throwing an exception in case, the resulting value overflows the max or min values of their types.

正如我们所看到的,它们是不言自明的,因为它们的功能与它们所派生的方法完全相同,但增加了一个异常,如果产生的值超过其类型的最大值或最小值。

We can use these methods with both integers and longs as parameters.

我们可以用整数作为参数来使用这些方法。

2.1. addExact()

2.1.addExact()

Adds the two parameters, throwing an ArithmeticException in case of overflow (which goes for all *Exact() methods) of the addition:

将两个参数相加,在溢出的情况下抛出一个ArithmeticException(这适用于所有*Exact()方法)的加法。

Math.addExact(100, 50);               // returns 150
Math.addExact(Integer.MAX_VALUE, 1);  // throws ArithmeticException

2.2. substractExact()

2.2.substractExact()

Substracts the value of the second parameter from the first one, throwing an ArithmeticException in case of overflow of the subtraction:

从第一个参数中抽象出第二个参数的值,如果减法出现溢出,则抛出ArithmeticException

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

2.3. incrementExact()

2.3.incrementExact()

Increments the parameter by one, throwing an ArithmeticException in case of overflow:

将参数增加1,如果出现溢出,则抛出ArithmeticException

Math.incrementExact(100);               // returns 101
Math.incrementExact(Integer.MAX_VALUE); // throws ArithmeticException

2.4. decrementExact()

2.4.decrementExact()

Decrements the parameter by one, throwing an ArithmeticException in case of overflow:

将参数递减1,如果溢出,则抛出ArithmeticException

Math.decrementExact(100);            // returns 99
Math.decrementExact(Long.MIN_VALUE); // throws ArithmeticException

2.5. multiplyExact()

2.5.multiplyExact()

Multiply the two parameters, throwing an ArithmeticException in case of overflow of the product:

两个参数相乘,如果乘积溢出,则抛出ArithmeticException

Math.multiplyExact(100, 5);            // returns 500
Math.multiplyExact(Long.MAX_VALUE, 2); // throws ArithmeticException

2.6. negateExact()

2.6.negateExact()

Changes the sign of the parameter, throwing an ArithmeticException in case of overflow.

改变参数的符号,如果出现溢出,则抛出ArithmeticException

In this case, we have to think about the internal representation of the value in memory to understand why there’s an overflow, as is not as intuitive as the rest of the “exact” methods:

在这种情况下,我们必须考虑内存中数值的内部表示,以了解为什么会有溢出,因为它不像其他 “精确 “方法那样直观。

Math.negateExact(100);               // returns -100
Math.negateExact(Integer.MIN_VALUE); // throws ArithmeticException

The second example requires an explanation as it’s not obvious: The overflow is due to the Integer.MIN_VALUE being −2.147.483.648, and on the other side the Integer.MAX_VALUE being 2.147.483.647 so the returned value doesn’t fit into an Integer by one unit.

第二个例子需要解释一下,因为它并不明显。溢出是由于Integer.MIN_VALUE是-2.147.483.648,而另一边的Integer.MAX_VALUE是2.147.483.647所以返回的值不符合一个单位的Integer

3. Other Methods

3.其他方法

3.1. floorDiv()

3.1.floorDiv()

Divides the first parameter by the second one, and then performs a floor() operation over the result, returning the Integer that is less or equal to the quotient:

用第一个参数除以第二个参数,然后对结果进行floor()操作,返回小于或等于商的Integer

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

The exact quotient is 3.5 so floor(3.5) == 3.

准确的商是3.5,所以floor(3.5) ==3。

Let’s look at another example:

让我们看看另一个例子。

Math.floorDiv(-7, 2)); // returns -4

The exact quotient is -3.5 so floor(-3.5) == -4.

准确的商是-3.5,所以floor(-3.5) ==-4。

3.2. modDiv()

3.2. modDiv()

This one is similar to the previous method floorDiv(), but applying the floor() operation over the modulus or remainder of the division instead of the quotient:

这个方法类似于之前的floorDiv(),但是在除法的模数或余数上应用floor()操作,而不是商。

Math.modDiv(5, 3));  // returns 2

As we can see, the modDiv() for two positive numbers is the same as % operator. Let’s look at a different example:

我们可以看到,两个正数的modDiv()与%操作符相同。我们来看看一个不同的例子。

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

It returns 1 and not 2 because floorDiv(-5, 3) is -2 and not -1.

它返回1而不是2,因为floorDiv(-5, 3)是-2而不是1。

3.3. nextDown()

3.3.nextDown()

Returns the immediately lower value of the parameter (supports float or double parameters):

返回参数的近似低值(支持floatdouble参数)。

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

4. Conclusion

4.结论

In this article, we’ve described briefly the functionality of all the new methods added to the class java.lang.Math in the version 8 of the Java platform and also seen some examples of how to use them.

在这篇文章中,我们简要介绍了Java平台第8版中java.lang.Math类中增加的所有新方法的功能,也看到了一些如何使用它们的例子。

As always, the full source code is available over on GitHub.

一如既往,完整的源代码可在GitHub上获得