Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

Math.Cos Method

Returns the cosine of the specified angle.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public static double Cos (
	double d
)
public static double Cos (
	double d
)
public static function Cos (
	d : double
) : double
Not applicable.

Parameters

d

An angle, measured in radians.

Return Value

The cosine of d.

The angle, d, must be in radians. Multiply by Math.PI/180 to convert degrees to radians.

Acceptable values of d range from approximately -9223372036854775295 to approximately 9223372036854775295. For values outside this range, the Cos method returns d unchanged rather than throwing an exception.

The following example uses Cos to evaluate certain trigonometric identities for selected angles.

// Example for the trigonometric Math.Sin( double ) 
// and Math.Cos( double ) methods.
using System;

class SinCos 
{
    public static void Main() 
    {
        Console.WriteLine( 
            "This example of trigonometric " +
            "Math.Sin( double ) and Math.Cos( double )\n" +
            "generates the following output.\n" );
        Console.WriteLine( 
            "Convert selected values for X to radians \n" +
            "and evaluate these trigonometric identities:" );
        Console.WriteLine( "   sin^2(X) + cos^2(X) == 1\n" +
                           "   sin(2 * X) == 2 * sin(X) * cos(X)" );
        Console.WriteLine( "   cos(2 * X) == cos^2(X) - sin^2(X)" );

        UseSineCosine(15.0);
        UseSineCosine(30.0);
        UseSineCosine(45.0);

        Console.WriteLine( 
            "\nConvert selected values for X and Y to radians \n" +
            "and evaluate these trigonometric identities:" );
        Console.WriteLine( "   sin(X + Y) == sin(X) * cos(Y) + cos(X) * sin(Y)" );
        Console.WriteLine( "   cos(X + Y) == cos(X) * cos(Y) - sin(X) * sin(Y)" );

        UseTwoAngles(15.0, 30.0);
        UseTwoAngles(30.0, 45.0);
    }

    // Evaluate trigonometric identities with a given angle.
    static void UseSineCosine(double degrees)
    {
        double angle    = Math.PI * degrees / 180.0;
        double sinAngle = Math.Sin(angle);
        double cosAngle = Math.Cos(angle);

        // Evaluate sin^2(X) + cos^2(X) == 1.
        Console.WriteLine( 
            "\n                           Math.Sin({0} deg) == {1:E16}\n" +
            "                           Math.Cos({0} deg) == {2:E16}",
            degrees, Math.Sin(angle), Math.Cos(angle) );
        Console.WriteLine( 
            "(Math.Sin({0} deg))^2 + (Math.Cos({0} deg))^2 == {1:E16}", 
            degrees, sinAngle * sinAngle + cosAngle * cosAngle );

        // Evaluate sin(2 * X) == 2 * sin(X) * cos(X).
        Console.WriteLine( 
            "                           Math.Sin({0} deg) == {1:E16}", 
            2.0 * degrees, Math.Sin(2.0 * angle) );
        Console.WriteLine( 
            "    2 * Math.Sin({0} deg) * Math.Cos({0} deg) == {1:E16}", 
            degrees, 2.0 * sinAngle * cosAngle );

        // Evaluate cos(2 * X) == cos^2(X) - sin^2(X).
        Console.WriteLine( 
            "                           Math.Cos({0} deg) == {1:E16}", 
            2.0 * degrees, Math.Cos(2.0 * angle) );
        Console.WriteLine( 
            "(Math.Cos({0} deg))^2 - (Math.Sin({0} deg))^2 == {1:E16}", 
            degrees, cosAngle * cosAngle - sinAngle * sinAngle );
    }

    // Evaluate trigonometric identities that are functions of two angles.
    static void UseTwoAngles(double degreesX, double degreesY)
    {
        double  angleX  = Math.PI * degreesX / 180.0;
        double  angleY  = Math.PI * degreesY / 180.0;

        // Evaluate sin(X + Y) == sin(X) * cos(Y) + cos(X) * sin(Y).
        Console.WriteLine( 
            "\n        Math.Sin({0} deg) * Math.Cos({1} deg) +\n" + 
            "        Math.Cos({0} deg) * Math.Sin({1} deg) == {2:E16}", 
            degreesX, degreesY, Math.Sin(angleX) * Math.Cos(angleY) +
            Math.Cos(angleX) * Math.Sin(angleY));
        Console.WriteLine( 
            "                           Math.Sin({0} deg) == {1:E16}",
            degreesX + degreesY, Math.Sin(angleX + angleY));

        // Evaluate cos(X + Y) == cos(X) * cos(Y) - sin(X) * sin(Y).
        Console.WriteLine( 
            "        Math.Cos({0} deg) * Math.Cos({1} deg) -\n" + 
            "        Math.Sin({0} deg) * Math.Sin({1} deg) == {2:E16}", 
            degreesX, degreesY, Math.Cos(angleX) * Math.Cos(angleY) -
            Math.Sin(angleX) * Math.Sin(angleY));
        Console.WriteLine( 
            "                           Math.Cos({0} deg) == {1:E16}",
            degreesX + degreesY, Math.Cos(angleX + angleY));
    }
}

