denysdovhan / wtfjs

🤪 A list of funny and tricky JavaScript examples
http://bit.ly/wtfjavascript
Do What The F*ck You Want To Public License
34.99k stars 2.55k forks source link

Why is Math.max() less than Math.min()? #23

Closed vermuz closed 3 years ago

vermuz commented 7 years ago

Thought, it should be here as well, https://charlieharvey.org.uk/page/why_math_max_is_less_than_math_min

denysdovhan commented 7 years ago

Great! Could you send a PR with a corresponding section?

ghost commented 7 years ago

I have to disagree: it may look funny but it is very natural behavior and not JavaScript-specific. Actually, one would expect Math.min() and Math.max() to be undefined because an empty set does not contain any elements, hence a maximum or minimum cannot exist. But that wouldn't really have any advantage and would be quite inconvenient because properties like

would not necessarily be true for empty sets. In that sense, Math.max/min() being +/-Infinity is the only choice that preserves these and other properties.

Mathematically, Math.max and Math.min behave like the supremum and infimum. The supremum of a set of numbers A is the smallest number that is greater than all elements of A. The supremum is called maximum of A if the supremum of A is in A. So for a finite nonempty set of numbers, supremum and maximum are one and the same. The supremum of the empty set is negative infinity because all numbers are upper bounds for the empty set and the smallest of these is negative infinity.

vermuz commented 7 years ago

Although, i agree about the inherent mathematical logic, as javascript user who probably hasnt thought through the logic, this behavior is pretty funny.

console.log(Math.min(1,4,7,2));
1

console.log(Math.max(1,4,7,2));
7

console.log(Math.min());
Infinity

console.log(Math.max());
-Infinity

i find this hilarious,

console.log(Math.min() > Math.max());
true
github-actions[bot] commented 3 years ago

:tada: This issue has been resolved in version 1.17.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: