[英]What is NEGATIVE_INFINITY for in Javascript
I got this in a function I found on internet, but I can't understand what it's for.我在互联网上找到的一个函数中得到了这个,但我不明白它的用途。 What I've read on W3school.com.
我在 W3school.com 上读到的内容。 Here is how they explain it:
以下是他们的解释:
Negative infinity can be explained as something that is lower than any other number.
负无穷大可以解释为比任何其他数字都低的东西。
So this is something I can understand but I can't think of a moment where you'd need this kind of constant.所以这是我能理解的,但我想不出你需要这种常数的时刻。 Plus, check this function out:
另外,检查一下这个功能:
function setEqualHeight(selector, triggerContinusly) {
var elements = $(selector)
elements.css("height", "auto")
var max = Number.NEGATIVE_INFINITY;
$.each(elements, function(index, item) {
if ($(item).height() > max) {
max = $(item).height()
}
})
$(selector).css("height", max + "px")
if (!!triggerContinusly) {
$(document).on("input", selector, function() {
setEqualHeight(selector, false)
})
$(window).resize(function() {
setEqualHeight(selector, false)
})
}
}
I understand the whole function.我了解整个功能。 But I can't figure out why would max be
= Number.NEGATIVE_INFINITY
at first.但我无法弄清楚为什么 max 一开始是
= Number.NEGATIVE_INFINITY
。 Then, he check if the height is higher then the smallest possible number?然后,他检查高度是否高于可能的最小数字?
This function works perfectly so I'm guessing it's correct but I really don't understand what is the usage in this function or why would people use this in general.这个函数运行完美,所以我猜它是正确的,但我真的不明白这个函数的用法是什么,或者为什么人们通常会使用它。
I hope you guys will be able to enlighten me !希望各位大侠不吝赐教!
It's common to use extreme values as a starting point for functions finding the maximum in a set of data.使用极值作为函数在一组数据中寻找最大值的起点是很常见的。 In this case, because elements don't have a height less than
0
, it isn't necessary, but provides better understanding once you've seen this concept before.在这种情况下,因为元素的高度不小于
0
,所以没有必要,但是一旦您之前看过这个概念,就可以更好地理解。
Consider the following arbitrary example.考虑以下任意示例。
var data = [ 1, 5, -10000, 50, 1240 ];
function maxData(data) {
var max = Number.NEGATIVE_INFINITY;
for (var i = 0; i < data.length; ++i)
if (data[ i ] > max)
max = data[ i ];
return max;
}
This works great in JavaScript because any value compared to Math.NEGATIVE_INFINITY
will be greater.这在 JavaScript 中效果很好,因为与
Math.NEGATIVE_INFINITY
相比的任何值都会更大。
In other languages like C++, it's common to use the minimum value of an integer, float, etc.在 C++ 等其他语言中,通常使用整数、浮点数等的最小值。
Negative Infinity is not a Rocket Science.负无限不是火箭科学。 Its a reverse of positive infinity.
它是正无穷大的反转。
var i=-3/0;
Ans= -Infinity
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.