问题
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 real numbers. I can't say that infinity is a measurable value. Does javascript think it is some number?
回答1:
Yes, Infinity
and -Infinity
are special values of the Number type. From the ES5 spec:
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
.)
Also note that NaN
is a value of the Number type too, despite it being an acronym for "not a number".
回答2:
JavaScript uses IEEE-754 to represent numerical types; that specification includes values for non-numbers such as +/-Infinity and "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.
回答3:
From the Mozilla docs:
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.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Infinity
来源:https://stackoverflow.com/questions/24166320/infinity-is-some-number-in-javascript