-
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]: Convert between degrees and radians #86402
Comments
Tagging subscribers to this area: @dotnet/area-system-numerics Issue DetailsBackground and motivationMost applications performing 2D and 3D spatial calculations need to convert between radians and degrees. For example, Unity includes the constants API Proposal namespace System.Numerics {
public interface ITrigonometricFunctions<TSelf> {
+ public static abstract TSelf ToDegrees(TSelf radians);
+ public static abstract TSelf ToRadians(TSelf degrees);
}
} And, if namespace System
{
public static class Math
{
+ public static double ToDegrees(double radians) => radians.ToDegrees();
+ public static double ToRadians(double degrees) => degrees.ToDegrees();
}
public static class MathF
{
+ public static float ToDegrees(float radians) => radians.ToDegrees();
+ public static float ToRadians(float degrees) => degrees.ToDegrees();
}
} API Usagevar angleInRadians = Math.PI;
// Convert from radians to degrees. Returns 180.0
var angleInDegrees= angleInRadians.ToDegrees();
// Convert from degrees to radians. Returns (an aproximation of) π
angleInRadians= angleInDegrees.ToRadians(); Alternative DesignsAn alternative design was proposed in #38566: namespace System {
public static class Math {
+ public const double RadiansToDegrees = 57.295779513;
+ public const double DegreesToRadians = 0.0174532925;
} However, this was rejected in favor of methods:
Risks
|
If this is an important scenario, equivalent vector methods should be exposed instead. |
Instead of putting them under System.Math, I would like .NET to provide graphics primitives and spatial calculation helpers in System.Numerics.Spatial as a separate new assembly. |
Conversion between degrees and radians are fairly core and primitive operations, there is no reason for them to be in their own less discoverable namespace/location nor for them to be disconnected from the generic math interfaces. If you have a desire for additional functionality, you'll need to open a separate issue/discussion covering what exactly you're looking for in more detail. -- "graphics primitives" and "spatial calculation helpers" is itself fairly vague and could cover many topics. We already have a number of graphics primitives under For a discussion, covering the gist of what's desired with a few examples in text is fine. For an actual issue proposing new API surface, it needs to follow the API proposal template and include the full surface area of what's being requested and details about the design, usage, other considerations, etc. |
namespace System.Numerics {
public interface ITrigonometricFunctions<TSelf> {
+ public static virtual TSelf RadiansToDegrees(TSelf radians) { imprecise, but working, body }
+ public static virtual TSelf DegreesToRadians(TSelf degrees) { imprecise, but working, body }
}
} |
Hi I'd like to take this up, if it's ready for grabs 🙂 |
Assigned out, let me know if you need any assistance 😄 |
@tannergooding When I tried to cast it as double and perform the operation. |
It does, because The implementation you've defined is invalid, is the problem. The implementation should be something more like: public static virtual TSelf RadiansToDegrees(TSelf radians)
{
return (radians * TSelf.CreateChecked(180)) / TSelf.Pi;
}
public static virtual TSelf DegreesToRadians(TSelf degrees)
{
return (degrees * TSelf.Pi) / TSelf.CreateChecked(180);
} When writing generic math based implementations, things need to be in terms of |
Will the Jit be able to lower this into a constant, and if so does it matter whether there are parens to do the division first? |
This is explicitly not lowerable to a constant and we don't want it to be because it may produce a different result for some inputs. That being said, for a constant input, the JIT would be able to optimize it down to a constant output. The parens are there for clarity and to help emphasize the correct order of operations, even if it matches what the compiler will actually do. Where division is done in an expression is a place that many people frequently get wrong so having them helps clarify it. It also makes it clear that |
@danmoseley As Tanner said in the API review:
|
I've put up #88866 since there's been no response in over a month and we only have a couple more days before we lose the opportunity to land these in .NET 8 |
Background and motivation
Most applications performing 2D and 3D spatial calculations need to convert between radians and degrees. For example, Unity includes the constants
Mathf.Deg2Rad
andRad2Deg
; Microsoft XNA/MonoGame includes the functionsMathHelper.ToDegrees
andToRadians
; and NetTopologySuite inclues the methodsRadians.ToDegrees
andDegrees.ToRadians
.API Proposal
And, if
Math
andMathF
can still evolve despite the introduction of generic math, it would be nice to also include sugar for these there.API Usage
Alternative Designs
An alternative design was proposed in #38566:
However, this was rejected in favor of methods:
There was one counterpoint that should still be considered tough:
Risks
The text was updated successfully, but these errors were encountered: