{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/corefx/issues/467","labels_url":"https://api.github.com/repos/dotnet/corefx/issues/467/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/corefx/issues/467/comments","events_url":"https://api.github.com/repos/dotnet/corefx/issues/467/events","html_url":"https://github.com/dotnet/corefx/issues/467","id":54711048,"number":467,"title":"Add a Clamp method to System.Math","user":{"login":"sgtfrankieboy","id":640715,"avatar_url":"https://avatars.githubusercontent.com/u/640715?v=3","gravatar_id":"","url":"https://api.github.com/users/sgtfrankieboy","html_url":"https://github.com/sgtfrankieboy","followers_url":"https://api.github.com/users/sgtfrankieboy/followers","following_url":"https://api.github.com/users/sgtfrankieboy/following{/other_user}","gists_url":"https://api.github.com/users/sgtfrankieboy/gists{/gist_id}","starred_url":"https://api.github.com/users/sgtfrankieboy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sgtfrankieboy/subscriptions","organizations_url":"https://api.github.com/users/sgtfrankieboy/orgs","repos_url":"https://api.github.com/users/sgtfrankieboy/repos","events_url":"https://api.github.com/users/sgtfrankieboy/events{/privacy}","received_events_url":"https://api.github.com/users/sgtfrankieboy/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dotnet/corefx/labels/api+addition","name":"api addition","color":"207de5"},{"url":"https://api.github.com/repos/dotnet/corefx/labels/needs-api-review","name":"needs-api-review","color":"009800"},{"url":"https://api.github.com/repos/dotnet/corefx/labels/System.Runtime.Extensions","name":"System.Runtime.Extensions","color":"d4c5f9"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":9,"created_at":"2015-01-18T21:34:17Z","updated_at":"2015-01-19T11:18:32Z","closed_at":null,"body":"I know the `System.Math` library hasn't been pushed to the .Net Core library but since discussing api additions could take some time I thought it was a good idea to propose it now.\r\n\r\n# Rationale and Usage\r\n\r\n`Math.Clamp` is an easy to implement method and makes it easier for developers. Currently developers would have to implement their own methods in an extension class or use `Math.Max` and `Math.Min` together. The proposed API will shorten the amount of code needed and increase readability.\r\n\r\nTwo examples on how we currently can clamp in .NET:\r\n\r\n```C#\r\nint result = Math.Max(Math.Min(value, 0), 100);\r\n```\r\n```C#\r\npublic static class Ext {\r\n    // Source: http://stackoverflow.com/a/2683487/1455541\r\n    public static T Clamp<T>(T val, T min, T max) where T : IComparable<T>\r\n    {\r\n        if (val.CompareTo(min) < 0) return min;\r\n        else if(val.CompareTo(max) > 0) return max;\r\n        else return val;\r\n    }\r\n}\r\n// Somewhere in the codebase\r\nint result = Ext.Clamp(value, 0, 100);\r\n```\r\nThe proposed API would turn that into:\r\n\r\n```C#\r\nint result = Math.Clamp(value, 0, 100);\r\n```\r\n\r\n# Proposed API 1\r\n\r\n```C#\r\npublic static class Math {\r\n\r\n    public static Byte Clamp(Byte value, Byte min, Byte max);\r\n    public static Decimal Clamp(Decimal value, Decimal min, Decimal max);\r\n    public static Double Clamp(Double value, Double min, Double max);\r\n    public static Int16 Clamp(Int16 value, Int16 min, Int16 max);\r\n    public static Int32 Clamp(Int32 value, Int32 min, Int32 max);\r\n    public static Int64 Clamp(Int64 value, Int64 min, Int64 max);\r\n    public static SByte Clamp(SByte value, SByte min, SByte max);\r\n    public static Single Clamp(Single value, Single min, Single max);\r\n    public static UInt16 Clamp(UInt16 value, UInt16 min, UInt16 max);\r\n    public static UInt32 Clamp(UInt32 value, UInt32 min, UInt32 max);\r\n    public static UInt64 Clamp(UInt64 value, UInt64 min, UInt64 max);\r\n\r\n}\r\n```\r\n\r\nThis proposed version mimics the way current `System.Math` methods do it.\r\n\r\n# Proposed API 2\r\n\r\nBecause this is a new API addition it can also be possible to have smaller implementation  using generics that should cover all the cases.\r\n\r\n```C#\r\npublic static class Math {\r\n\r\n    public static T Clamp<T>(T value, T min, T max)  where T : System.IComparable<T>;\r\n\r\n}\r\n```\r\n\r\n# Open Questions\r\n\r\n* If the api addition is going to be implemented should it be implemented as Proposed API 1 or 2?"},"comment":{"url":"https://api.github.com/repos/dotnet/corefx/issues/comments/70478455","html_url":"https://github.com/dotnet/corefx/issues/467#issuecomment-70478455","issue_url":"https://api.github.com/repos/dotnet/corefx/issues/467","id":70478455,"user":{"login":"sgtfrankieboy","id":640715,"avatar_url":"https://avatars.githubusercontent.com/u/640715?v=3","gravatar_id":"","url":"https://api.github.com/users/sgtfrankieboy","html_url":"https://github.com/sgtfrankieboy","followers_url":"https://api.github.com/users/sgtfrankieboy/followers","following_url":"https://api.github.com/users/sgtfrankieboy/following{/other_user}","gists_url":"https://api.github.com/users/sgtfrankieboy/gists{/gist_id}","starred_url":"https://api.github.com/users/sgtfrankieboy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sgtfrankieboy/subscriptions","organizations_url":"https://api.github.com/users/sgtfrankieboy/orgs","repos_url":"https://api.github.com/users/sgtfrankieboy/repos","events_url":"https://api.github.com/users/sgtfrankieboy/events{/privacy}","received_events_url":"https://api.github.com/users/sgtfrankieboy/received_events","type":"User","site_admin":false},"created_at":"2015-01-19T11:18:32Z","updated_at":"2015-01-19T11:18:32Z","body":"Neither `Clamp` methods in XNA and Unity have an option to change the epsilon.\r\n\r\nUnity however [has a read-only value](http://docs.unity3d.com/412/Documentation/ScriptReference/Mathf.Epsilon.html)."}}