Skip to content

Expose the DegreesToRadians and RadiansToDegrees APIs#88866

Merged
tannergooding merged 4 commits intodotnet:mainfrom
tannergooding:fix-86402
Jul 14, 2023
Merged

Expose the DegreesToRadians and RadiansToDegrees APIs#88866
tannergooding merged 4 commits intodotnet:mainfrom
tannergooding:fix-86402

Conversation

@tannergooding
Copy link
Member

This resolves #86402

@ghost
Copy link

ghost commented Jul 13, 2023

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost ghost assigned tannergooding Jul 13, 2023
@ghost
Copy link

ghost commented Jul 13, 2023

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

Issue Details

This resolves #86402

Author: tannergooding
Assignees: -
Labels:

area-System.Numerics, new-api-needs-documentation

Milestone: -

@GrabYourPitchforks
Copy link
Member

I think the order of operations leads to a loss of precision here. From Sharplab:

Console.WriteLine(DegToRads1(double.MaxValue));
Console.WriteLine(DegToRads2(double.MaxValue));

static double DegToRads1(double deg) => (deg * Math.PI) / 180.0;
static double DegToRads2(double deg) => deg * (Math.PI / 180.0);

Output:

∞
3.1375664143845866E+306

@tannergooding
Copy link
Member Author

That’s expected due to the type precision and input domain (as well as general limitations with how we can validly implement the DIM).

The same thing can happen for other pairs of functions as well (like exp and log, cos and acos, etc)

/// <returns>The value of <paramref name="degrees" /> converted to radians.</returns>
static virtual TSelf DegreesToRadians(TSelf degrees)
{
// We don't want to simplify this to: degrees * (Pi / 180)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a lengthy comment to this API here and pointed the other implementations to it to help avoid people trying to change the algorithm without considering the edge cases.

Copy link
Member

@GrabYourPitchforks GrabYourPitchforks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appreciate the comment - thank you! :)

Copy link
Member

@danmoseley danmoseley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great comment

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Convert between degrees and radians

5 participants