DEV Community

Cover image for Math.min returns Infinity?

Math.min returns Infinity?

Sung M. Kim on June 23, 2019

Preface I was playing around with "Math.min/max" and the result of empty calls looked like it was backwards. Question Shou...
Collapse
 
minimumviableperson profile image
Nicolas Marcora

I agree. Given no arguments, the maximum number should be -Infinity, since that is the max of nothing.

Your example is good, but I would push it even further than 3 to make a case.

Math.max(-100000000) should still return -100000000 as the max, meaning that, no matter how small, or negative, a number is, it should still be allowed the possibility of it being the highest number. Comparing it against -Infinity allows for that.

Collapse
 
dance2die profile image
Sung M. Kim

Thanks Nicolas.

That makes total sense with really low negative number as no matter how small a number is, it is bigger than -Infinity

Collapse
 
gizzzzmo profile image
Gizzzzmo

You can also think of it algebraically, in that all numbers form a monoid under max and min.
The operation is associative, and there exists an identity, namely -infinity for max and infinity for min.
And if you define max/min with arbitrarily many arguments inductively, then the empty operation has to return the identity.

Collapse
 
wwbrannon profile image
William Brannon

It's very not intuitive but I think they're following the usual mathematical convention. The max of no elements is odd to think about, but the supremum of the empty set is just its least upper bound. Because everything is an upper (and lower) bound of nothing, the least such value is -Infinity. Similarly the greatest lower bound, or infimum, or min, of the empty set is +Infinity.

See also this StackExchange discussion.

Collapse
 
dance2die profile image
Sung M. Kim

Thank you, William.

As I was reading John's message, I ran into a concept called, monoid.
It seems like monoid stems from the "mathemtical convention" as you pointed out.

Collapse
 
mjvmroz profile image
Michael Mroz

monoid is the first of many steps down the road that leads to functional programming. Join us.

Thread Thread
 
dance2die profile image
Sung M. Kim

🙂🤝

Collapse
 
kennethlum profile image
Kenneth Lum

I think one reason is

min(a, b, c) = min([], min(a, b, c)) = min(a, min(b, c)) = min(a, b, min(c)) = min(a, b, c, min([]))

that is, you call pull an element out and do a min and the final result is the same. You can pull the nothing out, or you can pull all out. The final form will make min([]) to be Infinity, or else say if it is -Infinity, then the final value would become -Infinity. It is like, with the absence of any value, I don't know what the minimum is and I assume it is still very big.

Collapse
 
rferromoreno profile image
Ricardo Ferro Moreno

Most people think that Math.max() stands for MAXINT. In some languages, MAXINT is the maximum representable integer value.

In JavaScript, you can check your MAXINT using Number.MAX_SAFE_INTEGER.

Actually, Math.max() is a function that returns the max value of a given list of values.

Now, try to think about how would you implement this function. Our own "Math.max" function. It would be something like this:

Math.prototype.max = function () {
  var max = -Infinity;
  for (var i=0; i<arguments.length; i++) {
    if (arguments[i]>max) {
      max = arguments[i];
    }
  }
  return max;
}

Since no arguments are given in a call to Math.max(), then we never iterate the for-loop, and then, -Infinity is returned.

Analogously, the same happens to Math.min().

If you want to read the real Math.max specification, please check the ECMAScript standard: ecma-international.org/ecma-262/6....

Collapse
 
dance2die profile image
Sung M. Kim

Thank you, Ricardo for the code as it makes it easier to grasp the concept
and for the link to the ECMAScript Language Specification.

It's funny that all these years using JavaScript, I've never checked out the spec 🙃

Collapse
 
totiiimon profile image
Totiimon

Despite being a little bit non-intuitive... I think one problem is trying to interprete this function as something like Number.MAX_VALUE In order to compare with something to find the max value, you will always have one number to start on, and to make it to be the max or the lowest always, makes total sense to have the "current" max or min value being -Infinity and Infinity respectively.

Collapse
 
dance2die profile image
Sung M. Kim • Edited

You are spot on with my misthinking that Math.max() === Number.MAX_VALUE 😂

Collapse
 
umutcnkus profile image
Umutcan Kuş

Actually, even after all explanations it is still not a good design decision to me. But, your idea for reason is legit.
Another interesting thing is:

Math.min() > Math.max()

The reason is clear i think (:
Here you can see some other strange things about js, including this one.
loomcom.com/blog/0097_the_wats_of_...

Collapse
 
dance2die profile image
Sung M. Kim

Wat!?
JavaScript...
J/k.

Thank you for leading me to the rabbit hole of JavaScript Umutcan 😉

Collapse
 
jakebman profile image
jakebman

Here's another one to blow your mind:

In Java, Math.abs(Integer.MIN_VALUE) is negative

This is per-spec:

Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.

Because |Integer.MIN_VALUE| > Integer.MAX_VALUE, there's no way to represent it as a positive value in only 32 bits. It's basically overflow. C# throws an OverflowException, for instance.
Java doesn't throw on overflow, so it handles it this way.

Collapse
 
dance2die profile image
Sung M. Kim

You are blowing my mind 🤯💥

blown

Collapse
 
dance2die profile image
Sung M. Kim

Thank you, @dominela10 .

It seems like my assumption was "sorta" correct but without concrete understanding of how it was so.
Your explanation made it easy to grasp what's going on.

Lastly, learned a new term, variadic :)

Collapse
 
lukaszwiktor profile image
Łukasz Wiktor • Edited

JavaScript as usual returns something even if it doesn't make sense...
Math.min and Math.max should throw an error if no arguments are given.

Collapse
 
dance2die profile image
Sung M. Kim

.min/max are acting sorta like a constant as Math.PI does when a no arg is passed. Also feels like an "alias" to -/Infinity.

Collapse
 
johnboy5358 profile image
John

This post has a connection with your question.

Hope it helps.

Collapse
 
dance2die profile image
Sung M. Kim

Thank you, John. The linked post was very helpful.