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.
Rationale and Usage
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.
Two examples on how we currently can clamp in .NET:
int result = Math.Max(Math.Min(value, 100), 0);
public static class Ext {
// Source: http://stackoverflow.com/a/2683487/1455541
public static T Clamp<T>(T val, T min, T max) where T : IComparable<T>
{
if (val.CompareTo(min) < 0) return min;
else if(val.CompareTo(max) > 0) return max;
else return val;
}
}
// Somewhere in the codebase
int result = Ext.Clamp(value, 0, 100);
The proposed API would turn that into:
int result = Math.Clamp(value, 0, 100);
Proposed API 1 - Specialized
public static class Math {
public static Byte Clamp(Byte value, Byte min, Byte max);
public static Decimal Clamp(Decimal value, Decimal min, Decimal max);
public static Double Clamp(Double value, Double min, Double max);
public static Int16 Clamp(Int16 value, Int16 min, Int16 max);
public static Int32 Clamp(Int32 value, Int32 min, Int32 max);
public static Int64 Clamp(Int64 value, Int64 min, Int64 max);
public static SByte Clamp(SByte value, SByte min, SByte max);
public static Single Clamp(Single value, Single min, Single max);
public static UInt16 Clamp(UInt16 value, UInt16 min, UInt16 max);
public static UInt32 Clamp(UInt32 value, UInt32 min, UInt32 max);
public static UInt64 Clamp(UInt64 value, UInt64 min, UInt64 max);
}
This proposed version mimics the way current System.Math methods do it.
Proposed API 2 - IComparable
Because this is a new API addition it can also be possible to have smaller implementation using generics that should cover all the cases.
public static class Math {
public static T Clamp<T>(T value, T min, T max) where T : System.IComparable<T>;
}
Open Questions
- If the api addition is going to be implemented should it be implemented as Proposed API 1 or 2?
I know the
System.Mathlibrary 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.Rationale and Usage
Math.Clampis 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 useMath.MaxandMath.Mintogether. The proposed API will shorten the amount of code needed and increase readability.Two examples on how we currently can clamp in .NET:
The proposed API would turn that into:
Proposed API 1 - Specialized
This proposed version mimics the way current
System.Mathmethods do it.Proposed API 2 - IComparable
Because this is a new API addition it can also be possible to have smaller implementation using generics that should cover all the cases.
Open Questions