/*
This example of trigonometric Math.Sin( double ) and Math.Cos( double )
generates the following output.

Convert selected values for X to radians
and evaluate these trigonometric identities:
   sin^2(X) + cos^2(X) == 1
   sin(2 * X) == 2 * sin(X) * cos(X)
   cos(2 * X) == cos^2(X) - sin^2(X)

                           Math.Sin(15 deg) == 2.5881904510252074E-001
                           Math.Cos(15 deg) == 9.6592582628906831E-001
(Math.Sin(15 deg))^2 + (Math.Cos(15 deg))^2 == 1.0000000000000000E+000
                           Math.Sin(30 deg) == 4.9999999999999994E-001
    2 * Math.Sin(15 deg) * Math.Cos(15 deg) == 4.9999999999999994E-001
                           Math.Cos(30 deg) == 8.6602540378443871E-001
(Math.Cos(15 deg))^2 - (Math.Sin(15 deg))^2 == 8.6602540378443871E-001

                           Math.Sin(30 deg) == 4.9999999999999994E-001
                           Math.Cos(30 deg) == 8.6602540378443871E-001
(Math.Sin(30 deg))^2 + (Math.Cos(30 deg))^2 == 1.0000000000000000E+000
                           Math.Sin(60 deg) == 8.6602540378443860E-001
    2 * Math.Sin(30 deg) * Math.Cos(30 deg) == 8.6602540378443860E-001
                           Math.Cos(60 deg) == 5.0000000000000011E-001
(Math.Cos(30 deg))^2 - (Math.Sin(30 deg))^2 == 5.0000000000000022E-001

                           Math.Sin(45 deg) == 7.0710678118654746E-001
                           Math.Cos(45 deg) == 7.0710678118654757E-001
(Math.Sin(45 deg))^2 + (Math.Cos(45 deg))^2 == 1.0000000000000000E+000
                           Math.Sin(90 deg) == 1.0000000000000000E+000
    2 * Math.Sin(45 deg) * Math.Cos(45 deg) == 1.0000000000000000E+000
                           Math.Cos(90 deg) == 6.1230317691118863E-017
(Math.Cos(45 deg))^2 - (Math.Sin(45 deg))^2 == 2.2204460492503131E-016

Convert selected values for X and Y to radians
and evaluate these trigonometric identities:
   sin(X + Y) == sin(X) * cos(Y) + cos(X) * sin(Y)
   cos(X + Y) == cos(X) * cos(Y) - sin(X) * sin(Y)

        Math.Sin(15 deg) * Math.Cos(30 deg) +
        Math.Cos(15 deg) * Math.Sin(30 deg) == 7.0710678118654746E-001
                           Math.Sin(45 deg) == 7.0710678118654746E-001
        Math.Cos(15 deg) * Math.Cos(30 deg) -
        Math.Sin(15 deg) * Math.Sin(30 deg) == 7.0710678118654757E-001
                           Math.Cos(45 deg) == 7.0710678118654757E-001

        Math.Sin(30 deg) * Math.Cos(45 deg) +
        Math.Cos(30 deg) * Math.Sin(45 deg) == 9.6592582628906831E-001
                           Math.Sin(75 deg) == 9.6592582628906820E-001
        Math.Cos(30 deg) * Math.Cos(45 deg) -
        Math.Sin(30 deg) * Math.Sin(45 deg) == 2.5881904510252085E-001
                           Math.Cos(75 deg) == 2.5881904510252096E-001
*/

// Example for the trigonometric Math.Sin( double ) 
// and Math.Cos( double ) methods.
import System.*;

class SinCos
{
    
