Thread: Cosine Function

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Cosine Function


    /*--- PROGRAM USING COSINE FUNCTION ---*/
    #include <math.h>
    #define PI 3.1416
    #define MAX 180

    main ( )
    {

    int angle;
    float x,y;

    angle = 0;
    printf( Angle Cos(angle)\n\n);

    while(angle <= MAX)
    {
    x = (PI/MAX)*angle;
    y = cos(x);
    printf(%15d %13.4f\n, angle, y);
    angle = angle + 10;
    }
    }





    In the above program why do we assign x=(PI/MAX) * angle instead of finding the cos (angle) directly??? i.e. angle = 30
    cos (angle) is the answer , then why are using cos(x)? Any help would be appreciated...
  2. #2
  3. No Profile Picture
    Still Learning
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Montreal, Canada
    Posts
    55
    Rep Power
    39
    The cos function takes its arguments in radians. The calculation of x is to convert the value in degrees to radians.
    This would be covered in most intro to trigonometry courses that are given in the last couple of years of high school or early in college.

    If you need To understand more detail there are lots of tutorials. Search for intro trig radians and you are bound to find something.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,214
    Rep Power
    2222
    A common way to generate the cos is with a Maclaurin series, which is a special form of a Taylor series. Maclaurin series for trig functions are based on radians, not degrees, so trig functions need radians.

    You'll cover power series in third-semester calculus. For that matter, throughout calculus we used radians, not degrees.

    BTW, tan(45 degrees) is 1.0. 45 degrees is PI/4 radians. You can use these relationships to generate a value for PI:
    PI = 4.0 * atan(1.0);
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,116
    Rep Power
    1803
    If you are writing code that deals extensively with degrees rather than radians, then you might render teh code more readable and maintainable by writing wrappers for teh standard trig functions eg:
    Code:
    #define PI 3.14159265358979
    
    double dcos( double deg )
    {
        return cos( deg * PI) / 180 ) ;
    }
    Note the definition of PI to 15 significant figures - this is the precision afforded by the double precision floating point data type on most systems - using fewer digits will lead to larger than necessary errors. You should always use the highest precision values possible in calculation and only truncate to the precision needed by the application on presentation of results.

    Note also that complex expressions using these "degree trig" wrappers will involve multiple conversions between degrees and radians which will effect both precision and performance which is some cases may be critical. For serious number crunching and scientific applications, you should stick to radians, possibly converting only at the human interface (input and output). In some application domains such as signal processing, radians is the natural and common trig unit in any case.

IMN logo majestic logo threadwatch logo seochat tools logo