admin管理员组文章数量:1278860
Why is the Infinity property used as a mand (rather than a result)
For example, this code below works, but the result isn't what I expected.
alert(isOdd(Infinity));
function isOdd(num) { return num%2==1; }
Why is the Infinity property used as a mand (rather than a result)
For example, this code below works, but the result isn't what I expected.
alert(isOdd(Infinity));
function isOdd(num) { return num%2==1; }
Share
Improve this question
edited Feb 25, 2019 at 22:04
Zze
18.8k14 gold badges94 silver badges125 bronze badges
asked May 8, 2013 at 23:47
inaina
19.5k41 gold badges127 silver badges208 bronze badges
12
- Can you explain a bit more? Provide an example. – user2398029 Commented May 8, 2013 at 23:54
-
1
There is no
infinity
keyword in Java. There are some constants to simulate infinity, but they aren't keywords or reserved words – RustyTheBoyRobot Commented May 8, 2013 at 23:57 - @louism example provided – ina Commented May 9, 2013 at 0:03
- 1 This isn't a specific programming question. – Alan Commented May 9, 2013 at 0:05
- 1 From MDN: "The value Infinity (positive infinity) is greater than any other number including itself. This value behaves mathematically like infinity; for example, anything multiplied by Infinity is Infinity, and anything divided by Infinity is 0." – apsillers Commented May 9, 2013 at 0:17
2 Answers
Reset to default 9MDN REFERENCE
Infinity is a property of the global object, i.e. it is a variable in global scope.
The initial value of Infinity is Number.POSITIVE_INFINITY. The value Infinity (positive infinity) is greater than any other number. This value behaves mathematically like infinity; for example, any positive number multiplied by Infinity is Infinity, and anything divided by Infinity is 0.
First what does this mean? In essence infinity is a concept not an actual value. Mathematics is based on concepts not values. For instance a number isn't a value, a numeral is.
The number 7 is the concept of the intended value, Romans wrote it as VII, in standard form (BASE-10) you write it as 7. In binary(BASE-2) you write it as 111. If you want to use a triangle or any other mark that is fine also as long as the concept is applied correctly.
Now that you know that, Infinity is simply the concept of being greater than any other number. It holds no value. The only reason that the basic concept of an infinity loops means to run forever is because in concept it means that whatever numeral iteration of that loop you are in (whether 1 or a million) infinity will always be greater than that number.
There are many methods to applying concepts in coding which is why everyone's code is ran differently but for example:
SAMPLE TAKEN FROM w3schools:
function findMax(x) {
var i;
var max = -Infinity;
for(i = 0; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
}
document.getElementById("demo").innerHTML = findMax(1, 123, 500, 115, 44, 88);
In the site's example they pass the argument of 6 values to the function findMax findMax(1, 123, 500, 115, 44, 88);
They are then using a loop to stop at the parameters length. In the loop they are reassigning the max value from the concept of infinity to a value and if greater than that value when looped again the max value is then changed to the new high value.
Why is this important? Because in the example they use the concept of negative infinity which is simply the values of infinity decremented negatively. One could easily argue that 0 could replace -Infinity but they'd be wrong. This is why.
What if your value range is dependent upon negative values also being passed in the formula above? What if all you have is negative values that were dynamically captured from user input or another function?
Consider findMax was findMax(-1, -10, -15, -20);
0 would give a false output that it was the correct max value which wouldn't be what you wanted. You'd want -1 one to be the output. There are other methods to achieving the solution but for the sake of Infinity concept discussion I will end here.
I hope this sheds more light on the process of Infinity concept.
Infinity
is a property of the global object that holds a numeric value representing the mathematical concept of infinity. I don't know any normal definition by which it could be called a "mand."
With regard to your edit, that should return false (I ran it to confirm this suspicion, and it did on my browser). This is correct, as infinity is not normally considered an odd number.
本文标签: What is the Infinity property used for in JavascriptStack Overflow
版权声明:本文标题:What is the Infinity property used for in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741252287a2366016.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论