# Math.min returns Infinity?

### Sung M. Kim γ»1 min read

# Preface

I was playing around with "Math.min/max" and the result of empty calls looked like it was backwards.

# Question

Shouldn't `Math.min`

return `-Infinity`

while `Math.max`

, `Infinity`

?

# MDN Documentations

MDN documentation on Math.max shows that

If no arguments are given, the result is -Infinity.

And Math.min documentation shows

If no arguments are given, the result is Infinity.

# But why? π€

After some thinking, it made sense.

Suppose that you are passing one value to `Math.min(3)`

. The minimum should `3`

as it's the sole value pass to the function. `3`

should be lower than whatever the minimum JavaScript has to compare.

Any value other than Infinity itself (`Infinity === Infinity`

is `true`

) should be the minimum, and as `3`

is smaller than `Infinity`

, 3 is returned by `Math.min`

.

Same thing for `Math.max`

. If you call `Math.max(3)`

, `3`

is bigger than `-Infinity`

thus, `3`

is returned.

# But...

I am not exactly sure if my thought process is correct or not but at least it helps to understand what default values are returned when no argument is passed to `Math.min/max`

functions.

Would anyone let me know if I understood the reason behind the return values?

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.Thanks Nicolas.

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

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.

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.

`monoid`

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

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.

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: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....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 π

`min()`

and`max()`

functions can be thought of as monoids.Monoids have following laws:

Associativity:

For all a, b and c in S, the equation (a β’ b) β’ c = a β’ (b β’ c) holds.

Identity element:

There exists an element e in S such that for every element a in S, the equations e β’ a = a β’ e = a hold.

The important law here is the second one. Since floating point numbers can't represent all real numbers, they have Infinity and -Infinity values to represent values that have gotten outside of the representable range. Since every possible Number besides Infinity has to be smaller than Infinity, and the other way around, these values are the identity elements for

`min()`

and`max()`

functions.Since

`Math.min()`

and`Math.max()`

are variadic, it makes sense for them to handle the case when they're given 0 arguments properly, especially with the lenient nature of JavaScript.Thank you, @dominela10 .

It seems like my assumption was "sorta" correct but without concrete understanding of

howit was so.Your explanation made it easy to grasp what's going on.

Lastly, learned a new term, variadic :)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:

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_...

Wat!?

JavaScript...

J/k.

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

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.You are spot on with my misthinking that

`Math.max() === Number.MAX_VALUE`

πHere's another one to blow your mind:

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

This is per-spec:

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.

You are blowing my mind π€―π₯

This post has a connection with your question.

Hope it helps.

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