0

I have to write this program only with #include<stdio.h>.

I have to read the highest power of the series 'n' from the user.

When x=45 and n=9, then the program gives me 0.7068251967. But when I use my calculator for the same then I get 0.7068251828.

I also have to use recursion.

#include<stdio.h>

float pow(float n, int p)
{
if(p == 0)
    return 1;
else
    return n * pow(n, p-1);
}

int fact(int n)
{
if(n == 0)
    return 1;
else
    return n * fact(n-1);
}

int main()
{
int n, x, i, sign = 1;
float sum, r;


printf("Enter the angle in degrees.\n");
scanf("%d", &x);

r = 3.14 * x / 180.0;

printf("Enter the odd number till which you want the series.\n");
scanf("%d", &n);

if(n % 2 == 0)
    printf("The number needs to be an odd number.\n");
else
{


for(i = 1, sum = 0; i <= n; i += 2, sign *= -1)
{
    sum += (sign * pow(r, i)) /  fact(i);
}

printf("The sum of the series is %.10f.\n", sum);
}


return 0;
}
  • 13
    You're doing computations with only 3 significant digits for Pi. – Mat Mar 22 '19 at 15:23
  • 1
    Use `double` for better results too. Relegate `float` to 20th century textbooks and where for some reason you can't use `double`. MSVC's `math.h` has the `#define M_PI` for you to use. – Weather Vane Mar 22 '19 at 15:26
  • 3
    Also, you don't need to use the factorial: you can calculate the coefficient as you go. Similarly you don't need pow either. If you must use recursion then use it to get the next term in the series. None of this will fix the immediate numerical issue though: the comments above take care of that. – Bathsheba Mar 22 '19 at 15:26
  • 1
    ...the factorial will overflow after 12! . – Weather Vane Mar 22 '19 at 15:30
  • @WeatherVane: Before that for 16 bit `int`. – Bathsheba Mar 22 '19 at 15:31
  • Your error message is backwards. After asking for an odd number, you print a message if it's even, saying it must be even. Unless this is a translation error, in which case I apologize. – Tim Randall Mar 22 '19 at 15:40
  • You can agree with the calculator just by changing the `float` to `double`, but you were summing the same series with the calculator. If you ask the calculator for *sine 45* you'll get the very different `0.7071067812`, because of the value of pi you were using. – Weather Vane Mar 22 '19 at 15:42

2 Answers2

0

I think one cause is the fact that you approximate pi by 3.14. Maybe your calculator takes into consideration more digits of pi. Try using more digits for approximatig pi.

Theodor Badea
  • 465
  • 2
  • 9
  • Y U downvote this? This is certainly a good component of the answer. The rest is using `float` where `double` should be used. – Joshua Mar 22 '19 at 15:31
0

@Mat is right, use M_PI instead of your 'poor man' 3.14. Besides do not always exponentiate x to the power n or factorials. Notice that the next term of the sum: a_{k+2}=-a_{k}x^2/(k(k-1)), (the even terms are zero) Use something like

double s=x,a=x,x2=-x*x;
int i;
for (i=3;i<=n;i+=2)
{
   a*=x2/i/(i-1);
   s+=a;
}
Alexey Godin
  • 192
  • 5
  • Annoyingly using `M_PI` means your code is not strictly portable. – Bathsheba Mar 22 '19 at 15:39
  • Then either define it with precision enough or at least use 4*atan(1) – Alexey Godin Mar 22 '19 at 16:48
  • No. No. No. Don't use the trig method. There's no guarantee it will recover the best answer in any floating point standard. My answer for a C++ question on this topic: https://stackoverflow.com/questions/49778240/does-c11-14-17-or-20-introduce-a-standard-constant-for-pi/49778493#49778493 – Bathsheba Mar 22 '19 at 16:48
  • Yep, 4*atan(1) is not the best solution, but it definitely beats 3.14 in this code. Defining a constant is the easiest solution. – Alexey Godin Mar 22 '19 at 16:50