-1

I'm getting a wrong solution for this series: (-1/4)^(n+1)*(z-1)^n

For |z-1|<4 should the series tend to converge to -1/3+z

For z=0.5 should be the solution -2/7, but if i try to plot with c, the result is 0...

Here is my code:

#include <stdio.h>
#include <math.h>

int main(){
    double sum=0;
    int n;
    for(n=0;n<=100000;n++){
        sum+=pow((-1/4),(n+1)) * pow((0.5-1),n);
    }
    printf("sum= %f\n",sum);
}
Federico J.
  • 15,388
  • 6
  • 32
  • 51

3 Answers3

0

Problem right here:

    sum+=pow((-1/4),(n+1)) * pow((0.5-1),n);

-1 is an integer literal, and so is 4; hence, (-1/4) is -0, and not -0.25 (which was probably what you wanted to use). Use floating point literals like -1.0 if you want them in C!

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
0

-1/4 will result to 0 as its an integer division, use floats instead:

     (float)-1/4
thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
0

1/4 refers to the euclidian division hence 0 obtained.
Use sum+=pow((-1.0/4.0),(n+1)) * pow((0.5-1),n); and you get the good results sum= -0.285714

coincoin
  • 4,595
  • 3
  • 23
  • 47