4 ms·
Unfortunately, the min and the max of empty python lists are not +INFINITY and -INFINITY, as they should be for similar reasons.
by enriquto 7y ago
Unfortunately, the min and the max of empty python lists are not +INFINITY and -INFINITY, as they should be for similar reasons.
- graup 7y agoGood point. I was a bit surprised when I found out that Javascript's `Math.min(...[]) ` and `Math.max(...[])` return +Infinity and -Infinity, respectively. But after thinking about it, it makes a lot of sense. It does the right thing when the argument is an empty list.
- enriquto 7y agoMaybe the justification is that you do not know the type of an empty python list. But numpy at least could assume that the numbers are floats and give the correct result instead of raising an error (and thus complicating the interface).
- throwaway_pdp09 7y ago> Maybe the justification is that you do not know the type of an empty python list. Excellent point, thanks!
- oefrha 7y agoThat wouldn't make any sense. >>> min(['a', 'b']) 'a' >>> min(['a']) 'a' >>> min([]) float('-inf') # ??? An empty list is not a list of numbers. It's not a list of anything. (You can use typing hinting to indicate the type of an empty list, but type hints are ignored by the interpreter.)
- enriquto 7y agoI'd rather argue that "min" only makes sense for numbers and not for other objects.
- oefrha 7y agoNo, min makes sense for any set with a total order. Sometimes partial order too.
- enriquto 7y agoTypically you would use "least" and "greatest" (for a partial order) or "first" and "last" (for a total order). It sounds very strange to talk about "min" and "max" for strings, for example. Do you call the first word on a dictionary the minimal word? But then again, what can you expect from a language that uses "+", the quintessential notation for commutative operations, for the (very non-commutative) operation of concatenation? Doubly sad when you realize that the language was designed by an actual mathematician.
- oefrha 7y agoI was trained as a mathematician and I can confirm that min and max (for minimal and maximal, or minimum and maximum) are mathematically accurate terms. See https://en.wikipedia.org/wiki/Maximal_and_minimal_elements https://en.wikipedia.org/wiki/Maximal_and_minimal_elements
- enriquto 7y ago> I can confirm that min and max (for minimal and maximal, or minimum and maximum) are mathematically accurate terms. This is true, but they do not have exactly the same meaning. On a poset you do not speak of "the min", but of "a min". You still have "the least", which is a different notion.
- oefrha 7y agoWhich is why I said "min makes sense for any set with a total order. Sometimes partial order too." There's no ambiguity for total order whatsoever.
- bakery2k 7y ago
- pwdisswordfish2 7y agoAn empty list is a list of anything. If it were not a list of anything, it would not be a list at all.
- dooglius 7y agoYou could make an artificial min/max of all types object that gets returned on empty lists
- int_19h 7y agomin/max(X) is expected to return some Y for which (Y in X) is true.
- eesmith 7y agoThe min() function doesn't know the type. The minimum possible value for a list of strings is "", for example. To get what you want, use min(lst, default=-math.inf) .
- eru 7y agoAgreed. In Python 2, you had float('-inf') < "" So you could sort of get away with that. But that positive infinity wasn't bigger than any string. And in Python 3 comparing strings and numbers just results in an error.
- billpg 7y agoMax returns a string with an infinite number of U+1FFFFs for an empty collection. True story.
- throwaway_pdp09 7y agoInteresting point. You got me thinking and opened a wormcan in my mind. I guess True and False are in the set of Booleans, but infinities are, very strongly arguably, not in the domain of numbers. But still, but still.
- enriquto 7y agoThere are many different "domains" of numbers, and many of them contain infinities. For example, the affinely extended real line, or the IEEE754 floats, are two widely used domains of numbers that contain +inf and -inf.
- throwaway_pdp09 7y agoThose infinities are conceptual only. They don't exist. If infinity was part of the integer domain then (infinity + 1) > infinity which it isn't. I know about IEEE infs and nans, and neither are numbers, just useful conventions.
- enriquto 7y agoI do not understand what your words mean. All numbers are conceptual. None of them "exists", they are all just useful conventions.
- throwaway_pdp09 7y ago> All numbers are conceptual Agreed, neet to be precise here, so let's de-abstract away from numbers. Which is greater, 3 apples or 8 apples? Now give me infinity apples. Finite apples exist, infinite apples don't except as an idea. Personally, infinities make me uncomfortable.
- layoutIfNeeded 7y agoHow do you know that 3 apples is less than 8 apples? How do you know when I give you a basket of apples that it contains 3 apples or 8?
- deleted 7y ago[deleted]
- mFixman 7y ago`min` and `max` operate on semigroups, not on monoids. They don't have to have a default value, and any nicely strongly typed language should prevent them from acting on empty lists. You could invent a monoid instance of `float` for `<` which includes `inf` as an empty element and, if you ignore NaN, it kinda sorta works in isolation. However, adding infinity breaks a lot of other nice properties to the numbering system. For example, the following identity of `min` is valid in the finite numbers. ``` n < min A ⟺ (∀ a ∈ A) n < a ``` The right part of the equation is always true if `A = ∅`, and according to our new monoid the left part is true if and only if `n ≠ ∞`.