In C#, Math.Cos(d) returns the cosine of an angle given in radians. The answer is always between -1 and 1. Math.Cos(0) is 1.
Use the same clock-hand picture as Sin(): a hand of length 1 starting at 3 o'clock. The sine is how high the tip is. The cosine is how far right the tip is. At the start it is 1 (all the way right), after a quarter turn it is 0, and after a half turn it is -1. Together, Cos and Sin give you the x and y position of a point on a circle.
This is a static member of the static class System.Math. You call it with the class name, such as Math.Cos(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Cos 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.Cos(0); // 1
double b = Math.Cos(Math.PI); // -1
double rad = 60 * Math.PI / 180; // 60 degrees in radians
double c = Math.Cos(rad); // about 0.5
Call
What it does
Math.Cos(0)
Returns 1.
Math.Cos(Math.PI)
A half turn gives -1.
Math.Cos(Math.PI / 2)
A quarter turn should be 0, but returns a tiny number near 6.1E-17 because pi is not exact.
Math.Cos(double.NaN)
Returns NaN.
Like Sin, results that should be exactly 0 come out as tiny numbers, so round before printing or comparing. The cosine is an even function: Cos(-x) equals Cos(x). Infinity and NaN input give NaN. The method never throws.
Library declarations
C#
public static double Cos(double d);
Parameters
Parameter
Description
d
Required. An angle in radians.
Return Value
Return Value
Description
double
The cosine of d, a number from -1 to 1. NaN if d is NaN or infinity.
C# Examples
Example 1 — Cosine at Key Angles
Print the cosine of 0, a third of a half turn, a quarter turn, a half turn, and a full 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.Cos(0)));
Console.WriteLine(R(Math.Cos(Math.PI / 3)));
Console.WriteLine(R(Math.Cos(Math.PI / 2)));
Console.WriteLine(R(Math.Cos(Math.PI)));
Console.WriteLine(R(Math.Cos(2 * Math.PI)));
}
}
Output
1
0.5
0
-1
1
How It Works
Angle 0. The hand points all the way right, so the cosine is 1.
pi / 3 (60 degrees). The hand has moved up and left of the start. Its horizontal position is half, 0.5.
pi / 2 (90 degrees). The hand points straight up, so it is not right or left at all. The raw value is about 0.00000000000000006, which rounds to 0.
pi and 2 pi. Half a turn points left, -1. A full turn is back at the start, 1.
Example 2 — Find a Point on a Circle
Use Cos and Sin with a radius to get the x and y position of a point, for two angles.
The formula. A point on a circle with a given radius is at x = radius * Cos(angle) and y = radius * Sin(angle), measured from the centre.
60 degrees. The cosine is 0.5, so x is 10 x 0.5 = 5. The sine is about 0.866, so y is 8.66.
A half turn. At pi radians, the point is on the far left: x is -10.
Tiny noise. y should be exactly 0 here, but the raw value is a tiny positive number. Rounding to 2 places shows 0.
Example 3 — Sine Squared Plus Cosine Squared
Check the identity that sin squared plus cos squared is always 1, that cosine is even, and what infinity gives.
C#
using System;
using System.Globalization;
class Program {
static void Main() {
foreach (double angle in new double[] { 0.5, 1, 2 }) {
double sum = Math.Sin(angle) * Math.Sin(angle) + Math.Cos(angle) * Math.Cos(angle);
Console.WriteLine(Math.Round(sum, 6));
}
Console.WriteLine(Math.Round(Math.Cos(1), 6) == Math.Round(Math.Cos(-1), 6));
Console.WriteLine(Math.Cos(double.PositiveInfinity));
}
}
Output
1
1
1
True
NaN
How It Works
The rule. For every angle, the sine squared plus the cosine squared is 1. It is the circle's radius-1 rule in disguise.
Three angles. The loop tests 0.5, 1, and 2 radians. Rounding to 6 places removes tiny errors, so each line shows 1.
Even function. Turning backwards by 1 radian lands at the same horizontal position as turning forwards, so Cos(1) and Cos(-1) match. The comparison is True.
Infinity. An infinite angle makes no sense, so you get NaN (no exception).
Frequently Asked Questions
It returns the cosine of an angle in radians, a number from -1 to 1. Math.Cos(0) is 1 and Math.Cos(Math.PI) is -1.
Radians. Convert degrees with degrees * Math.PI / 180 before calling it.
Use x = radius * Math.Cos(angle) and y = radius * Math.Sin(angle), with the angle in radians. See also Math.SinCos, which returns both in one call.
Math.PI is a rounded copy of pi, so the angle is a hair off a quarter turn. The result is about 6.1E-17. Round to a few decimals before comparing.
No. Math is a static class, so you call Math.Cos(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.