EN
C# / .NET - Math.Cos() method example
0 points
The Math.Cos()
function returns the cosine of the specified angle in radians in the range -1
to +1
.
xxxxxxxxxx
1
using System;
2
3
class Program
4
{
5
static void Main(string[] args)
6
{
7
Console.WriteLine( Math.Cos( 0 ) ); // 1 <- 0 degrees
8
Console.WriteLine( Math.Cos( 1.5707963267948966 ) ); // ~0 <- ~90 degrees == PI / 2
9
Console.WriteLine( Math.Cos( 3.1415926535897932 ) ); // ~-1 <- ~180 degrees == PI
10
Console.WriteLine( Math.Cos( 4.71238898038469 ) ); // ~0 <- ~270 degrees == -PI * (3 / 2)
11
Console.WriteLine( Math.Cos( 6.2831853071795850 ) ); // ~1 <- ~360 degrees == PI * 2
12
13
Console.WriteLine( Math.Cos( -1.5707963267948966) ); // ~0 <- ~-90 degrees == -PI / 2
14
}
15
}
Note:
6.123233995736766e-17
,-1.8369701987210297e-16
and6.123233995736766e-17
should be equal to0
but they are not because of computation precision error.
Syntax |
xxxxxxxxxx 1 namespace System 2 { 3 public final class Math 4 { 5 // ... 6 public static double Cos(double number) { ... } 7 // ... 8 } 9 } |
Parameters | number - double value in radians (primitive value). |
Result |
returns a numeric value between |
Description | Cos is a static method that takes only one parameter and returns an approximation of cos(x) mathematical function result. |
xxxxxxxxxx
1
using System;
2
3
class Program
4
{
5
static void Main(string[] args)
6
{
7
double x1 = 0.0; // beginning of calculation in radians
8
double x2 = Math.PI / 2; // ending of calculation radians
9
10
double dx = Math.PI / 9; // calculation step in degrees
11
12
for (double rad = x1; rad <= x2; rad += dx)
13
{
14
double y = Math.Cos(rad);
15
16
Console.WriteLine("cos(" + rad + " rad) = " + y);
17
}
18
}
19
}
Output:
xxxxxxxxxx
1
cos(0.0 rad) = 1
2
cos(0.3490658503988659 rad) = 0.9396926207859084
3
cos(0.6981317007977318 rad) = 0.766044443118978
4
cos(1.0471975511965976 rad) = 0.5000000000000001
5
cos(1.3962634015954636 rad) = 0.17364817766693041
xxxxxxxxxx
1
using System;
2
3
class Program
4
{
5
static double calculateCos(double deg)
6
{
7
double rad = (Math.PI / 180) * deg;
8
9
return Math.Cos(rad);
10
}
11
12
static void Main(string[] args)
13
{
14
double x1 = 0.0; // beginning of calculation in degrees
15
double x2 = 90.0; // ending of calculation degrees
16
17
double dx = 15.0; // calculation step in degrees
18
19
for (double deg = x1; deg <= x2; deg += dx)
20
{
21
double y = calculateCos(deg);
22
23
Console.WriteLine("cos(" + deg + " deg) = " + y);
24
}
25
}
26
}
Output:
xxxxxxxxxx
1
cos(0.0 deg) = 1
2
cos(15.0 deg) = 0.9659258262890683
3
cos(30.0 deg) = 0.8660254037844387
4
cos(45.0 deg) = 0.7071067811865476
5
cos(60.0 deg) = 0.5000000000000001
6
cos(75.0 deg) = 0.25881904510252074
7
cos(90.0 deg) = 6.123233995736766E-17
xxxxxxxxxx
1
using System;
2
using System.Text;
3
4
class Program
5
{
6
static void Main(string[] args)
7
{
8
double x1 = 0.0; // beginning of cosine chart
9
double x2 = 3 * 3.14; // end of cosine chart
10
11
double dx = 3.14 / 4.0; // x axis step
12
double dy = 1.0 / 5.0; // y axis step
13
14
for (double rad = x1; rad < x2; rad += dx)
15
{
16
double y1 = 0.0;
17
double y2 = Math.Cos(rad) + 1;
18
19
StringBuilder line = new StringBuilder();
20
21
for (double y = y1; y < y2; y += dy)
22
{
23
line.Append(" ");
24
}
25
Console.WriteLine(line.Append("+"));
26
}
27
}
28
}
Output:
xxxxxxxxxx
1
+
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+