-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add DivRem with DivisionRounding (WIP + Review request) #104589
base: main
Are you sure you want to change the base?
Add DivRem with DivisionRounding (WIP + Review request) #104589
Conversation
Note regarding the
|
Note regarding the
|
Tagging subscribers to this area: @dotnet/area-system-numerics |
@@ -11138,6 +11149,7 @@ public partial interface IBinaryFloatingPointIeee754<TSelf> : System.IComparable | |||
public partial interface IBinaryInteger<TSelf> : System.IComparable, System.IComparable<TSelf>, System.IEquatable<TSelf>, System.IFormattable, System.IParsable<TSelf>, System.ISpanFormattable, System.ISpanParsable<TSelf>, System.Numerics.IAdditionOperators<TSelf, TSelf, TSelf>, System.Numerics.IAdditiveIdentity<TSelf, TSelf>, System.Numerics.IBinaryNumber<TSelf>, System.Numerics.IBitwiseOperators<TSelf, TSelf, TSelf>, System.Numerics.IComparisonOperators<TSelf, TSelf, bool>, System.Numerics.IDecrementOperators<TSelf>, System.Numerics.IDivisionOperators<TSelf, TSelf, TSelf>, System.Numerics.IEqualityOperators<TSelf, TSelf, bool>, System.Numerics.IIncrementOperators<TSelf>, System.Numerics.IModulusOperators<TSelf, TSelf, TSelf>, System.Numerics.IMultiplicativeIdentity<TSelf, TSelf>, System.Numerics.IMultiplyOperators<TSelf, TSelf, TSelf>, System.Numerics.INumber<TSelf>, System.Numerics.INumberBase<TSelf>, System.Numerics.IShiftOperators<TSelf, int, TSelf>, System.Numerics.ISubtractionOperators<TSelf, TSelf, TSelf>, System.Numerics.IUnaryNegationOperators<TSelf, TSelf>, System.Numerics.IUnaryPlusOperators<TSelf, TSelf> where TSelf : System.Numerics.IBinaryInteger<TSelf>? | |||
{ | |||
static virtual (TSelf Quotient, TSelf Remainder) DivRem(TSelf left, TSelf right) { throw null; } | |||
static (TSelf Quotient, TSelf Remainder) DivRem(TSelf left, TSelf right, System.DivisionRounding rounding) { throw null; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is incorrect. It should be static virtual
as the same of implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason runtime\src\libraries\System.Runtime\src>dotnet build /t:GenerateReferenceAssemblySource
would remove the virtual
from those methods. I've added it manually.
<Suppression> | ||
<DiagnosticId>CP0006</DiagnosticId> | ||
<Target>M:System.Numerics.IBinaryInteger`1.DivRem(`0,`0,System.DivisionRounding)</Target> | ||
<Left>net8.0/System.Runtime.dll</Left> | ||
<Right>net9.0/System.Runtime.dll</Right> | ||
</Suppression> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This suppression shouldn't be required if you are creating the ref source correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed the suppression, I was able to build with build.cmd -configuration release /p:ApiCompatValidateAssemblies=false
{ | ||
public enum DivisionRounding | ||
{ | ||
Truncate = 0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: assigning underlying numeric value isn't required for implementation source.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar enums include it:
ToEven = 0, |
DivisionRounding.Ceiling => DivRemCeiling(left, right), | ||
DivisionRounding.AwayFromZero => DivRemAwayFromZero(left, right), | ||
DivisionRounding.Euclidean => DivRemEuclidean(left, right), | ||
_ => throw new ArgumentOutOfRangeException(nameof(rounding)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should ideally use a throw helper.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
@tannergooding Would you be able to comment on the |
Partial implementation of #93568 (requesting review of general approach before implementing other types since this is mostly copy & paste).
Done:
System
IBinaryInteger
DivRem
implementationDivRem
implementation forInt32
DivisionRounding
forInt32.DivRem
Todo:
DivRem
implementations for remaining typesDivide
andRemainder
methods for all typesThanks for the feedback!