-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Description
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 and Rad2Deg; Microsoft XNA/MonoGame includes the functions MathHelper.ToDegrees and ToRadians; and NetTopologySuite inclues the methods Radians.ToDegrees and Degrees.ToRadians.
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 Math and MathF can still evolve despite the introduction of generic math, it would be nice to also include sugar for these there.
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 Usage
var angleInRadians = Math.PI;
// Convert from radians to degrees. Returns 180.0
var angleInDegrees= double.ToDegrees(angleInRadians);
// Convert from degrees to radians. Returns (an aproximation of) π
angleInRadians= double.ToRadians(angleInDegrees);Alternative Designs
An alternative design was proposed in #38566:
namespace System {
public static class MathF {
+ public const float RadiansToDegrees = 57.295779513f;
+ public const float DegreesToRadians = 0.0174532925f;
}However, this was rejected in favor of methods:
Functions will allow a more precise implementation to be provided or for other optimizations to occur, without restricting things.
There was one counterpoint that should still be considered tough:
A constant could also be used for vectors
var input = new Vector3(10f, 20f, 30f) * MathF.DegreesToRadians;
Risks
- Adding members to an interface is always risky
- There may be some churn and confusion as developers migrate from various APIs in existing libraries, but this is short-term