2
#include <stdio.h>

double pi = 3.141592653589; 
int numberOfTerms = 5; 

int factorial(int n)
{
    if(n > 1)
        return n * factorial(n - 1);
    else
        return 1;
}

double DegreesToRadian( double degrees )
    { 
        return degrees * pi / 180;
    } 

void cosine(double cos){
        int x = 0;
        double ans = 1;
        int exponent = 2;
        int isPlus = 0;
        for(x; x < numberOfTerms - 1; x++){
            if(isPlus == 0){
                ans -= (pow(cos, exponent))/factorial(exponent);
                exponent += 2;
                isPlus = 1;
            }else{
                ans += (pow(cos, exponent))/factorial(exponent);
                exponent += 2;
                isPlus = 0;
            }
        }
        printf ("%.12f \t", ans);
    }

void sine(double sin){
        int x = 0;
        double ans = sin;
        int exponent = 3;
        int isPlus = 0;
        for(x; x < numberOfTerms - 1; x++){
            if(isPlus == 0){
                ans -= (pow(sin, exponent))/factorial(exponent);
                exponent += 2;
                isPlus = 1;
            }else{
                ans += (pow(sin, exponent))/factorial(exponent);
                exponent += 2;
                isPlus = 0;
            }
        }
        printf ("%.12f \n", ans);
    }

int main()
{
    double j = -180.00;
    printf(" ");
    printf("\n\n");

        for (j; j <= 180; j += 5){
            printf("%.2f \t", j);
            printf( "%.12f \t", DegreesToRadian(j));
            cosine(DegreesToRadian(j));
            sine(DegreesToRadian(j));
        }

return 0;   
}

I'm using Taylor Series to find the sin and cosine of a number but when I change the numberOfTerms to 10 or 15 it becomes inaccurate(waaaaaaaaayy off), what do I need to change to make it accurate? (Yeah my functions are not optimal lel)

I get a [Warning] incompatible implicit declaration of built-in function 'pow' if that matters.

CassiusDC
  • 25
  • 5
  • Your value for pi is wrong - use M_PI from math.h instead of a home-spun approximation. (You also need to #include anyway, for functions such as `pow()`). – Paul R Nov 13 '16 at 08:30
  • @PaulR I'm required by my school to not use math.h and use the PI approximation. :( – CassiusDC Nov 13 '16 at 08:32
  • @PaulR: wouldn't `atan(1)*4` better? And implicit declaration of stuff returning `float` is really bad. `math.h` is missing there. – Jean-François Fabre Nov 13 '16 at 08:32
  • @CassiusDC: in that case you should not be using `pow()`. (You don't really need it anyway.) – Paul R Nov 13 '16 at 08:33
  • 1
    ok: newsflash: you're not using `math.h` but you're using `pow` which is in the math library. – Jean-François Fabre Nov 13 '16 at 08:33
  • @Jean-FrançoisFabre Tried whipping up my own exponential function, the results were still way off (same as using predefined pow) when changing the numberOfTerms to 10 or 15 – CassiusDC Nov 13 '16 at 08:38
  • I tried to answer but deleted it since the answer below seems to pinpoint the problem (but not giving any solution I'm afraid) – Jean-François Fabre Nov 13 '16 at 08:40
  • 1
    There is no need for either a power function or a factorial function. Please see this [previous answer](http://stackoverflow.com/a/38265293/4142924) – Weather Vane Nov 13 '16 at 09:34
  • Some quantification would be useful - such as the expected and actual result "waaaay off" is not quantifiable and relates toy *your* expectations. It would be helpful to determine whether this is a coding error or an expected floating-point precision error. Note that `double` is goof for 15 significant figures decimal, you PI definition has 13; you could eke a couple more digits out there, and your last digit rounding is incorrect (should be ...590 not ---589) suggest 3.14159265358979. – Clifford Nov 13 '16 at 09:47
  • Floating point uses a finite number of bits to represent an infinite number of real values and is necessarily an approximation. Errors can become large if expressions mix both very large and very small terms. – Clifford Nov 13 '16 at 09:48

2 Answers2

1

Let us assume you keep the value of numberOfTerms as 10. Then, in the cosine and sine functions, in the for loop, you are incrementing exponent by 2 every time. And, you are using the factorial of exponent in the denominator.

If the loop runs 9 times, the value for exponent would increase as 2, 4, 6, 8, 10, 12, 14, 16, 18.

We know that 14! = 87178291200. But a signed int (which is used to return the result of the factorial function) can hold a positive value up to 2147483647. There occurs an overflow.

I suggest you use double (or even unsigned long long) for the return type and the parameter of the factorial function. But do not try to compute factorials of large numbers as they would not fit in any data type in C.

Also, since you have not defined pow function yourself, I think you are missing a #include<math.h> at the top.

Another suggestion, define pi as a symbolic constant rather than a global variable.

skrtbhtngr
  • 2,223
  • 23
  • 29
0

The implicit declaration of pow returns an int, but the actual definition returns double, the code will interpret the double an an int by bit pattern resulting in an entirely incorrect value - not just the integer part of the double.

Clifford
  • 88,407
  • 13
  • 85
  • 165