Javascript中使用的Infinity属性是什么? [英] What is the Infinity property used for in Javascript?

查看:93
本文介绍了Javascript中使用的Infinity属性是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Infinity属性用作命令(而不是结果)



例如,下面的代码有效,但结果不符合我的预期。



  alert(isOdd(Infinity)); function isOdd(num){ return num%2 == 1; }  

解决方案

MDN参考


Infinity是全局对象的属性,即它是
全局范围内的变量。



Infinity的初始值是Number.POSITIVE_INFINITY。值
无穷大(正无穷大)大于任何其他数字。这个
值在数学上表现得像无穷大;例如,任何正数
数乘以无穷大都是无穷大,任何除以
无穷大的数字都是0.


首先是什么意思?本质上,无限是一个概念,而不是一个实际价值。数学基于概念而非价值。例如,数字不是值,数字是。



数字7是预期值的概念,罗马人以标准形式将其写成VII( BASE-10)你把它写成7.在二进制(BASE-2)中你把它写成111.如果你想要使用三角形或任何其他标记,只要正确应用这个概念。



现在您知道,Infinity只是比任何其他数字更大的概念。 它没有任何价值。无限循环的基本概念意味着永远运行的唯一原因是因为在概念上它意味着你所处的那个循环的数字迭代(无论是1或100万)无穷大总是大于那个数字。



在编码中应用概念有很多种方法,这就是为什么每个人的代码运行方式不同但是例如:



从w3schools采取的样本:

  function findMax(x){
var i;
var max = -Infinity;
for(i = 0; i< arguments.length; i ++){
if(arguments [i]> max){
max = arguments [i];
}
}
返回最大值;
}
document.getElementById(demo)。innerHTML = findMax(1,123,500,115,44,88);

在网站示例中,他们将6个值的参数传递给函数findMax
findMax( 1,112,500,115,44,88);



然后他们使用循环来停止参数长度。在循环中,它们将最大值从无穷大概念重新分配给一个值,如果再次循环时大于该值,则最大值将更改为新的高值。



为什么这很重要?因为在这个例子中他们使用负无穷大的概念,这只是无穷大的负值递减。人们可以轻易地争辩说0可以取代 - 无穷大,但它们是错的。这就是原因。



如果你的价值范围依赖于上面公式中传递的负值怎么办?如果您拥有的是从用户输入或其他功能动态捕获的负值,该怎么办?



考虑findMax是findMax(-1,-10,-15,-20);



0会给出一个错误的输出,它是正确的最大值,这不是你想要的。你想要-1一个作为输出。还有其他方法可以实现这个解决方案但是为了Infinity概念的讨论,我将在这里结束。



我希望这能更好地阐明Infinity概念的过程。 / p>

Why is the Infinity property used as a command (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; }

解决方案

MDN 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.

这篇关于Javascript中使用的Infinity属性是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