Home Communities
IT Knowledge
Inspiration
Languages
EN

# C# / .NET - Math.Sin() method example

0 points
Created by:
559

`Math.Sin` is a static method that takes only one parameter and returns an approximation of sine mathematical function.

``````using System;

public class Program
{
public static void Main(string[] args)
{
Console.WriteLine( Math.Sin( 0                  ) ); //   0 <-     0 degrees
Console.WriteLine( Math.Sin( 1.5707963267948966 ) ); //  ~1 <-  ~90 degrees ==  PI / 2
Console.WriteLine( Math.Sin( 3.1415926535897932 ) ); //  ~0 <- ~180 degrees ==  PI
Console.WriteLine( Math.Sin( 4.71238898038469   ) ); // ~-1 <- ~270 degrees == -PI * (3/2)
Console.WriteLine( Math.Sin( 6.2831853071795850 ) ); //  ~0 <- ~360 degrees ==  PI * 2

Console.WriteLine( Math.Sin(-1.5707963267948966 ) ); // ~-1 <- ~-90 degrees == -PI / 2
}
}``````

Note: `1.2246467991473532e-16` and `-1.133107779529596e-15` should be equal to `0` but they are not because of compuptation precision error.

## 1. Documentation

 Syntax ``````namespace System { public static class Math { // ... public static double Sin(double number) { ... } // ... } }`````` Parameters `number` - double value that represents an angle in radians (primitive value). Result `number` value calculated as `sin(x)` mathematical function in a range `-1` to `+1` (primitive value). If the function can not calculate it returns `NaN`. Description `Sin` is a static method that takes only one parameter and returns an approximation of `sin(x)` mathematical function.

``````using System;

public class Program
{
public static void Main(string[] args)
{
double x1 = 0.0;          // beginning of calculation in radians
double x2 = Math.PI / 2;  // ending of calculation radians

double dx = Math.PI / 9; // calculation step in degrees

{
}
}
}``````

Output:

``````sin(0 rad) = 0

## 3. Working with degrees

``````using System;

public class Program
{
static double CalculateSin(double deg)
{
double radians = (Math.PI / 180) * deg;
}

public static void Main(string[] args)
{
// Example:

double x1 = 0.0;  // beginning of calculation in degrees
double x2 = 90.0; // ending of calculation degrees

double dx = 15.0;  // calculation step in degrees

for (double deg = x1; deg <= x2; deg += dx)
{
double y = CalculateSin(deg);
Console.WriteLine("sin(" + deg + " deg) = " + y);
}
}
}``````

Output:

``````sin(0 deg) = 0
sin(15 deg) = 0.25881904510252074
sin(30 deg) = 0.49999999999999994
sin(45 deg) = 0.7071067811865476
sin(60 deg) = 0.8660254037844386
sin(75 deg) = 0.9659258262890683
sin(90 deg) = 1``````

## 4. Reversed console plot example

``````using System;
using System.Text;

public class Program
{
public static void Main(string[] args)
{
double x1 = 0.0;      // beginning of sine chart
double x2 = 2 * 3.14; // end of sine chart

double dx = 3.14 / 4.0; // x axis step
double dy = 1.0 / 5.0; // y axis step

{
double y1 = 0.0;
double y2 = Math.Sin(rad) + 1;
StringBuilder line = new StringBuilder();
for (double y = y1; y < y2; y += dy)
{
line.Append(" ");
}
Console.WriteLine(line + "+");
}
}
}``````

Output:

``````     +
+
+
+
+
+
+
+``````

## References

1. Sine - Wikipedia

Join to our subscribers to be up to date with content, news and offers.