Problem with implementing Liebniz series

This is a discussion on Problem with implementing Liebniz series within the C Programming forums, part of the General Programming Boards category; Hi All, I'm trying to write a small program that approximates PI using Liebniz series (4*Pi = 1 - 1/3 ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    3

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,825
    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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    3
    Glad u tried it out

    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

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,825
    How about actually posting the answers you get?
    Or try with double instead.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by C_Necessity
    Glad u tried it out

    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).

    [edit]
    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
    Last edited by Dave Evans; 06-10-2005 at 03:06 PM.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    3
    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

  7. #7
    Jez
    Jez is offline
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    >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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21