# Problem with implementing Liebniz series

• 06-10-2005
C_Necessity
Problem with implementing Liebniz series
Hi All,

I'm trying to write a small program that approximates PI using Liebniz series (4*Pi = 1 - 1/3 + 1/5 - 1/7 + 1/9 - ...)

I think the implementation logic is correct ( though not efficient), the problem I am faced with is that any time i run the program by providing the number of terms of liebniz series required, the output is still 0.000. I have tried every diognostic method in my limited knowledge. Plz check out the code n see what is not right.
Code:

```#include <stdio.h> int main() {         long double pi, v, temp2=0;         int i=1,n,flg=1,count=0;                 printf("Enter the number of terms of Liebniz's series to use: ");         scanf("%d",&n);                 while(count!=n)         {                 if(i%2!=0){                         count++;                         /* Problem HeRe */                         v = 1.0/i;                         printf("\nCount%d",count);                         printf("\t%d",i);                         printf("\t%Lf",v);                         if(flg==1){                                 temp2+=v;                                 flg=0;                         }                         else if (flg==0){                                 temp2-=v;                                 flg=1;                         }                 }                 i++;         }         pi=4*temp2;         printf("The Approximation of Pi = %Lf",pi);         return 0; }```

Thanx n looking forward to ur insights
• 06-10-2005
Salem
Dunno
Code:

```\$ gcc -Wall -W -ansi -pedantic  hello.c \$ ./a.out Enter the number of terms of Liebniz's series to use: 10 Count1  1      1.000000 Count2  3      0.333333 Count3  5      0.200000 Count4  7      0.142857 Count5  9      0.111111 Count6  11      0.090909 Count7  13      0.076923 Count8  15      0.066667 Count9  17      0.058824 Count10 19      0.052632The Approximation of Pi = 3.041840```
This is compiled with gcc on Linux.
What are you using?
• 06-10-2005
C_Necessity

I'm using gcc on WinXP. In my output the third column shows 0.0000 or some weird negative values. (The printf statements r there only for diagnostic purposes). What do u think is the problem?

Thnx for the quick response
• 06-10-2005
Salem
• 06-10-2005
Dave Evans
Quote:

Originally Posted by C_Necessity

I'm using gcc on WinXP. In my output the third column shows 0.0000 or some weird negative values. (The printf statements r there only for diagnostic purposes). What do u think is the problem?

Thnx for the quick response

Try making the variables double (instead of long double), and use %f instead of %Lf in the printf() statements.

(If you are using mingw --- as in dev-cpp ---, there has been a longstanding problem with printf and long doubles.)

If it works with doubles, then you can still do the calculations with long doubles, and then cast the output to double when printing (I think).

I knew that if I looked hard enough that I could blame it on Microsoft: Here's proof

I use cygwin gcc on my Windows XP box, and it works OK with long doubles (and just about everything else, as far as I have tried).
[/edit]

Regards,

Dave
• 06-15-2005
C_Necessity
Hi all,

Apologies for responding so late; the good news is that i tried using double instead of long double and the program works well. Here is an output using 100,000 terms of the Liebniz Series.

Count99995 199989 0.000005
Count99996 199991 0.000005
Count99997 199993 0.000005
Count99998 199995 0.000005
Count99999 199997 0.000005
Count100000 199999 0.000005The Approximation of Pi = 3.141583

The problem is why doesnt the Long Double type work?

Thnx a lot guys
• 06-15-2005
Jez
>The problem is why doesnt the Long Double type work?
Haven't you read the link in Dave Evans' post? It does explain it.

You'll get a much more accurate answer if you sum the terms backwards, starting with the smallest terms. This is why you currently only get barely 6 decimal places of accuracy.