Search code examples
.netmath

Does the .net framework have a build-in method to convert from degrees to radians?


I know that I can just say:

radians  = degrees * Math.PI/180

However I expect there to be a built in framework method, as it is such a common requirement, however I cannot find one. Am I not looking hard enough or is it missing from the .net framework.

(Please don’t insult me by telling me how to write my own method.)


Solution

  • For the benefit of recent searchers, as of .NET 8+, the ITrigonometricFunctions<TSelf> interface includes the static generic methods TSelf DegreesToRadians<TSelf>(TSelf degrees) and TSelf RadiansToDegrees<TSelf>(TSelf radians).

    This interface is implemented by System.Double, System.Single, System.Half, and System.Runtime.InteropServices.NFloat.

    So you can now do these conversions by calling Double.DegreesToRadians(someDoubleDegreesValue), Single.RadiansToDegrees(someSingleRadiansValue), etc.