This was a homework problem to find cosine of an angle without using the inbuilt cos function I wrote the following code:

// Program to find cos x using loop.

#include <stdio.h>

#define _USE_MATH_DEFINES
#include <math.h>

double cosine(double);

int main()
{
    double x;

    printf("Enter angle in degrees: ");
    scanf("%lf", &x);
    x = x*M_PI/180.0;

    printf("The value of cos(%lf) is %lf", x, cosine(x));
}

double cosine(double x)
{
    double previous, current = 1;
    double denominator = 1*2, numerator = x*x;
    double sign = -1;


    while(1)
    {
        previous = current;
        current = current + ((numerator)/(denominator))*sign;
        denominator = denominator * (denominator+1) * (denominator+2);
        numerator = numerator*x*x;
        sign = -sign;

        if (fabs(previous - current)<=0.0001)
        {
            break;
        }
    }
    return current;
}

For x = 180 the answer isn't -1 (which is the correct one) I have no clue what is going wrong here. Please help I am relatively new at programming.

1

There are 1 best solutions below

2
NoDakker On

I tested out your code and found an error in the derivation of the factorial value for your denominator. The following line of code was actually not providing a proper factorial value.

denominator = denominator * (denominator+1) * (denominator+2);

It actually was increasing the denominator value too fast.

With that in mind, I did a bit of refactoring including revising the "while" loop test for previous and current to a simple "for" loop with enough iterations to provide the precision you most likely need. Following is a refactored version of your program.

// Program to find cos x using loop.

#include <stdio.h>

#define _USE_MATH_DEFINES
#include <math.h>

double cosine(double);

int main()
{
    double x;

    printf("Enter angle in degrees: ");
    scanf("%lf", &x);
    x = x * M_PI / 180.0;

    printf("The value of cos(%lf) is %lf\n", x, cosine(x));
}

double cosine(double x)
{
    double current = 1.00;
    double denominator = 2.00, numerator = x*x;
    double factor = 2.00;
    double sign = -1;

    for(int i = 0; i < 16; i++)
    {
        current = current + ((numerator)/(denominator))*sign;
        denominator = denominator * (factor+1.00) * (factor+2.00);  /* Derives the proper factorial increase */
        numerator = numerator * x * x;
        sign = -sign;
        factor = factor + 2.00;
    }
    return current;
}

Some points to note.

  • Instead of the previous formula for calculating the needed factorial, a work field for keeping track of the ascending factorial values is added and incremented as needed and utilized in the denominator calculation.
  • Instead of testing for smaller and smaller differences with the "while" loop, a "for" loop is utilized with enough iterations to provide a desired precision for the cosine value.

With those tweaks, following were some tests listed at the terminal.

@Vera:~/C_Programs/Console/Taylor/bin/Release$ ./Taylor 
Enter angle in degrees: 0
The value of cos(0.000000) is 1.000000
@Vera:~/C_Programs/Console/Taylor/bin/Release$ ./Taylor 
Enter angle in degrees: 90
The value of cos(1.570796) is 0.000000
@Vera:~/C_Programs/Console/Taylor/bin/Release$ ./Taylor 
Enter angle in degrees: 180
The value of cos(3.141593) is -1.000000
@Vera:~/C_Programs/Console/Taylor/bin/Release$ ./Taylor 
Enter angle in degrees: 270
The value of cos(4.712389) is 0.000000
@Vera:~/C_Programs/Console/Taylor/bin/Release$ ./Taylor 
Enter angle in degrees: 360
The value of cos(6.283185) is 1.000000

Give those tweaks a try and see if it meets the spirit of your project.