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

发布于 2024-07-24 01:24:03 字数 36 浏览 7 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

甲如呢乙后呢 2024-07-31 01:24:03
  • 正无穷大意味着在正方向上趋向无穷大——在正方向上数值越来越大。
  • 负无穷大意味着在负方向上趋向无穷大——在负方向上进入幅度越来越大的值。
  • Not-a-number (NaN) 是未定义的内容,例如 0/0

以及 Float< 规范中的常量/code>类:

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

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

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

输出:

NaN
Infinity
-Infinity
  • 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.

And the constants from the specification of the Float class:

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

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
花开雨落又逢春i 2024-07-31 01:24:03

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

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

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

另外考虑一下:

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

从数学上来说,每个人都可以看到它是 0。但对于机器来说,它是一个“Infinity” - “Infinity”(相同的 Rank),这确实是 NaN。

This may be a good reference if you want to learn more about floating point numbers in 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.

In Java, the Double and Float classes both have constants to represent all three cases. They are POSITIVE_INFINITY, NEGATIVE_INFINITY, and 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.

征﹌骨岁月お 2024-07-31 01:24:03
  • 1/0 将导致正无穷大。
  • 0/0 将导致 Nan。 您可以将 NaN 用作任何其他数字,例如:NaN+NaN=NaN,NaN+2.0=NaN
  • -1/0 将导致负无穷大。

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

  • 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.

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.

萌面超妹 2024-07-31 01:24:03

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

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

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.

我也只是我 2024-07-31 01:24:03

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

例如:

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

You can use them as any other number:

e.g:

float min = Float.NEGATIVE_INFINITY;
float max = Float.POSITIVE_INFINITY;
float nan = Float.NaN;
┾廆蒐ゝ 2024-07-31 01:24:03

正无穷大是一个大到不可能的正数
代表正常。 负无穷大是一个如此大的负数
无法正常表示。 NaN 表示“不是数字”并且
不产生数字的数学运算的结果-
就像用 0 除以 0 一样。

这不是一个完整的答案(或者不够澄清) - 考虑一下:

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

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文