C# 9 includes the new nint/nuint types, but it gets pretty awkward to use them in code that was previously using Math APIs such as Min or Max. This proposal is about adding overloads for the int/uint APIs in System.Math to support nint/nuint.
Right now you either have to manually write ternary operators everywhere to work around this, or just stick with int/uint.
namespace System
{
public static class Math
{
public static nint Abs(nint value);
public static nuint Clamp(nuint value, nuint min, nuint max);
public static nint Clamp(nint value, nint min, nint max);
public static nint DivRem(nint a, nint b, out nint result);
public static nint Max(nint val1, nint val2);
public static nuint Max(nuint val1, nuint val2);
public static nint Min(nint val1, nint val2);
public static nuint Min(nuint val1, nuint val2);
public static nint Sign(nint value);
}
}
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.
Looking at the other APIs, there is also:
Clamp
DivRem
I don't think BigMul makes sense, for the same reason it won't work for long; but Clamp and DivRem seem like reasonable overloads to provide.
Tagging subscribers to this area: @tannergooding, @pgovind, @jeffhandley
See info in area-owners.md if you want to be subscribed.
Added Clamp and DivRem, was looking at the .NET Standard reference and missed those 馃槄
Thanks!
Notably we don't expose unsigned DivRem overloads today. Maybe we should....
Looks good as proposed
C#
namespace System
{
public static class Math
{
public static nint Abs(nint value);
public static nuint Clamp(nuint value, nuint min, nuint max);
public static nint Clamp(nint value, nint min, nint max);
public static nint DivRem(nint a, nint b, out nint result);
public static nint Max(nint val1, nint val2);
public static nuint Max(nuint val1, nuint val2);
public static nint Min(nint val1, nint val2);
public static nuint Min(nuint val1, nuint val2);
public static nint Sign(nint value);
}
}
See the decision on https://github.com/dotnet/runtime/issues/42156 for DivRem and the value tuple pattern.
Most helpful comment
Video
Looks good as proposed
C# namespace System { public static class Math { public static nint Abs(nint value); public static nuint Clamp(nuint value, nuint min, nuint max); public static nint Clamp(nint value, nint min, nint max); public static nint DivRem(nint a, nint b, out nint result); public static nint Max(nint val1, nint val2); public static nuint Max(nuint val1, nuint val2); public static nint Min(nint val1, nint val2); public static nuint Min(nuint val1, nuint val2); public static nint Sign(nint value); } }