    public static void main(String[] args)
    {
        Console.WriteLine(
            ("This example of trigonometric " 
            + "Math.Sin( double ) and Math.Cos( double )\n" 
            + "generates the following output.\n"));
        Console.WriteLine(
            ("Convert selected values for X to radians \n" 
            + "and evaluate these trigonometric identities:"));
        Console.WriteLine(("   sin^2(X) + cos^2(X) == 1\n" 
            + "   sin(2 * X) == 2 * sin(X) * cos(X)"));
        Console.WriteLine("   cos(2 * X) == cos^2(X) - sin^2(X)");

        UseSineCosine(15.0);
        UseSineCosine(30.0);
        UseSineCosine(45.0);

        Console.WriteLine(
            ("\nConvert selected values for X and Y to radians \n" 
            + "and evaluate these trigonometric identities:"));
        Console.WriteLine("  sin(X + Y) == sin(X) * cos(Y) + cos(X) * sin(Y)");
        Console.WriteLine("  cos(X + Y) == cos(X) * cos(Y) - sin(X) * sin(Y)");

        UseTwoAngles(15.0, 30.0);
        UseTwoAngles(30.0, 45.0);
    } //main
    
    // Evaluate trigonometric identities with a given angle.
    static void UseSineCosine(double degrees)
    {
        double angle = System.Math.PI * degrees / 180.0;
        double sinAngle = System.Math.Sin(angle);
        double cosAngle = System.Math.Cos(angle);

        // Evaluate sin^2(X) + cos^2(X) == 1.
        Console.WriteLine(
            "\n                           Math.Sin({0} deg) == {1}\n" 
            + "                           Math.Cos({0} deg) == {2}", 
            System.Convert.ToString(degrees), 
            ((System.Double)(System.Math.Sin(angle))).ToString("E16"), 
            ((System.Double)(System.Math.Cos(angle))).ToString("E16"));
        Console.WriteLine(
            "(Math.Sin({0} deg))^2 + (Math.Cos({0} deg))^2 == {1}", 
            System.Convert.ToString(degrees), 
            ((System.Double)(
            sinAngle * sinAngle + cosAngle * cosAngle)).ToString("E16"));

        // Evaluate sin(2 * X) == 2 * sin(X) * cos(X).
        Console.WriteLine(
            "                           Math.Sin({0} deg) == {1}", 
            System.Convert.ToString(2.0 * degrees), 
            ((System.Double)(
            System.Math.Sin((2.0 * angle)))).ToString("E16"));
        Console.WriteLine(
            "    2 * Math.Sin({0} deg) * Math.Cos({0} deg) == {1}", 
            System.Convert.ToString(degrees),
            ((System.Double)( 2.0 * sinAngle * cosAngle)).ToString ("E16"));

        // Evaluate cos(2 * X) == cos^2(X) - sin^2(X).
        Console.WriteLine(
            "                           Math.Cos({0} deg) == {1}", 
            System.Convert.ToString( 2.0 * degrees), 
            ((System.Double)(
            System.Math.Cos((2.0 * angle)))).ToString ("E16") );
        Console.WriteLine(
            "(Math.Cos({0} deg))^2 - (Math.Sin({0} deg))^2 == {1}", 
            System.Convert.ToString(degrees),(
            (System.Double)( 
            cosAngle * cosAngle - sinAngle * sinAngle)).ToString ("E16"));
    } //UseSineCosine
   
    // Evaluate trigonometric identities that are functions of two angles.
    static void UseTwoAngles(double degreesX, double degreesY)
    {
        double angleX = System.Math.PI * degreesX / 180.0;
        double angleY = System.Math.PI * degreesY / 180.0;

        // Evaluate sin(X + Y) == sin(X) * cos(Y) + cos(X) * sin(Y).
        Console.WriteLine(
            "\n        Math.Sin({0} deg) * Math.Cos({1} deg) +\n" 
            + "        Math.Cos({0} deg) * Math.Sin({1} deg) == {2}", 
            System.Convert.ToString(degreesX), 
            System.Convert.ToString(degreesY), 
            ((System.Double)(
            System.Math.Sin(angleX) * System.Math.Cos(angleY) 
            + System.Math.Cos(angleX) 
            * System.Math.Sin(angleY))).ToString("E16"));
        Console.WriteLine(
            "                           Math.Sin({0} deg) == {1}", 
            System.Convert.ToString(degreesX + degreesY), 
            ((System.Double)(System.Math.Sin(
            (angleX + angleY)))).ToString("E16"));

        // Evaluate cos(X + Y) == cos(X) * cos(Y) - sin(X) * sin(Y).
        Console.WriteLine(
            "        Math.Cos({0} deg) * Math.Cos({1} deg) -\n" 
            + "        Math.Sin({0} deg) * Math.Sin({1} deg) == {2}", 
            System.Convert.ToString(degreesX), 
            System.Convert.ToString(degreesY), 
            ((System.Double)(
            System.Math.Cos(angleX) * System.Math.Cos(angleY) 
            - System.Math.Sin(angleX) 
            * System.Math.Sin(angleY))).ToString("E16"));
        Console.WriteLine(
            "                           Math.Cos({0} deg) == {1}", 
            System.Convert.ToString(degreesX + degreesY), 
            ((System.Double)(System.Math.Cos(
            (angleX + angleY)))).ToString("E16"));
    } //UseTwoAngles
} //SinCos

