Home Communities
IT Knowledge
Inspiration
Languages
EN

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

0 points
Created by:
640

`Math.Tan()` is a static method that takes only one parameter and returns the approximated value of the tangent mathematical function.

``````using System;

public class Program
{
public static void Main(string[] args)
{
Console.WriteLine( Math.Tan( 0                  ) ); //     0 <-    0 degrees
Console.WriteLine( Math.Tan( 0.7853981633974483 ) ); //    ~1 <-  ~45 degrees ==  PI / 4
Console.WriteLine( Math.Tan( 1.5707963267948966 ) ); // ~+Inf <-  ~90 degrees ==  PI / 2

Console.WriteLine( Math.Tan(-0.7853981633974483 ) ); //   ~-1 <- ~-45 degrees == -PI / 4
Console.WriteLine( Math.Tan(-1.5707963267948966 ) ); // ~-Inf <- ~-90 degrees == -PI / 2
}
}``````

Note: `0.9999999999999999`, `16331239353195370`, `-0.9999999999999999` and `-16331239353195370` should be equal to `1``+Inf`, `-1` and `-Inf` but they are not because of compuptation precision error.

1. Documentation

 Syntax ``````namespace System { public static class Math { // ... public static double Tan(double number) { ... } // ... } }`````` Parameters `number` - double value in radians (primitive value). Result `number` value calculated as `tan(x)` mathematical function (primitive value). Description `Math.Tan()` is a static method that takes only one parameter and returns the approximated value of tangent 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:

``````tan(0 rad) = 0

3. Working with degrees

``````using System;

public class Program
{
static double CalculateTan(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 = 30.0; // calculation step in degrees

for (double deg = x1; deg <= x2; deg += dx)
{
double y = CalculateTan(deg);

Console.WriteLine("tan(" + deg + " deg) = " + y);
}
}
}``````

Output:

``````tan(0 deg) = 0
tan(30 deg) = 0.5773502691896257
tan(60 deg) = 1.7320508075688767
tan(90 deg) = 16331239353195370``````

4. Reversed console plot example

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

public class Program
{
static void PrintLine(double y1, double y2, double dy, char character)
{
StringBuilder line = new StringBuilder("");

for (double y = y1; y < y2; y += dy)
{
line.Append(" ");
}

Console.WriteLine(line.Append(character));
}

public static void Main(string[] args)
{
double x1 = -3.14; // begining of sine chart
double x2 = +3.14; // end of sine chart

double y1 = -4.0;
double y2 = +4.0;

double xSteps = 60;
double ySteps = 60;

double dx = (x2 - x1) / xSteps; // x axis step
double dy = (y2 - y1) / ySteps; // y axis step

{

if (y <= y1 || y >= y2)
{
Console.WriteLine(" ");
}
else
{
PrintLine(y1, y, dy, 'x');
}
}
}
}``````

Output:

``````                               x
x
x
x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x

x
x
x
x
x
x
x
x
x
x
x
x``````

References

1. Trigonometric functions - Wikipedia

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