When I type in an array into the parameter of the javascript math minimum and maximum functions, it returns the correct value:
console.log( Math.min( 5 ) );
It's the same reason why the sum of an empty list is usually defined as 0 and their product as 1: it is the identity element of the operation. That is, whenever you include -Infinity as an element when computing max, it does not affect the result; respectively for Infinity and min.
This is sensible because it allows desirable "associative" properties for the aggregate operations. For example, the sum of a list is the same as computing the sums of any sublists (maybe including empty) and summing them. Likewise for products, mins, maxes and so on.
It's tricky, but important, to decide correctly what aggregate functions should do when passed the empty set.
Sometimes it's 'intuitively obvious': What is the SUM of no elements? Zero, I'm sure everyone would readily say.
Sometimes it's less so: What is the PRODUCT of no elements? Those with some mathematical training will quickly say "one", but this is not at all obvious.
Then you get to MIN and MAX and wow! How did we get those infinities?
One way to decide what an aggregate function should do here is consider what behaviours we want to remain consistent, even with empty sets. For example, suppose we have these non-empty sets:
A = { 1, 2, 3 }
B = { 4, 5 }
Now, it's true here, and indeed for any non-empty sets, that
SUM(A ∪ B) = SUM({SUM(A), SUM(B)})
15 = 6 + 9
PRODUCT(A ∪ B) = PRODUCT({ PRODUCT(A), PRODUCT(B) })
120 = 6 * 20
MIN(A ∪ B) = MIN({ MIN(A), MIN(B) })
1 = MIN(1, 4)
Wouldn't it be nice, say the mathematicians, if these properties remain true even when one or both of the sets are empty? It surely would.
And it's maintaining this behaviour that decides what value we assign to SOME_AGGREGATE_FUNCTION(∅)
:
In order for
SUM(A ∪ B) = SUM({ SUM(A), SUM(B) })
to remain true when A
is empty and B
is not, we must have SUM(∅) = 0
In order for
PRODUCT(A ∪ B) = PRODUCT({ PRODUCT(A), PRODUCT(B) })
to remain true when A
is empty and B
is not, we must have PRODUCT(∅) = 1
And finally:
In order for
MIN(A ∪ B) = MIN({ MIN(A), MIN(B) })
to remain true when A
is empty and B
is not, we need MIN(∅)
to be a value which is guaranteed to be greater than any possible value in B, so that it doesn't 'interfere with' the result of MIN(B)
. And we get our answer: MIN(∅) = +∞
Probably because the implementation initialises an internal comparison variable to the highest (for Math.min) or lowest (for Math.max), before starting to compare against the empty arrays, and then returns the value of this internal comparison variable which of course has not been changed.
I don't know for sure. But, just making a guess.
Remember how we find the min. Declare a variable with an extremely high value (Infinity) and then go through the values and whenever you find a value which is less than the one stored in your variable, you store it instead as the new min.
So, since you are not giving it any values to find the min for, it gives you the initial value i.e. Infinity.
Same for max.
The idea is, mathematically without any parameter you actually have an undefined value for the minimum.
As for implementation wise, usually the min value is initialized with a very large value(Infinity), which is then updated as smaller values are found. If no value is found, then you have Infinity
as the min value.
The case is opposite with finding the maximum value and that is why you have -Infinity
.
Of course it would, because the start number should be Infinity
for Math.min
. All number that are lower than positive infinity should be the smallest from a list, if there are no smaller.
And for Math.max
it's the same; all numbers that are larger than negative infinity should be the biggest if there are no bigger.
So for your first example:
Math.min(5)
where 5
is smaller than positive infinity (Infinity
) it will return 5
.
Calling Math.min()
and Math.max
with an array parameter may not work on every platform. You should do the following instead:
Math.min.apply(null, [ 1, 2, 3, 4 , 5 ]);
Where the first parameter is the scope argument. Because Math.min()
and Math.max()
are "static" functions, we should set the scope argument to null.