JS中的数字类型变量的最小值和最大值

时间:2022-09-26 21:08:04

I was studying min and max methods in JavaScript and I couldnt find any post that explain these methods algorithms. My guess is that in numerical variable like 1234 you can get min and max by splitting the number in 4 values and compare them. Or add these values in array, sort that array and get min as array[0]. Is there any post related to these methods explained?

我正在研究JavaScript中的min和max方法,我找不到任何解释这些方法算法的帖子。我的猜测是,在像1234这样的数值变量中,您可以通过将数字分成4个值来获得最小值和最大值并进行比较。或者在数组中添加这些值,对该数组进行排序,并将min作为数组[0]。有没有解释过与这些方法有关的帖子?

2 个解决方案

#1


1  

Math.min and Math.max are returning you the minimum and the maximum value respectively from the set of passed arguments.
So that you can just split a string representation of your number to retrieve an array of digits and then apply Math.min or Math.max methods to the result array.

Math.min和Math.max分别从传递的参数集中返回最小值和最大值。这样您就可以拆分数字的字符串表示来检索数字数组,然后将Math.min或Math.max方法应用于结果数组。

(function() {
  var minDigit,
      maxDigit,
      numToDigitsArray;
  
  numToDigitsArray = function(num) {
    return ('' + Math.abs(num)).split('').map(function(digit) {
      return parseInt(digit);
    });
  };
  
  minDigit = function(num) {
    return Math.min.apply(null, numToDigitsArray(num));
  };
  
  maxDigit = function(num) {
    return Math.max.apply(null, numToDigitsArray(num));
  };
  
  console.log(minDigit(-5343240123));
  console.log(maxDigit(753943240123));
})();

#2


1  

In the current source of the v8 JavaScript Engine, /build/v8/src/math.js defines Math.min() and Math.max() thusly:

在v8 JavaScript引擎的当前源代码中,/ build / v8 / src / math.js因此定义了Math.min()和Math.max():

Math.min:

Math.min:

// ECMA 262 - 15.8.2.12
function MathMin(arg1, arg2) {  // length == 2
  var length = %_ArgumentsLength();
  if (length == 2) {
    arg1 = TO_NUMBER_INLINE(arg1);
    arg2 = TO_NUMBER_INLINE(arg2);
    if (arg2 > arg1) return arg1;
    if (arg1 > arg2) return arg2;
    if (arg1 == arg2) {
      // Make sure -0 is considered less than +0.
      return (arg1 === 0 && %_IsMinusZero(arg1)) ? arg1 : arg2;
    }
    // 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);
    if (!IS_NUMBER(n)) n = NonNumberToNumber(n);
    // Make sure -0 is considered less than +0.
    if (NUMBER_IS_NAN(n) || n < r || (r === 0 && n === 0 && %_IsMinusZero(n))) {
      r = n;
    }
  }
  return r;
}

Math.max:

Math.max:

// ECMA 262 - 15.8.2.11
function MathMax(arg1, arg2) {  // length == 2
  var length = %_ArgumentsLength();
  if (length == 2) {
    arg1 = TO_NUMBER_INLINE(arg1);
    arg2 = TO_NUMBER_INLINE(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);
    if (!IS_NUMBER(n)) n = NonNumberToNumber(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;
}

If you use Function.prototype.apply() with Math.min() or Math.max(), you can get the minimum or maximum value for a sequence of numbers.

如果将Function.prototype.apply()与Math.min()或Math.max()一起使用,则可以获得一系列数字的最小值或最大值。

(borrowing from adeneo's comment above because I'm lazy:)

(借用上面的adeneo的评论因为我很懒:)

Math.max.apply(Math, '1234'.split(''));
// returns 4

#1


1  

Math.min and Math.max are returning you the minimum and the maximum value respectively from the set of passed arguments.
So that you can just split a string representation of your number to retrieve an array of digits and then apply Math.min or Math.max methods to the result array.

Math.min和Math.max分别从传递的参数集中返回最小值和最大值。这样您就可以拆分数字的字符串表示来检索数字数组,然后将Math.min或Math.max方法应用于结果数组。

(function() {
  var minDigit,
      maxDigit,
      numToDigitsArray;
  
  numToDigitsArray = function(num) {
    return ('' + Math.abs(num)).split('').map(function(digit) {
      return parseInt(digit);
    });
  };
  
  minDigit = function(num) {
    return Math.min.apply(null, numToDigitsArray(num));
  };
  
  maxDigit = function(num) {
    return Math.max.apply(null, numToDigitsArray(num));
  };
  
  console.log(minDigit(-5343240123));
  console.log(maxDigit(753943240123));
})();

#2


1  

In the current source of the v8 JavaScript Engine, /build/v8/src/math.js defines Math.min() and Math.max() thusly:

在v8 JavaScript引擎的当前源代码中,/ build / v8 / src / math.js因此定义了Math.min()和Math.max():

Math.min:

Math.min:

// ECMA 262 - 15.8.2.12
function MathMin(arg1, arg2) {  // length == 2
  var length = %_ArgumentsLength();
  if (length == 2) {
    arg1 = TO_NUMBER_INLINE(arg1);
    arg2 = TO_NUMBER_INLINE(arg2);
    if (arg2 > arg1) return arg1;
    if (arg1 > arg2) return arg2;
    if (arg1 == arg2) {
      // Make sure -0 is considered less than +0.
      return (arg1 === 0 && %_IsMinusZero(arg1)) ? arg1 : arg2;
    }
    // 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);
    if (!IS_NUMBER(n)) n = NonNumberToNumber(n);
    // Make sure -0 is considered less than +0.
    if (NUMBER_IS_NAN(n) || n < r || (r === 0 && n === 0 && %_IsMinusZero(n))) {
      r = n;
    }
  }
  return r;
}

Math.max:

Math.max:

// ECMA 262 - 15.8.2.11
function MathMax(arg1, arg2) {  // length == 2
  var length = %_ArgumentsLength();
  if (length == 2) {
    arg1 = TO_NUMBER_INLINE(arg1);
    arg2 = TO_NUMBER_INLINE(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);
    if (!IS_NUMBER(n)) n = NonNumberToNumber(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;
}

If you use Function.prototype.apply() with Math.min() or Math.max(), you can get the minimum or maximum value for a sequence of numbers.

如果将Function.prototype.apply()与Math.min()或Math.max()一起使用,则可以获得一系列数字的最小值或最大值。

(borrowing from adeneo's comment above because I'm lazy:)

(借用上面的adeneo的评论因为我很懒:)

Math.max.apply(Math, '1234'.split(''));
// returns 4