EN
C# / .NET - Math.Sin() method example
0 points
Math.Sin
is a static method that takes only one parameter and returns an approximation of sine mathematical function.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main(string[] args)
6
{
7
Console.WriteLine( Math.Sin( 0 ) ); // 0 <- 0 degrees
8
Console.WriteLine( Math.Sin( 1.5707963267948966 ) ); // ~1 <- ~90 degrees == PI / 2
9
Console.WriteLine( Math.Sin( 3.1415926535897932 ) ); // ~0 <- ~180 degrees == PI
10
Console.WriteLine( Math.Sin( 4.71238898038469 ) ); // ~-1 <- ~270 degrees == -PI * (3/2)
11
Console.WriteLine( Math.Sin( 6.2831853071795850 ) ); // ~0 <- ~360 degrees == PI * 2
12
13
Console.WriteLine( Math.Sin(-1.5707963267948966 ) ); // ~-1 <- ~-90 degrees == -PI / 2
14
}
15
}
Note:
1.2246467991473532e-16
and-1.133107779529596e-15
should be equal to0
but they are not because of compuptation precision error.
Syntax |
xxxxxxxxxx 1 namespace System 2 { 3 public static class Math 4 { 5 // ... 6 public static double Sin(double number) { ... } 7 // ... 8 } 9 } |
Parameters | number - double value that represents an angle in radians (primitive value). |
Result |
If the function can not calculate it returns |
Description | Sin is a static method that takes only one parameter and returns an approximation of sin(x) mathematical function. |
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public 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.Sin(rad);
15
Console.WriteLine("sin(" + rad + " rad) = " + y);
16
}
17
}
18
}
Output:
xxxxxxxxxx
1
sin(0 rad) = 0
2
sin(0.3490658503988659 rad) = 0.3420201433256687
3
sin(0.6981317007977318 rad) = 0.6427876096865393
4
sin(1.0471975511965976 rad) = 0.8660254037844386
5
sin(1.3962634015954636 rad) = 0.984807753012208
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
static double CalculateSin(double deg)
6
{
7
double radians = (Math.PI / 180) * deg;
8
return Math.Sin(radians);
9
}
10
11
public static void Main(string[] args)
12
{
13
// Example:
14
15
double x1 = 0.0; // beginning of calculation in degrees
16
double x2 = 90.0; // ending of calculation degrees
17
18
double dx = 15.0; // calculation step in degrees
19
20
for (double deg = x1; deg <= x2; deg += dx)
21
{
22
double y = CalculateSin(deg);
23
Console.WriteLine("sin(" + deg + " deg) = " + y);
24
}
25
}
26
}
Output:
xxxxxxxxxx
1
sin(0 deg) = 0
2
sin(15 deg) = 0.25881904510252074
3
sin(30 deg) = 0.49999999999999994
4
sin(45 deg) = 0.7071067811865476
5
sin(60 deg) = 0.8660254037844386
6
sin(75 deg) = 0.9659258262890683
7
sin(90 deg) = 1
xxxxxxxxxx
1
using System;
2
using System.Text;
3
4
public class Program
5
{
6
public static void Main(string[] args)
7
{
8
double x1 = 0.0; // beginning of sine chart
9
double x2 = 2 * 3.14; // end of sine 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.Sin(rad) + 1;
18
StringBuilder line = new StringBuilder();
19
for (double y = y1; y < y2; y += dy)
20
{
21
line.Append(" ");
22
}
23
Console.WriteLine(line + "+");
24
}
25
}
26
}
Output:
xxxxxxxxxx
1
+
2
+
3
+
4
+
5
+
6
+
7
+
8
+