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?
Top comments (21)
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
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.
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.🙂🤝
I think one reason is
that is, you call pull an element out and do a
min
and the final result is the same. You can pull thenothing
out, or you can pull all out. The final form will makemin([])
to beInfinity
, 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.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 🙃
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 themax
or thelowest
always, makes total sense to have the "current" max or min value being-Infinity
andInfinity
respectively.You are spot on with my misthinking that
Math.max() === Number.MAX_VALUE
😂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 😉
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 🤯💥
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 :)
JavaScript as usual returns something even if it doesn't make sense...
Math.min
andMath.max
should throw an error if no arguments are given..min/max
are acting sorta like a constant asMath.PI
does when a no arg is passed. Also feels like an "alias" to-/Infinity
.