java 这三个特殊的浮点值是什么意思:正无穷大、负无穷大、NaN?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1007586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:47:00  来源:igfitidea点击:

What do these three special floating-point values mean: positive infinity, negative infinity, NaN?

javafloating-pointnaninfinity

提问by Johanna

How can we use them in our codes, and what will cause NaN(not a number)?

我们如何在我们的代码中使用它们,什么会导致 NaN(不是数字)?

回答by coobird

  • Positive infinity means going to infinity in the positive direction -- going into values that are larger and larger in magnitude in the positive direction.
  • Negative infinity means going to infinity in the negative direction -- going into values that are larger and larger in magnitude in the negative direction.
  • Not-a-number(NaN) is something that is undefined, such as the result of 0/0.
  • 正无穷大意味着在正方向上趋于无穷——在正方向上进入越来越大的值。
  • 负无穷大意味着在负方向上趋于无穷——在负方向上进入越来越大的值。
  • 非数字(NaN) 是未定义的东西,例如0/0.

And the constants from the specification of the Floatclass:

以及Float类规范中的常量:

More information can be found in the IEEE-754 page in Wikipedia.

更多信息可以在维基百科IEEE-754 页面中找到。

Here's a little program to illustrate the three constants:

这是一个小程序来说明这三个常量:

System.out.println(0f / 0f);
System.out.println(1f / 0f);
System.out.println(-1f / 0f);

Output:

输出:

NaN
Infinity
-Infinity

回答by Cameron

Thismay be a good reference if you want to learn more about floating point numbers in Java.

如果您想了解有关 Java 中浮点数的更多信息,可能是一个很好的参考。

Positive Infinity is a positive number so large that it can't be represented normally. Negative Infinity is a negative number so large that it cannot be represented normally. NaN means "Not a Number" and results from a mathematical operation that doesn't yield a number- like dividing 0 by 0.

正无穷大是一个大到无法正常表示的正数。Negative Infinity 是一个大到无法正常表示的负数。NaN 的意思是“不是数字”,它是由不产生数字的数学运算得出的,例如 0 除以 0。

In Java, the Double and Float classes both have constants to represent all three cases. They are POSITIVE_INFINITY, NEGATIVE_INFINITY, and NaN.

在 Java 中,Double 和 Float 类都有代表所有三种情况的常量。它们是 POSITIVE_INFINITY、NEGATIVE_INFINITY 和 NaN。

Plus consider this:

另外考虑一下:

double a = Math.pow(10, 600) - Math.pow(10, 600); //==NaN

Mathematically, everybody can see it is 0. But for the machine, it is an "Infinity" - "Infinity" (of same Rank), which is indeed NaN.

数学上大家都能看出来它是0。但对于机器来说,它是一个“Infinity”——“Infinity”(同等级),确实是NaN。

回答by kadam

  • 1/0 will result in positive infinity.
  • 0/0 will result in Nan. You can use NaN as any other number, eg: NaN+NaN=NaN, NaN+2.0=NaN
  • -1/0 will result in negative infinity.
  • 1/0 将导致正无穷大。
  • 0/0 将导致南。您可以将 NaN 用作任何其他数字,例如:NaN+NaN=NaN, NaN+2.0=NaN
  • -1/0 将导致负无穷大。

Infinity (in java) means that the result of an operation will be such an extremely large positive or negative number that it cannot be represented normally.

Infinity(在java中)意味着运算的结果将是一个非常大的正数或负数,以至于无法正常表示。

回答by David Cournapeau

The idea is to represent special numbers which can arise naturally from operations on "normal" numbers. You could see infinity (both positive and negative) as "overflow" of the floating point representation, the idea being that in at least some conditions, having such a value returned by a function still gives meaningful result. They still have some ordering properties, for example (so they won't screw sorting operations, for example).

这个想法是表示可以从“正常”数字的操作中自然产生的特殊数字。您可以将无穷大(正数和负数)视为浮点表示的“溢出”,其想法是至少在某些情况下,函数返回这样的值仍然会给出有意义的结果。例如,它们仍然具有一些排序属性(因此它们不会破坏排序操作)。

Nan is very particular: if x is Nan, x == x is false (that's actually one way to test for nan, at least in C, again). This can be quite confusing if you are not used to floating point peculiarities. Unless you do scientific computation, I would say that having Nan returned by an operation is a bug, at least in most cases that come to mind. Nan can come for various operations: 0/0, inf - inf, inf/inf, 0 * inf. Nan does not have any ordering property, either.

Nan 非常特别:如果 x 是 Nan,则 x == x 是假的(这实际上是一种测试 nan 的方法,至少在 C 中也是如此)。如果您不习惯浮点特性,这可能会令人困惑。除非您进行科学计算,否则我会说操作返回 Nan 是一个错误,至少在大多数情况下会想到。Nan 可以进行各种操作:0/0、inf - inf、inf/inf、0 * inf。Nan 也没有任何排序属性。

回答by Freddy

You can use them as any other number:

您可以将它们用作任何其他数字:

e.g:

例如:

float min = Float.NEGATIVE_INFINITY;
float max = Float.POSITIVE_INFINITY;
float nan = Float.NaN;

回答by Alex

Positive Infinity is a positive number so large that it can't be represented normally. Negative Infinity is a negative number so large that it cannot be represented normally. NaN means "Not a Number" and results from a mathematical operation that doesn't yield a number- like dividing 0 by 0.

正无穷大是一个大到无法正常表示的正数。Negative Infinity 是一个大到无法正常表示的负数。NaN 的意思是“不是数字”,它是由不产生数字数学运算得出的——比如将 0 除以 0。

this is not a complete answer(or not clarified enough) - consider this:

这不是一个完整的答案(或不够明确) - 考虑一下:

double a = Math.pow(10,600) - Math.pow(10,600); //==NaN

mathematically everybody can see it is 0. but for the machine it is an "Infinity" - "Infinity"(of same order) witch is indeed NaN...

从数学上讲,每个人都可以看到它是 0。但对于机器来说,它是一个“无穷大”-“无穷大”(相同顺序)女巫确实是 NaN ......