Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generalize min and max to non-numeric types #238

Closed
StefanKarpinski opened this issue Oct 23, 2011 · 9 comments
Closed

generalize min and max to non-numeric types #238

StefanKarpinski opened this issue Oct 23, 2011 · 9 comments

Comments

@StefanKarpinski
Copy link
Member

Since < is generic and applies not only to numeric types, in principle, min and max should also be, although then having negative and positive infinities as the zero-argument default is wrong.

@JeffBezanson
Copy link
Member

We could just remove the type declarations on the arguments to min and max.

Then I propose we get rid of the definitions for max(x::Real) etc. and have 1-argument versions always mean reduction over a container.

Maybe we should also have a compiler intrinsic for min/max of two native type numbers, since that could be faster than calling libm fmax like we do now.

@StefanKarpinski
Copy link
Member Author

Yes, that all seems reasonable. Also, if you apply min/max to an empty but typed container, then we can give the right min/max value for that type in some cases, but in general, it's ill-defined.

@JeffBezanson
Copy link
Member

I just started doing that; min(int8([])) gives an Int8, but min({}) gives an error.
Interestingly I believe "" serves as a minimum string, but there is no maximal string...except RepString("\U10ffff",Inf)???

@StefanKarpinski
Copy link
Member Author

Interestingly I believe "" serves as a minimum string, but there is no maximal string...except RepString("\U10ffff",Inf)???

Yes, that seems correct — RepString("\U10ffff", Inf) isn't a real string since strings have finite length. The fact that that expression does throw an error is a mistake. I think strings are a case where there's a global min but no global max.

@JeffBezanson
Copy link
Member

Oh dear, that's because my default constructors accept any arguments and assign them to the fields, which performs conversion. Thinking about this again, I'm not sure this is right. Maybe a default constructor should only accept arguments of the field types.

@StefanKarpinski
Copy link
Member Author

I kind of always thought that was a bit suspect. It's nice sometimes, but it's also kind of dangerous.

@JeffBezanson
Copy link
Member

I can change it very easily if we want.

@StefanKarpinski
Copy link
Member Author

Yeah, the only issue is that there's probably a bunch of places we'll want to insert manual convert calls then. But it's probably best to do this now rather than later.

@JeffBezanson
Copy link
Member

As far as the original purpose of this issue, we're ready to go. I made max and min reductions require typemin(eltype(container)) to be able to give an answer for an empty container. All we have to do is change the 2-argument declarations and delete the 0-argument ones. FWIW lisp doesn't allow min and max to take zero arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants