Infinity is some number in javascript?

谁说胖子不能爱 提交于 2019-12-18 05:54:54

问题


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 simply Infinity) 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!