In C#, Math.Sin(a) returns the sine of an angle. The angle must be in radians, not degrees. The answer is always between -1 and 1.
Picture a clock hand of length 1 turning from the 3 o'clock position. The sine is how high the tip is above the middle line. At the start it is 0, at the top it is 1, and at the bottom it is -1. A full turn is 2 * Math.PI radians, so a quarter turn is Math.PI / 2. To convert degrees to radians, multiply by Math.PI / 180. Its partners are Cos() and Tan().
This is a static member of the static class System.Math. You call it with the class name, such as Math.Sin(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Sin is in every .NET version. It takes and returns double, and the angle is in radians.
Syntax
Pass an angle in radians. The answer is a double from -1 to 1.
C#
double a = Math.Sin(0); // 0
double b = Math.Sin(Math.PI / 2); // 1
double deg30 = 30 * Math.PI / 180; // 30 degrees in radians
double c = Math.Sin(deg30); // about 0.5
Call
What it does
Math.Sin(0)
Returns 0.
Math.Sin(Math.PI / 2)
A quarter turn gives 1.
Math.Sin(Math.PI)
A half turn should be 0, but returns a tiny number about 1.2E-16 because pi is not exact.
Math.Sin(double.PositiveInfinity)
Returns NaN.
Because Math.PI is only an approximation of pi, results that should be exactly 0 come out as tiny numbers like 1.2246467991473532E-16. Round them (Math.Round(value, 4)) before printing or comparing. Infinity and NaN input give NaN output. The method never throws.
Library declarations
C#
public static double Sin(double a);
Parameters
Parameter
Description
a
Required. An angle in radians. A negative angle turns the other way.
Return Value
Return Value
Description
double
The sine of a, a number from -1 to 1. NaN if a is NaN or infinity.
C# Examples
Example 1 — Sine at Key Angles
Print the sine of 0, a quarter turn, a half turn, one sixth of a turn, and three quarters of a turn, rounded to 4 decimals.
C#
using System;
using System.Globalization;
class Program {
static string R(double value) {
return Math.Round(value, 4).ToString(CultureInfo.InvariantCulture);
}
static void Main() {
Console.WriteLine(R(Math.Sin(0)));
Console.WriteLine(R(Math.Sin(Math.PI / 2)));
Console.WriteLine(R(Math.Sin(Math.PI)));
Console.WriteLine(R(Math.Sin(Math.PI / 6)));
Console.WriteLine(R(Math.Sin(3 * Math.PI / 2)));
}
}
Output
0
1
0
0.5
-1
How It Works
The helper.R rounds to 4 places and prints with a dot, so tiny floating-point errors and regional settings cannot change the output.
0 and pi / 2. At the start, the sine is 0. After a quarter turn (pi / 2 radians), it is at the top, 1.
pi. After a half turn, the sine should be 0. The raw value is about 0.00000000000000012, which rounds to 0.
pi / 6 and 3 pi / 2. One sixth of a half turn (30 degrees) gives 0.5. Three quarters of a turn is the bottom, -1.
Example 2 — Convert Degrees First
Write a small helper that converts degrees to radians, and see what happens when you forget to convert.
C#
using System;
using System.Globalization;
class Program {
static double Rad(double degrees) {
return degrees * Math.PI / 180;
}
static string R(double value) {
return Math.Round(value, 4).ToString(CultureInfo.InvariantCulture);
}
static void Main() {
Console.WriteLine(R(Math.Sin(Rad(30))));
Console.WriteLine(R(Math.Sin(Rad(90))));
Console.WriteLine(R(Math.Sin(Rad(150))));
Console.WriteLine(R(Math.Sin(Rad(210))));
Console.WriteLine(R(Math.Sin(90)));
}
}
Output
0.5
1
0.5
-0.5
0.894
How It Works
The conversion.Rad multiplies by Math.PI / 180. One degree is about 0.01745 radians.
30 and 90 degrees. The sine of 30 degrees is 0.5, and the sine of 90 degrees is 1. These are the angles you will see most often.
150 and 210 degrees. 150 degrees is mirrored around the top, so it gives 0.5 again. 210 degrees is below the middle line, so it gives -0.5.
The classic mistake.Math.Sin(90) treats 90 as radians, which is about 14 full turns plus a bit. The answer, 0.894, is not what you wanted.
Example 3 — Walk Around a Circle
Step through quarter turns and watch the sine rise to 1, return to 0, and drop to -1. Then see what infinity gives.
C#
using System;
using System.Globalization;
class Program {
static string R(double value) {
return Math.Round(value, 4).ToString(CultureInfo.InvariantCulture);
}
static void Main() {
for (int i = 0; i < 4; i++) {
double angle = i * Math.PI / 2;
Console.WriteLine(R(Math.Sin(angle)));
}
Console.WriteLine(Math.Sin(double.PositiveInfinity));
}
}
Output
0
1
0
-1
NaN
How It Works
Quarter turns. The loop makes the angles 0, pi / 2, pi, and 3 pi / 2. Each is a quarter of a full circle further round.
Up to the top. The sine goes 0 then 1 then back to 0. This is the shape of a wave.
Down to the bottom. At three quarters of a turn, the sine is -1, the lowest value it can have.
Infinity. An angle of infinity has no meaning, so Math.Sin(double.PositiveInfinity) returns NaN and does not throw.
Frequently Asked Questions
It returns the sine of an angle given in radians. The result is between -1 and 1. Math.Sin(Math.PI / 2) is 1.
Math.Sin expects radians. 90 radians is not a quarter turn. Convert degrees first with Math.Sin(90 * Math.PI / 180), which is 1.
Math.PI is a rounded copy of pi, so the angle is a tiny bit off from a half turn. The result is about 1.2E-16. Round it with Math.Round(value, 4) before comparing.
Multiply by Math.PI / 180. To go back, multiply radians by 180 / Math.PI. Newer .NET versions also have double.DegreesToRadians(degrees).
No. Math is a static class, so you call Math.Sin(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.