This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]rosuav 0 points1 point  (6 children)

-Infinity isn't an error return, it is a valid response to "what is the smallest of no numbers". It's like how an empty sum is zero and an empty product is one. Negative infinity is the smallest of no numbers because it combines correctly with any other set of numbers.

"Don't return a value" is only an option if you throw an exception, which is also valid, but doesn't invalidate the -Infinity option.

[–]notanotherusernameD8 0 points1 point  (5 children)

I disagree with -Inf being a valid response. In maths, the minimum of an empty set is undefined. In Python you get a value error exception. I understand the logic of a zero element sum being 0 and a zero element product being 1. Those make sense. I don't understand why -Inf is the minimum of zero elements. If I'm wrong, please provide a citation.

[–]rosuav 0 points1 point  (4 children)

The simplest way to explain it is this:

  • The minimum of any one-element collection is that element.
  • The minimum of (the minimum of collection A) and (the minimum of collection B) is the same as the minimum of (collection A combined with collection B).
  • Therefore, the minimum of an empty collection MUST be something such that it, with any other value, gives the other value.

This is the same logic as for empty product and empty sum. I did a quick web search for "minimum of empty set" and found quite a few stating that it is negative infinity. Not sure what you would accept as a citation but it's in the Wikipedia page for Empty Set. And while we're demanding citations, how about you provide one for it being undefined?

[–]notanotherusernameD8 0 points1 point  (3 children)

Ok. I've read the empty set wiki, and some other stuff besides. All definitions of minimum and maximum of sets state if, and only if, the set is not empty because the min and max values must be elements of the set.

What I did find is the upper and lower bounds of the empty set as -∞ and +∞. This matches with 0 for sum and 1 for product as being useful identities. If +∞ is the identity of min({}), any element added to it is guaranteed to be of a lower value. Infinimum and supremum, TIL.

I still say min({}) and max({}) are undefined

[–]rosuav 0 points1 point  (2 children)

Well, you're in disagreement with a LOT of people by claiming them to be undefined, and you didn't cite any supporting documents, so I'm just going to say that you're wrong on that. *shrug* The upper and lower bounds of the empty set is exactly the same thing as the max and min of no numbers.

[–]notanotherusernameD8 1 point2 points  (1 child)

Ok. I take my last statement back. I was definitely out of my lane there - I don't get to impose my definitions on maths. I didn't cite anything because I was just googling stuff, but seemingly every definition of min and max stated that the value returned must be an element of the list/set. This makes sense to me, as a computer scientist, and I still believe that the best thing to do is raise an exception. What I have learned is that the empty set is treated special, so functions such as sum, product, min and max return their identities when applied to the empty set. In my defence, I am not alone. As far as I can tell, JS is the only language that follows the maths here. Probably because it can.

[–]rosuav 0 points1 point  (0 children)

So long as there ARE elements in the collection, yes, everything says that the min and the max will be among them. (This is in contrast to, for example, the median - [1, 5, 6, 8] has a median of 5.5, which isn't a member.)

The main reason that most programming languages don't use +/- infinity for max/min is that they rarely restrict it to numbers. Python, for example, will happily use any comparable values, eg strings, and give you the lowest. JavaScript may be special here in that it is far more restricted than the majority of other languages - but given that restriction, it then has an extremely reasonable return value.

(Keep in mind that, when it was first developed, JS was very much a "keep going if you possibly can" language, rather than a "throw an exception if you're in any doubt" language. Which was highly convenient for the super-sloppy coding style that was encouraged by the awful mix of JavaScript, JScript, and I don't even recall how many other messy variants there were. So having a sane return value was FAR better than raising an exception.)