Infinity in Java – Java中的无穷大

最后修改: 2022年 10月 12日

1. Overview

1.概述

In this tutorial, we’ll have a look at the concept of infinity in Java and how we can work with it.

在本教程中,我们将看看Java中的无穷大概念,以及我们如何使用它。

2. Intro to Numbers in Java

2.Java中的数字介绍

In mathematics, we have a set of real numbers and a set of integers. Evidently, both of these sets are unlimited, and both contain positive and negative infinity.

在数学上,我们有一个实数集和一个整数集。显而易见,这两个集合都是无限的,都包含正负无穷大。

In the computer world, we need a memory location where we can store the values for these sets, and this location must be finite as the memory of a computer is finite.

在计算机世界中,我们需要一个可以存储这些集合的数值的内存位置,而且这个位置必须是有限的,因为计算机的内存是有限的。

For int type in Java, the concept of infinity is not covered. We can only store integer numbers that fit in the memory location that we chose.

对于Java中的int类型,没有涵盖无限的概念。我们只能存储适合我们选择的内存位置的整数。

For real numbers, we also have the concept of infinity, either positive or negative. The 32 bits float type and also the 64 bits double type supports this in Java. Moving forward, we’ll use the double type for exemplifying as it is also the most used type for real numbers in Java, for it has better precision.

对于实数,我们也有无限的概念,无论是正数还是负数。32位的float类型和64位的double类型在Java中支持这个概念。接下来,我们将使用double类型来举例说明,因为它也是Java中最常用的实数类型,因为它的精度更高。

3. Positive Infinity

3.积极的无限性

The constant Double.POSITIVE_INFINITY holds the positive infinity value of type double. This value is obtained by dividing 1.0 by 0.0. Its String representation is Infinity. This value is a convention, and its hexadecimal representation is 7FF0000000000000. Every double variable with this bitwise value contains positive infinity.

常数Double.POSITIVE_INFINITY持有double类型的正无穷值。这个值是由1.0除以0.0得到的。它的String表示法是Infinity。这个值是一个惯例,它的十六进制表示是7FF0000000000000。每个具有这个位值的double变量都包含正的无穷大。

4. Negative Infinity

4.负无穷大

The constant Double.NEGATIVE_INFINITY holds the negative infinity value of type double. This value is obtained by dividing -1.0 by 0.0. Its String representation is -Infinity. This value is also a convention, and its hexadecimal representation is FFF0000000000000. Every double variable with this bitwise value contains negative infinity.

常数Double.NEGATIVE_INFINITY持有double类型的负无限值。这个值是由-1.0除以0.0得到的。它的字符串表示是-Infinity。这个值也是一个惯例,它的十六进制表示是FFF0000000000000。每个具有这个位值的double变量都包含负无穷。

5. Operations with Infinity

5.与无限的操作

Let’s declare a double variable called positiveInfinity and assign to it the value Double.POSITIVE_INFINITY and another double variable negativeInfinity and assign to it value Double.NEGATIVE_INFINITY. Then, we get the following results for the operations:

让我们声明一个名为positiveInfinitydouble变量,并为其赋值Double.POSITIVE_INFINITY,以及另一个double变量negativeInfinity并为其赋值Double.NEGATIVE_INFINITY。然后,我们得到以下的操作结果。

Double positiveInfinity = Double.POSITIVE_INFINITY;
Double negativeInfinity = Double.NEGATIVE_INFINITY;
        
assertEquals(Double.NaN, (positiveInfinity + negativeInfinity));
assertEquals(Double.NaN, (positiveInfinity / negativeInfinity));
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity - negativeInfinity));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity - positiveInfinity));
assertEquals(Double.NEGATIVE_INFINITY, (positiveInfinity * negativeInfinity));

Here, the constant Double.NaN represents a result that is not a number.

这里,常数Double.NaN表示一个不是数字的结果。

Let’s see the mathematical operations with infinity and a positive number:

让我们看看无限大和正数的数学运算。

Double positiveInfinity = Double.POSITIVE_INFINITY;
Double negativeInfinity = Double.NEGATIVE_INFINITY;
double positiveNumber = 10.0; 
        
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity + positiveNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity + positiveNumber));
        
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity - positiveNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity - positiveNumber));
        
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity * positiveNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity * positiveNumber));
       
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity / positiveNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity / positiveNumber));
        
assertEquals(Double.NEGATIVE_INFINITY, (positiveNumber - positiveInfinity));
assertEquals(Double.POSITIVE_INFINITY, (positiveNumber - negativeInfinity));
        
assertEquals(0.0, (positiveNumber / positiveInfinity));
assertEquals(-0.0, (positiveNumber / negativeInfinity));

Now, let’s see the mathematical operations with infinity and a negative number:

现在,让我们看看无限大和负数的数学运算。

Double positiveInfinity = Double.POSITIVE_INFINITY;
Double negativeInfinity = Double.NEGATIVE_INFINITY;
double negativeNumber = -10.0; 
        
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity + negativeNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity + negativeNumber));
        
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity - negativeNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity - negativeNumber));
        
assertEquals(Double.NEGATIVE_INFINITY, (positiveInfinity * negativeNumber));
assertEquals(Double.POSITIVE_INFINITY, (negativeInfinity * negativeNumber));
        
assertEquals(Double.NEGATIVE_INFINITY, (positiveInfinity / negativeNumber));
assertEquals(Double.POSITIVE_INFINITY, (negativeInfinity / negativeNumber));
        
assertEquals(Double.NEGATIVE_INFINITY, (negativeNumber - positiveInfinity));
assertEquals(Double.POSITIVE_INFINITY, (negativeNumber - negativeInfinity));
        
assertEquals(-0.0, (negativeNumber / positiveInfinity));
assertEquals(0.0, (negativeNumber / negativeInfinity));

There are a few rules of thumb to remember these operations better:

有几条经验法则可以更好地记住这些操作。

  • Replace the negative and positive infinities with Infinity and -Infinity, respectively, and perform the sign operations first
  • For any operation between a number different from zero and infinity, you will get a result of infinity
  • When we add or divide positive and negative infinity, we get not a number result

6. Division by Zero

6.除以零

The division by zero is a special case of division because it produces negative and positive infinity values.

除以0是除法的一个特例,因为它产生负的和正的无穷大的数值。

To exemplify, let’s take a double value d and check the results of the following divisions by zero:

为了举例说明,让我们拿一个doubled,检查以下除以0的结果。

double d = 1.0;
        
assertEquals(Double.POSITIVE_INFINITY, (d / 0.0));
assertEquals(Double.NEGATIVE_INFINITY, (d / -0.0));
assertEquals(Double.NEGATIVE_INFINITY, (-d / 0.0));
assertEquals(Double.POSITIVE_INFINITY, (-d / -0.0));

7. Conclusion

7.结语

In this article, we explored the concept and usage of positive and negative infinity in Java. The implementation and code snippets can be found over on GitHub.

在这篇文章中,我们探讨了Java中正负无限的概念和用法。实现和代码片段可以在GitHub上找到over on GitHub>。