C# Reference ยท Numbers and Math
Trigonometric functions in C#
Reference for trigonometry in C#: Math.Sin, Cos, Tan, Asin, Acos, Atan, Atan2, hyperbolic functions, radians and degrees, and rounding noise.
The Math class provides the standard trigonometric functions: Sin, Cos and Tan, their inverses Asin, Acos, Atan and Atan2, and the hyperbolic versions Sinh, Cosh and Tanh. All of them take and return double, and all of them work in radians, not degrees. A full circle is 2 * Math.PI (also available as Math.Tau).
To convert, multiply degrees by Math.PI / 180, or call double.DegreesToRadians and double.RadiansToDegrees. Forgetting this conversion is the most common trigonometry bug: Math.Sin(90) is not 1.
double radians = double.DegreesToRadians(30); double half = Math.Sin(radians); // approximately 0.5
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static double Sin(double a); public static double Cos(double d); public static double Tan(double a); public static double Asin(double d); public static double Acos(double d); public static double Atan(double d); public static double Atan2(double y, double x); public static double Sinh(double value); public static double Cosh(double value); public static double Tanh(double value); public static (double Sin, double Cos) SinCos(double x); double.DegreesToRadians(double degrees); double.RadiansToDegrees(double radians);
| Name | Description |
|---|---|
a, d, value |
The angle in radians (for Sin, Cos, Tan) or the ratio (for the inverse functions). |
y, x |
For Atan2, the vertical and horizontal coordinates, in that order. |
Returns: Sin, Cos: values from -1 to 1. Tan: any real number. Asin: -PI/2 to PI/2. Acos: 0 to PI. Atan: -PI/2 to PI/2. Atan2: -PI to PI (the angle of the point (x, y) from the positive x axis).
Errors / undefined behavior: no exceptions. Asin and Acos return NaN outside [-1, 1]. Sin, Cos and Tan of infinity or NaN return NaN. Atan2(0, 0) is 0.
Usage notes
- Radians, always. 180 degrees is
Math.PI. Keep angles in radians internally and convert only at the edges (user input and display). - Argument order of
Atan2. It isAtan2(y, x), the reverse of the usual(x, y)point order. Mixing them up rotates the answer by 90 degrees and mirrors it. Atan2overAtan.Math.Atan(y / x)divides by zero for vertical lines and cannot tell the quadrant.Atan2handles both.- Never expect exact zeros.
Math.Sin(Math.PI)is about1.22e-16, not0, becauseMath.PIis itself an approximation. Compare with a tolerance or round for display. See Comparing floating-point values. - Large arguments lose accuracy. For angles far from zero, reduce them first, for example
angle % Math.Tau, if you need the most precise results. SinCosis faster than two calls when you need both values, as in rotations and polar-to-Cartesian conversions.- Float versions.
MathF.Sinand friends work onfloat. The generic-math types exposedouble.Sin,double.SinPi,double.CosPianddouble.TanPi, which take their argument in half-turns and are exact at multiples such as 0.5. - Platform differences. The functions use the system math library, so the last digits can differ between operating systems and CPUs. Print rounded values in tests.