Runtime: Add System.Math API overloads for nint/nuint

Created on 22 Oct 2020  路  7Comments  路  Source: dotnet/runtime

Background and Motivation

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.

Proposed API

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);
    }
}
api-approved area-System.Numerics

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); } }

All 7 comments

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....

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); } }

See the decision on https://github.com/dotnet/runtime/issues/42156 for DivRem and the value tuple pattern.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

v0l picture v0l  路  3Comments

matty-hall picture matty-hall  路  3Comments

iCodeWebApps picture iCodeWebApps  路  3Comments

omariom picture omariom  路  3Comments

btecu picture btecu  路  3Comments