/*
This example of trigonometric Math.Sin( double ) and Math.Cos( double )
generates the following output.

Convert selected values for X to radians
and evaluate these trigonometric identities:
   sin^2(X) + cos^2(X) == 1
   sin(2 * X) == 2 * sin(X) * cos(X)
   cos(2 * X) == cos^2(X) - sin^2(X)

                           Math.Sin(15 deg) == 2.5881904510252074E-001
                           Math.Cos(15 deg) == 9.6592582628906831E-001
(Math.Sin(15 deg))^2 + (Math.Cos(15 deg))^2 == 1.0000000000000000E+000
                           Math.Sin(30 deg) == 4.9999999999999994E-001
    2 * Math.Sin(15 deg) * Math.Cos(15 deg) == 4.9999999999999994E-001
                           Math.Cos(30 deg) == 8.6602540378443871E-001
(Math.Cos(15 deg))^2 - (Math.Sin(15 deg))^2 == 8.6602540378443871E-001

                           Math.Sin(30 deg) == 4.9999999999999994E-001
                           Math.Cos(30 deg) == 8.6602540378443871E-001
(Math.Sin(30 deg))^2 + (Math.Cos(30 deg))^2 == 1.0000000000000000E+000
                           Math.Sin(60 deg) == 8.6602540378443860E-001
    2 * Math.Sin(30 deg) * Math.Cos(30 deg) == 8.6602540378443860E-001
                           Math.Cos(60 deg) == 5.0000000000000011E-001
(Math.Cos(30 deg))^2 - (Math.Sin(30 deg))^2 == 5.0000000000000022E-001

                           Math.Sin(45 deg) == 7.0710678118654746E-001
                           Math.Cos(45 deg) == 7.0710678118654757E-001
(Math.Sin(45 deg))^2 + (Math.Cos(45 deg))^2 == 1.0000000000000000E+000
                           Math.Sin(90 deg) == 1.0000000000000000E+000
    2 * Math.Sin(45 deg) * Math.Cos(45 deg) == 1.0000000000000000E+000
                           Math.Cos(90 deg) == 6.1230317691118863E-017
(Math.Cos(45 deg))^2 - (Math.Sin(45 deg))^2 == 2.2204460492503131E-016

Convert selected values for X and Y to radians
and evaluate these trigonometric identities:
   sin(X + Y) == sin(X) * cos(Y) + cos(X) * sin(Y)
   cos(X + Y) == cos(X) * cos(Y) - sin(X) * sin(Y)

        Math.Sin(15 deg) * Math.Cos(30 deg) +
        Math.Cos(15 deg) * Math.Sin(30 deg) == 7.0710678118654746E-001
                           Math.Sin(45 deg) == 7.0710678118654746E-001
        Math.Cos(15 deg) * Math.Cos(30 deg) -
        Math.Sin(15 deg) * Math.Sin(30 deg) == 7.0710678118654757E-001
                           Math.Cos(45 deg) == 7.0710678118654757E-001

        Math.Sin(30 deg) * Math.Cos(45 deg) +
        Math.Cos(30 deg) * Math.Sin(45 deg) == 9.6592582628906831E-001
                           Math.Sin(75 deg) == 9.6592582628906820E-001
        Math.Cos(30 deg) * Math.Cos(45 deg) -
        Math.Sin(30 deg) * Math.Sin(45 deg) == 2.5881904510252085E-001
                           Math.Cos(75 deg) == 2.5881904510252096E-001
*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.