1. 概述

在这个教程中,我们将探讨Java中的无穷大概念以及如何处理它。

2. Java中的数字简介

在数学中,我们有一组实数和一组整数。显然,这两个集合都是无限的,包含正负无穷。在计算机世界里,我们需要一个内存位置来存储这些集合的值,而这个位置必须是有限的,因为计算机的内存是有限的。

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

对于实数,我们也支持正负无穷的概念。 Java的32位float类型和64位double类型支持这一点。后续我们将使用double类型为例,因为它也是Java中用于表示实数的最常用类型,因为它具有更好的精度。

3. 正无穷大

常量Double.POSITIVE_INFINITY持有double类型的正无穷大值。这个值通过将1.0除以0.0得到。其字符串表示形式为Infinity。这个值是一种约定,其十六进制表示为7FF0000000000000。具有此位模式的所有double变量都包含正无穷大。

4. 负无穷大

常量Double.NEGATIVE_INFINITY持有double类型的负无穷大值。这个值通过将-1.0除以0.0得到。其字符串表示形式为-Infinity。这个值也是一种约定,其十六进制表示为FFF0000000000000。具有此位模式的所有double变量都包含负无穷大。

5. 与无穷大的运算

让我们声明一个名为positiveInfinitydouble变量并将其值设置为Double.POSITIVE_INFINITY,另一个名为negativeInfinitydouble变量并将其值设置为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));

这里,常量Double.NaN表示非数字结果。

现在,让我们看看与正数的数学运算:

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));

接下来,看看与负数的数学运算:

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));

要更好地记住这些运算规则,可以记住以下几点:

  • 将正负无穷大替换为Infinity-Infinity,先执行符号操作
  • 对于任何不为零的数与无穷大之间的运算,结果都将为无穷大
  • 当我们对正负无穷大进行加法或除法时,结果会是not a number(NaN)

6. 除以零

除以零是一个特殊的除法情况,因为它会产生正负无穷大的值。

举个例子,我们取一个doubled,然后检查以下除以零的运算结果:

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. 总结

在这篇文章中,我们详细研究了Java中正负无穷大的概念及其用法。实现和代码示例可以在GitHub上找到。