为什么整数在Java中不代表NaN ?

时间:2022-09-06 08:12:29

When I write something like

当我写类似的东西时

double a = 0.0;
double b = 0.0;
double c = a/b;

The result is Double.NaN, but when I try the same for integers, it produces an ArithmeticException. So, why isn't there a Integer.NaN?

结果是两倍。NaN,但是当我对整数进行同样的尝试时,它会产生一个算术异常。那么,为什么没有一个整数呢?

3 个解决方案

#1


8  

The answer has very little to do with Java. Infinity or undefined numbers are not a part of the integer set, so they are excluded from Integer, whereas floating point types represent real numbers as well as complex numbers, so to deal with these, NaN has been included with floating point types.

答案与Java没什么关系。无穷大或未定义的数字不是整数集的一部分,因此它们被排除在整数之外,而浮点类型表示实数和复数,所以为了处理这些,NaN被包含在浮点类型中。

#2


4  

For the same reason that there is no integer NaN in any other language.

出于同样的原因,在任何其他语言中都没有整型NaN。

Modern computers use 2's complement binary representation for integers, and that representation doesn't have a NaN value. (All values in the domain of the representation type represent definite integers.)

现代计算机对整数使用2的补码二进制表示,而那个表示没有NaN值。(表示类型域中的所有值都表示定整数。)

It follows that computer integer arithmetic hardware does not recognize any NaN representation.

因此,计算机整数算法硬件不能识别任何NaN表示。

In theory, someone could invent an alternative representation for integers that includes NaN (or INF, or some other exotic value). However, arithmetic using such a representation would not be supported by the hardware. While it would be possible to implement it in software, it would be prohibitively expensive1... and undesirable in other respects too to include this support in the Java language.

在理论上,有人可以为包含NaN(或INF,或其他奇异值)的整数发明一种替代表示。但是,硬件不支持使用这种表示的算术。虽然在软件中实现它是可能的,但这将是非常昂贵的……在其他方面也不希望在Java语言中包含这种支持。

1 - It is of course relative, but I'd anticipate that a software implementation of NaNs would be (at least) an order of magnitude slower than hardware. If you actually, really, needed this, then that would be acceptable. But the vast majority of integer arithmetic codes don't need this. In most cases throwing an exception for "divide by zero" is just fine, and an order of magnitude slow down in all integer arithmetic operations is ... not acceptable.

1 -它当然是相对的,但是我预计NaNs的软件实现将(至少)比硬件慢一个数量级。如果你真的需要这个,那是可以接受的。但是绝大多数的整数算术代码不需要这个。在大多数情况下,为“除零”抛出异常是可以的,并且在所有整数算术运算中,一个数量级的慢下来是……不能接受的。


By contrast:

相比之下:

  • the "unused" values in the representation space already exist
  • 表示空间中的“未使用”值已经存在
  • NaN and INF values are part of the IEE floating point standard, and
  • NaN和INF值是IEE浮点标准的一部分
  • they are (typically) implemented by the native hardware implementation of floating point arithmetic
  • 它们(通常)是由浮点运算的本地硬件实现实现的。

#3


0  

As noted in other comments, it's largely because NaN is a standard value for floating point numbers. You can read about the reasons NaN would be returned on Wikipedia here:

正如其他评论所指出的,这主要是因为NaN是浮点数的标准值。你可以在*上看到NaN被返回的原因:

http://en.wikipedia.org/wiki/NaN

http://en.wikipedia.org/wiki/NaN

Notice that only one of these reasons exists for integer numbers (divide by zero). There is also both a positive and a negative infinity value for floating point numbers that integers don't have and is closely linked to NaN in the floating point specification.

注意,这些原因中只有一个存在于整数(除以0)。浮点数也有一个正的和一个负的无穷大值,这是整数没有的,并且在浮点数规范中与NaN密切相关。

#1


8  

The answer has very little to do with Java. Infinity or undefined numbers are not a part of the integer set, so they are excluded from Integer, whereas floating point types represent real numbers as well as complex numbers, so to deal with these, NaN has been included with floating point types.

答案与Java没什么关系。无穷大或未定义的数字不是整数集的一部分,因此它们被排除在整数之外,而浮点类型表示实数和复数,所以为了处理这些,NaN被包含在浮点类型中。

#2


4  

For the same reason that there is no integer NaN in any other language.

出于同样的原因,在任何其他语言中都没有整型NaN。

Modern computers use 2's complement binary representation for integers, and that representation doesn't have a NaN value. (All values in the domain of the representation type represent definite integers.)

现代计算机对整数使用2的补码二进制表示,而那个表示没有NaN值。(表示类型域中的所有值都表示定整数。)

It follows that computer integer arithmetic hardware does not recognize any NaN representation.

因此,计算机整数算法硬件不能识别任何NaN表示。

In theory, someone could invent an alternative representation for integers that includes NaN (or INF, or some other exotic value). However, arithmetic using such a representation would not be supported by the hardware. While it would be possible to implement it in software, it would be prohibitively expensive1... and undesirable in other respects too to include this support in the Java language.

在理论上,有人可以为包含NaN(或INF,或其他奇异值)的整数发明一种替代表示。但是,硬件不支持使用这种表示的算术。虽然在软件中实现它是可能的,但这将是非常昂贵的……在其他方面也不希望在Java语言中包含这种支持。

1 - It is of course relative, but I'd anticipate that a software implementation of NaNs would be (at least) an order of magnitude slower than hardware. If you actually, really, needed this, then that would be acceptable. But the vast majority of integer arithmetic codes don't need this. In most cases throwing an exception for "divide by zero" is just fine, and an order of magnitude slow down in all integer arithmetic operations is ... not acceptable.

1 -它当然是相对的,但是我预计NaNs的软件实现将(至少)比硬件慢一个数量级。如果你真的需要这个,那是可以接受的。但是绝大多数的整数算术代码不需要这个。在大多数情况下,为“除零”抛出异常是可以的,并且在所有整数算术运算中,一个数量级的慢下来是……不能接受的。


By contrast:

相比之下:

  • the "unused" values in the representation space already exist
  • 表示空间中的“未使用”值已经存在
  • NaN and INF values are part of the IEE floating point standard, and
  • NaN和INF值是IEE浮点标准的一部分
  • they are (typically) implemented by the native hardware implementation of floating point arithmetic
  • 它们(通常)是由浮点运算的本地硬件实现实现的。

#3


0  

As noted in other comments, it's largely because NaN is a standard value for floating point numbers. You can read about the reasons NaN would be returned on Wikipedia here:

正如其他评论所指出的,这主要是因为NaN是浮点数的标准值。你可以在*上看到NaN被返回的原因:

http://en.wikipedia.org/wiki/NaN

http://en.wikipedia.org/wiki/NaN

Notice that only one of these reasons exists for integer numbers (divide by zero). There is also both a positive and a negative infinity value for floating point numbers that integers don't have and is closely linked to NaN in the floating point specification.

注意,这些原因中只有一个存在于整数(除以0)。浮点数也有一个正的和一个负的无穷大值,这是整数没有的,并且在浮点数规范中与NaN密切相关。