Javascript Math.max和Math.min如何实际工作?

时间:2015-11-22 05:07:51

标签: javascript math

我真的很好奇这些功能是如何运作的?我知道有很多关于如何使用它们的问题,我已经知道如何使用它们,但我无法在任何地方找到如何实际在数组上实现此功能,例如,如果没有这样的功能?如果没有帮助者,你会如何编写这样的函数?

6 个答案:

答案 0 :(得分:5)

这是Chrome V8引擎中的Math.max代码。

function MathMax(arg1, arg2) {  // length == 2
  var length = %_ArgumentsLength();
  if (length == 2) {
    arg1 = TO_NUMBER(arg1);
    arg2 = TO_NUMBER(arg2);
    if (arg2 > arg1) return arg2;
    if (arg1 > arg2) return arg1;
    if (arg1 == arg2) {
      // Make sure -0 is considered less than +0.
      return (arg1 === 0 && %_IsMinusZero(arg1)) ? arg2 : arg1;
    }
    // All comparisons failed, one of the arguments must be NaN.
    return NaN;
  }
  var r = -INFINITY;
  for (var i = 0; i < length; i++) {
    var n = %_Arguments(i);
    n = TO_NUMBER(n);
    // Make sure +0 is considered greater than -0.
    if (NUMBER_IS_NAN(n) || n > r || (r === 0 && n === 0 && %_IsMinusZero(r))) {
      r = n;
    }
  }
  return r;
}

Here是存储库。

答案 1 :(得分:3)

让我们来看看规范(可以/应该帮助您实现!)

ECMAScript 1st Edition (ECMA-262)(Math.max / min的初始定义)中,我们看到以下内容:

function(){}.bind(this);

规范的后续版本为我们提供了:

ECMAScript 5.1

15.8.2.11 max(x, y)
Returns the larger of the two arguments.
    • If either argument is NaN, the result is NaN.
    • If x>y, the result is x.
    • If y>x, the result is y.
    • If x is +0 and y is +0, the result is +0.
    • If x is +0 and y is −0, the result is +0.
    • If x is −0 and y is +0, the result is +0.
    • If x is −0 and y is −0, the result is −0.

15.8.2.12 min(x, y)
    Returns the smaller of the two arguments.
    • If either argument is NaN, the result is NaN.
    • If x<y, the result is x.
    • If y<x, the result is y.
    • If x is +0 and y is +0, the result is +0.
    • If x is +0 and y is −0, the result is −0.
    • If x is −0 and y is +0, the result is −0.
    • If x is −0 and y is −0, the result is −0.

可以在此处找到对11.8.5的引用:The Abstract Relational Comparison Algorithm

ECMAScript 2015

15.8.2.11 max ( [ value1 [ , value2 [ , … ] ] ] )

    Given zero or more arguments, calls ToNumber on each of the arguments and returns the largest of the resulting values.

    • If no arguments are given, the result is −∞.
    • If any value is NaN, the result is NaN.
    • The comparison of values to determine the largest value is done as in 11.8.5 except that +0 is considered to be larger than −0.

    The length property of the max method is 2.

15.8.2.12 min ( [ value1 [ , value2 [ , … ] ] ] )

    Given zero or more arguments, calls ToNumber on each of the arguments and returns the smallest of the resulting values.

    • If no arguments are given, the result is +∞.
    • If any value is NaN, the result is NaN.
    • The comparison of values to determine the smallest value is done as in 11.8.5 except that +0 is considered to be larger than −0.

    The length property of the min method is 2.

同样,7.2.11可以在这里找到:Abstract Relational Comparison

答案 2 :(得分:2)

以下是如果Math.min()Math.max() 存在的话,如何实现这些功能。

函数有一个 arguments 对象,您可以迭代它以获取其值。

重要的是要注意,没有参数的 Math.min() 会返回Infinity,而 Math.max() 则不会返回任何参数返回-Infinity

&#13;
&#13;
function min() {
  var result= Infinity;
  for(var i in arguments) {
    if(arguments[i] < result) {
      result = arguments[i];
    }
  }
  return result;
}

function max() {
  var result= -Infinity;
  for(var i in arguments) {
    if(arguments[i] > result) {
      result = arguments[i];
    }
  }
  return result;
}

//Tests
console.log(min(5,3,-2,4,14));       //-2
console.log(Math.min(5,3,-2,4,14));  //-2

console.log(max(5,3,-2,4,14));       //14
console.log(Math.max(5,3,-2,4,14));  //14

console.log(min());                  //Infinity
console.log(Math.min());             //Infinity

console.log(max());                  //-Infinity
console.log(Math.max());             //-Infinity
&#13;
&#13;
&#13;

答案 3 :(得分:1)

基本功能:

Math.max()和Math.min()用于数字(或它们可以强制转换为数字),不能直接将数组作为参数传递。

例如:

Math.max(1,52,28)

您可以使用逗号分隔数字。

<强>阵列

此示例显示了如何将它们应用于数组:

JavaScript: min & max Array values?

基本以下作品:

Math.max.apply(null, [1,5,2,3]);

为何有效?

这是有效的,因为apply是一个函数,所有函数都使用了一个带有数组参数的函数。

Math.max.apply(null,[1,5,2,3])与Math.max(1,5,2,3)相同

答案 4 :(得分:1)

好吧,这里min没有Math.min(代码在ES6中)。

function min() {
  return Array.from(arguments).reduce(
    (minSoFar, next) => minSoFar < next ? minSoFar : next
  , Infinity)
}

可以使用简单的循环实现相同的逻辑。您只需要通过迭代跟踪变量中的一个变量,这是您到目前为止看到的最低值。 minSoFar的初始值为Infinity。原因是除了Infinity之外的任何数字小于Infinity,但是在发送 no 参数的情况下,我们想要返回{{ 1}}本身,因为那些没有参数的Infinity评估为。

Math.min()

function min() { let minSoFar = Infinity for(let i = 0, l = arguments.length; i < l; i++) { const next = arguments[i] minSoFar = minSoFar < next ? minSoFar : next } return minSoFar } 可以使用几乎相同的逻辑来实现,只有您能够跟踪到目前为止看到的最高值,并且初始值为Max

答案 5 :(得分:0)

使用Array.prototype.reduce

可以轻松实现
function min() {
   var args = Array.prototype.slice.call(arguments);

   var minValue = args.reduce(function(currentMin, nextNum) {
      if (nextNum < currentMin) {
         // nextNum is less than currentMin, so we return num
         // which will replace currentMin with nextNum
         return nextNum;
      }
      else {
         return currentMin;
      }
   }, Infinity);

   return minValue;
}
相关问题