Skip to content

Math.Min<T> Max<T> Clamp<T> #43796

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

Closed
TonyValenti opened this issue Oct 24, 2020 · 11 comments
Closed

Math.Min<T> Max<T> Clamp<T> #43796

TonyValenti opened this issue Oct 24, 2020 · 11 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Numerics
Milestone

Comments

@TonyValenti
Copy link

TonyValenti commented Oct 24, 2020

Background and Motivation

Please make it easier to min/max/clamp any classes.

Proposed API

Right now the Math.Min / Max / Clamp methods only work with a small number of built in data types. A generic version of these methods should be introduced on the Comparer<T> base class.

public T Comparer<T>.Min(T Value1, T Value2);
public T Comparer<T>.Max(T Value1, T Value2);
public T Comparer<T>.Clamp(T Value, T Min, T Max);

public static T Comparer.Min(T Value1, T Value2) => Comparer<T>.Default.Min(Value1, Value2);
public static T Comparer.Max(T Value1, T Value2) => Comparer<T>.Default.Max(Value1, Value2);
public static T Comparer.Clamp(T Value, T Min, T Max) => Comparer<T>.Default.Clamp(Value1, Min, Max);

Example Implementation

    public static class ComparerExtensions {
        public static T Min<T>(this Comparer<T> This, T Value1, T Value2) {
            var ret = This.Compare(Value1, Value2) <= 0
                ? Value1
                : Value2
                ;

            return ret;
        }

        public static T Max<T>(this Comparer<T> This, T Value1, T Value2) {
            var ret = This.Compare(Value1, Value2) >= 0
                ? Value1
                : Value2
                ;

            return ret;
        }

        public static T Clamp<T>(this Comparer<T> This, T Value, T Min, T Max) {
            var ret = Value;
            if (This.Compare(Value, Min) < 0) {
                ret = Min;
            } else if (This.Compare(Value, Max) > 0) {
                ret = Max;
            }

            return ret;
        }
    }

Usage Examples

Comparer.Clamp(MyValue, TimeSpan.Zero, TimeSpan.FromSeconds(30))

Alternative Designs

This could just be ignored.

Risks

None.

@TonyValenti TonyValenti added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Oct 24, 2020
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Oct 24, 2020
@EgorBo
Copy link
Member

EgorBo commented Oct 24, 2020

Comparer<T>.Default.CompareToneeds to be devirtualized for this: #39873

@ghost
Copy link

ghost commented Oct 24, 2020

Tagging subscribers to this area: @tannergooding, @pgovind, @jeffhandley
See info in area-owners.md if you want to be subscribed.

@GrabYourPitchforks
Copy link
Member

Generic min / max / clamp methods seem worthwhile. But the names feel a bit wrong (first / last? before / after?), and Math might not be the right place for it. Otherwise you end up with weirdness like this:

string first = Math.Min("foo", "bar"); // would compile and return a logical result

I don't have better recommendations unfortunately. :(

@En3Tho
Copy link
Contributor

En3Tho commented Oct 29, 2020

What about Comparer.Min/Max/Clamp? Or some new Comparable class? But this way Math still would need specified methods and Comparer class would have generic ones?...

@TonyValenti
Copy link
Author

@En3Tho Excellent idea!

@GrabYourPitchforks
Copy link
Member

Related: #18481

@tannergooding tannergooding added needs author feedback and removed untriaged New issue has not been triaged by the area owner labels Jan 7, 2021
@tannergooding
Copy link
Member

@TonyValenti, could you update the original post with the updated API proposal? It sounds like users are wanting to add this to Comparable?

@TonyValenti
Copy link
Author

@tannergooding Updated.

@ghost ghost added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration and removed needs author feedback labels Jan 7, 2021
@tannergooding tannergooding added this to the Future milestone Jun 17, 2021
@tannergooding
Copy link
Member

This might be feasible with the generic math interfaces that are being proposed.

@tannergooding
Copy link
Member

Closing. This is now feasible using the generic math interfaces. In particular anything where T : INumber<T> can now T.Min, T.Max, or T.Clamp

@tannergooding tannergooding closed this as not planned Won't fix, can't repro, duplicate, stale Sep 8, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Oct 9, 2022
@tannergooding tannergooding removed the needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration label Jun 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Numerics
Projects
None yet
Development

No branches or pull requests

7 participants