javascript 无穷大是javascript中的某个数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24166320/
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
Infinity is some number in javascript?
提问by nicael
alert(1/0)
alerts Infinity
and alert(1/-0)
alerts -Infinity
. alert(-1/-0)
alerts Infinity
, as I could expect when doing some operations with realnumbers. I can't say that infinity is a measurable value. Does javascript think it is some number?
alert(1/0)
警报Infinity
和alert(1/-0)
警报-Infinity
。alert(-1/-0)
alerts Infinity
,正如我在使用实数进行一些操作时所期望的那样。我不能说无穷大是一个可测量的值。javascript 认为这是一个数字吗?
回答by James Allardice
Yes, Infinity
and -Infinity
are special values of the Number type. From the ES5 spec:
是的,Infinity
并且-Infinity
是Number 类型的特殊值。从 ES5 规范:
There are two other special values, called positive Infinity and negative Infinity. For brevity, these values are also referred to for expository purposes by the symbols +∞ and ?∞, respectively. (Note that these two infinite Number values are produced by the program expressions
+Infinity
(or simplyInfinity
) and-Infinity
.)
还有另外两个特殊值,称为正无穷大和负无穷大。为简洁起见,出于说明目的,这些值也分别用符号 +∞ 和 ?∞ 表示。(请注意,这两个无限 Number 值是由程序表达式
+Infinity
(或简称Infinity
)和 产生的-Infinity
。)
Also note that NaN
is a value of the Number type too, despite it being an acronym for "not a number".
另请注意NaN
,尽管它是“非数字”的首字母缩写词,但它也是 Number 类型的值。
回答by maerics
JavaScript uses IEEE-754 to represent numerical types; that specification includes values for non-numberssuch as +/-Infinity and "NaN".
JavaScript 使用IEEE-754 来表示数字类型;该规范包括非数字的值,例如 +/-Infinity 和“NaN”。
(1/0) // => Infinity
typeof(Infinity) // => "number"
Number.POSITIVE_INFINITY === Infinity // => true
Number.NEGATIVE_INFINITY === -Infinity // => true
Arithmetic and logical operations including the infinite values should behave as expected.
包括无限值在内的算术和逻辑运算应按预期运行。
回答by Tom
From the Mozilla docs:
来自 Mozilla 文档:
The initial value of Infinity is Number.POSITIVE_INFINITY. The value Infinity (positive infinity) is greater than any other number including itself. This value behaves mathematically like infinity; for example, any positive number multiplied by Infinity is Infinity, and anything divided by Infinity is 0.
Infinity 的初始值为 Number.POSITIVE_INFINITY。值 Infinity(正无穷大)大于任何其他数字,包括它自己。这个值在数学上表现得像无穷大;例如,任何正数乘以无穷大都是无穷大,任何除以无穷大都是 0。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Infinity
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Infinity