Languages
[Edit]
EN

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

0 points
Created by:
Elleanor-Williamson
380

The Math.Cos() function returns the cosine of the specified angle in radians in the range -1 to +1.

using System;

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

Note: 6.123233995736766e-17, -1.8369701987210297e-16 and 6.123233995736766e-17 should be equal to 0 but they are not because of computation precision error.


1. Documentation

Syntax
namespace System
{
    public final class Math
    {
        // ...
        public static double Cos(double number) { ... }
        // ...
    }
}
Parametersnumber - double value in radians (primitive value).
Result

returns a numeric value between -1 and +1, which represents the cosine of the angle (primitive value).

DescriptionCos is a static method that takes only one parameter and returns an approximation of cos(x) mathematical function result.

2. Working with radians

using System;

class Program
{
    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.Cos(rad);

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

Output: 

cos(0.0 rad) = 1
cos(0.3490658503988659 rad) = 0.9396926207859084
cos(0.6981317007977318 rad) = 0.766044443118978
cos(1.0471975511965976 rad) = 0.5000000000000001
cos(1.3962634015954636 rad) = 0.17364817766693041

3. Working with degrees

using System;

class Program
{
    static double calculateCos(double deg)
    {
        double rad = (Math.PI / 180) * deg;

        return Math.Cos(rad);
    }

    static void Main(string[] args)
    {
        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 = calculateCos(deg);

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

Output: 

cos(0.0 deg) = 1
cos(15.0 deg) = 0.9659258262890683
cos(30.0 deg) = 0.8660254037844387
cos(45.0 deg) = 0.7071067811865476
cos(60.0 deg) = 0.5000000000000001
cos(75.0 deg) = 0.25881904510252074
cos(90.0 deg) = 6.123233995736766E-17

4. Reversed console plot example

using System;
using System.Text;

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

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

        for (double rad = x1; rad < x2; rad += dx)
        {
            double y1 = 0.0;
            double y2 = Math.Cos(rad) + 1;

            StringBuilder line = new StringBuilder();

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

Output: 

           +
         +
      +
  +
 +
  +
     +
         +
          +
         +
      +
  +

References

  1. Cosine - 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.

C# / .NET - Math object

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