Languages
[Edit]
EN

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

0 points
Created by:
Zeeshan-Peel
730

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) { ... }
        // ...
    }
}
Parametersnumber - double value in radians (primitive value).
Resultnumber value calculated as tan(x) mathematical function (primitive value).
DescriptionMath.Tan() is a static method that takes only one parameter and returns the approximated value of tangent mathematical function.

2. Working with radians

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

        for (double rad = x1; rad <= x2; rad += dx)
        {
            double y = Math.Tan(rad);

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

Output: 

tan(0 rad) = 0
tan(0.3490658503988659 rad) = 0.36397023426620234
tan(0.6981317007977318 rad) = 0.8390996311772799
tan(1.0471975511965976 rad) = 1.7320508075688767
tan(1.3962634015954636 rad) = 5.671281819617707

3. Working with degrees

using System;

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

        return Math.Tan(radians);
    }

    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

        for (double rad = x1; rad < x2; rad += dx)
        {
            double y = Math.Tan(rad);

            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

See also

  1. C# / .NET - Math.Atan()
  2. C# / .NET - Math.Atan2()

References

  1. Trigonometric functions - Wikipedia

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Cross technology - Math.tan()

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join