这三个特殊浮点值是什么意思:正无穷大、负无穷大、NaN?
我们如何在代码中使用它们,什么会导致 NaN(不是数字)?
How can we use them in our codes, and what will cause NaN(not a number)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我们如何在代码中使用它们,什么会导致 NaN(不是数字)?
How can we use them in our codes, and what will cause NaN(not a number)?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
0/0
。以及
Float< 规范中的常量/code>
类:
Float.NEGATIVE_INFINITY
Float.POSITIVE_INFINITY
Float.NaN
更多信息可以在维基百科中的 IEEE-754 页面。
这是一个小程序来说明这三个常量:
输出:
0/0
.And the constants from the specification of the
Float
class:Float.NEGATIVE_INFINITY
Float.POSITIVE_INFINITY
Float.NaN
More information can be found in the IEEE-754 page in Wikipedia.
Here's a little program to illustrate the three constants:
Output:
如果您想了解有关 Java 中浮点数的更多信息,这可能是一个很好的参考。
正无穷大是一个大到无法正常表示的正数。 负无穷大是一个大到无法正常表示的负数。 NaN 的意思是“不是数字”,是不产生数字的数学运算的结果,例如 0 除以 0。
在 Java 中,Double 和 Float 类都有常量来表示所有三种情况。 它们是 POSITIVE_INFINITY、NEGATIVE_INFINITY 和 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:
Mathematically, everybody can see it is 0. But for the machine, it is an "Infinity" - "Infinity" (of same Rank), which is indeed NaN.
无穷大(在java中)意味着运算结果将是一个非常大的正数或负数,以至于无法正常表示。
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.
这个想法是表示可以从“正常”数字的运算中自然产生的特殊数字。 您可以将无穷大(正数和负数)视为浮点表示的“溢出”,其想法是,至少在某些条件下,函数返回这样的值仍然会给出有意义的结果。 例如,它们仍然具有一些排序属性(例如,它们不会破坏排序操作)。
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.
您可以将它们用作任何其他数字:
例如:
You can use them as any other number:
e.g:
这不是一个完整的答案(或者不够澄清) - 考虑一下:
数学上每个人都可以看到它是 0。但对于机器来说,它是一个“无穷大” - “无穷大”(相同顺序)女巫确实是 NaN...
this is not a complete answer(or not clarified enough) - consider this:
mathematically everybody can see it is 0. but for the machine it is an "Infinity" - "Infinity"(of same order) witch is indeed NaN...