-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
API Proposal: Generic System.Math Functions #63732
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. |
I'm hoping that this is a duplicate but I couldn't find it. Relates to #63548 |
Tagging subscribers to this area: @dotnet/area-system-numerics Issue DetailsBackground and motivationWith the introduction of generic operators it's expected to be able to use API Proposalnamespace System
{
public static class Math
{
public static T Max<T>(T v1, T v2) where T : IComparisonOperators<T, T>;
public static T Min<T>(T v1, T v2) where T : IComparisonOperators<T, T>;
// ...
}
} API UsageT gv1 = e1;
T gv2 = e2;
T max = Math.Max(gv1, gv2); Alternative DesignsNo response RisksNo response
|
It is a breaking change in overload resolution to add many of these APIs to Take for example: Now, it would be possible to expose The path forward that API review had settled on is that since That is, if you are in a non-generic context, |
I don't know how many APIs could be included here, but I think Min/Max are probably okay? (edit: perhaps in a new static class) I think generic LINQ operators would go under the same umbrella as well: |
I tried to cover this in the second half of my response. There isn't any benefit to doing this. If its a generic method then it has to be appropriately constrained. If its appropriately constrained then you have to likewise have either a concrete type that is statically known to meet the constraint or an appropriately constrained In the first case, you can just call the relevant methods The first does have a minor exception for the scenario where
APIs that work over "spans", "arrays", or "enumerables" probably shouldn't go on |
In this case, |
It cannot. This is made worse by the fact that double x = Math.Sqrt(5); |
Added an edit to my response. I don't think adding these to the same class is a strict requirement. Following |
This overload resolution problem due to implicit conversion is the reason why However, as I've stated above, I do not believe this to be a problem. There are roughly five contexts under which you want to call these functions:
The first scenario ( The second scenario ( The third scenario ( The fourth scenario ( The fifth scenario ( |
Do we really need an impl per type for something like |
Given the above, I would need someone here to give a concrete example of where I've missed something or where the normal use-case of This does not extend to examples like providing a generic variant of the Noting that the distinction between |
Yes. Consider for example Now it may be possible to provide a "default implementation", but that requires |
The same applies to many other methods as well. Things like For a fixed-width two's complement value, such as For a one's complement value, all values have a valid |
There was a request to consolidate Math and MathF. Seeing your statement and this request as well, I wonder if it makes sense to introduce a new math class featuring all of these with support of future changes in mind. Then marking System.Math as deprecated, just like WebClient => HttpClient. Which means developer can still use and build with Math, but they get aware of a newer class exist with better support. And they get aware they have to explicit type Max(5.0d) if they want to call the double overload. |
As per the above (#63732 (comment))
That is,
The path forward isn't going to be "more Math" classes, its going to be that you consistently access these on the members themselves. So you do
The second scenario isn't one that any new math class exposed by the BCL would cover and so exposing a new math class would not provide any additional benefit over what generic math is already providing to the end user. |
Now I think both |
I don't believe we've decided on obsoleting The annotation here is that they may not get new APIs and so if we expose some new API, then |
I agree that one should use They are currently defined in Also for example the implementation for |
Having a comparison that says "x is less than y" is not strictly the same as saying "x is smaller than y". This is certainly true for most numbers and number like types; but comparisons (and things like |
Could you elaborate, please, what is "signed magnitude" in the context of your sentence? |
Take a type where In such domains a type may implement The ordering of values is not strictly related to their size and therefore whether something is larger or smaller. |
I probably don't understand why you imply that max and min mean larger and smaller. They are by math definition are least and greatest, as far as I remember. |
Some types make a distinction between sort order (what Types may implement the former and not the latter and even when the latter is implemented it is not always defined in terms of the former. |
@tannergooding I see, thank you. |
Going to close this. Users can now trivially provide their own equivalents using
|
Background and motivation
With the introduction of generic operators it's expected to be able to use
System.Math
functions in a generic manner.API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: