approximate PI help

This is a discussion on approximate PI help within the C Programming forums, part of the General Programming Boards category; hello all my current project is a program to approximate π (<sorry: its ugly but is pi) up to 1/99 ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    11

    Question approximate PI help

    hello all

    my current project is a program to approximate π (<sorry: its ugly but is pi) up to 1/99
    w/ the usually taylor series representation, eg (π=4x(1-1/3+1/5...etc)

    so far my code is
    Code:
    #include<stdio.h>
    #include<stdafx.h>
    #include"conio.h"
    #include<math.h>
    
    int main()
    {
    double n;
    int i=0;
    
      while(i<100)
      {
      n=(((-1)^i)/(2*i+1));
      i++;
      }
    
    printf(" PI is &#37;lf (debug %d i)", n, i);
    getch();
    return (0);
    }
    which runs but my n seems to stay 0 always... i was wondering if im approaching the problem too simply (quite simple myself) or if i am screwing up somthing (possibly ^ , i tried pow(x,x) config but that ran me into syntax.... anyways thank you for help in advance.
    Last edited by musifuger; 04-01-2008 at 05:57 PM. Reason: spacing

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    In C, ^ is bitwise XOR. You indeed need pow().

    Furthermore, you aren't faithfully representing the summation; there are two errors. Try using a debugger to step through each line while watching the variables.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    11
    thanks zx

    i am still a bit unsure of this, ive seen this stuff only on forums and never been taught in class.

    is the pow() done in the code in the loop or is it done in the printf (eg... printf("%f is power or 4 to the 5th", pow(4,5));...) as ive seen examples of both but not usually explained well.

    and would it need to be used as a function (im thinking not as functions arnt usually neccessary just easier to keep track of code and reuse.

    i seem to become lost easily on the simplest things and im wondering if its my fault or my teachers :P

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need it anywhere you plan to raise something to a power. And of course it's a function.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Quote Originally Posted by musifuger View Post
    im wondering if its my fault or my teachers :P
    Given the fallout when some of those teachers post on this very forum, it's fair to wonder.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    11
    This one i fail syntax because pow, i still cant find an example that clears it up for me.
    Code:
    #include<stdio.h>
    #include<stdafx.h>
    #include"conio.h"
    #include<math.h>
    
    
    int main(void)
    {
    double n=0.0;
    int i=0;
    
    	while(i<100)
    	{
    	n= pow((-1), i)/((2*i)+1);
    	++i;
    	}
    
    printf(" PI is %lf (debug %d i)", n, i);
    getch();
    return (0);
    }

    and i have tried to play w/ it this way also but i get "Error 1 error C2296: '/' : illegal, left operand has type 'void' f:\microsoft visual studio 8\projects\7 pi\7 pi\7 pi.cpp 21"

    and im sure its because i dont understand the pow also...
    Code:
    #include<stdio.h>
    #include<stdafx.h>
    #include"conio.h"
    #include<math.h>
    
    void
    pow1(double x, int i)
    {
    	pow(x, i);
    }
    
    int main(void)
    {
    double n=0.0 ,x=-1;
    int i=0;
    
    	while(i<100)
    	{
    	n= pow1(x, i)/(2*i+1);
    	++i;
    	}
    
    printf(" PI is %lf (debug %d i)", n, i);
    getch();
    return (0);
    }
    i have seen a few posts talking about making the pow thru a loop, and not sure if this is easier i have tried to get it going but sadly have lost my code for this as of now: /

    sorry to be thick of skull but any help would help... also for the debug im still getting 0 for n even w/ a print for 'n' inside the while loop.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You're better off not having the pow1 function (especially since it's declared wrong for the purpose intended).

    Code:
    	n= pow1(x, i)/(2*i+1);
    With that snippet above, each time through the loop, you are setting "n" to the latest and greatest calculation. I presume you don't want to do this, but you want to actually adjust "n" by the amount you calculate.

    Also, don't you need to flip x each loop too?

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Todd Burch View Post
    You're better off not having the pow1 function (especially since it's declared wrong for the purpose intended).

    Code:
    	n= pow1(x, i)/(2*i+1);
    With that snippet above, each time through the loop, you are setting "n" to the latest and greatest calculation. I presume you don't want to do this, but you want to actually adjust "n" by the amount you calculate.

    Also, don't you need to flip x each loop too?

    Todd
    I'm not familiar with pow1 either, although there is a powl. Todd's right about the n, but not the x, since we do actually want to calculate (-1)^i. Of course, any sane person wouldn't use pow, but would flip instead....

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    11
    ahh i believe it should be n+=(pow(-1, i)/(2*i+1));
    and yes -1^n should alternate the sign as is done in a taylor series (looks for old calc book)

    i guess its not only pow im wondering about now as in my class has been guessing over most of our structure for c this semester.

    am i correct in thinking i can use pow the way i have it in the line above?
    and as to the pow1 i thought they were just names given to different prototypes? i hope they arnt keywords or im much more lost than already thought.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's how pow works, yes.

    powl returns a long double, instead of just a plain old double, in case you have big numbers.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    11
    thanks tabstop
    i shouldnt need powl for this application.

    i am getting an error of "Error 1 error C2668: 'pow' : ambiguous call to overloaded function f:\microsoft visual studio 8\projects\7 pi\7 pi\7 pi.cpp 16"

    going to sleep since i doubt i can fix this tired, again thanks for th help.... and ill post my code again just so you can see what else it could be doing.

    Code:
    int main()
    {
    double n=0.0;
    int i=0;
    
    	while(i<100)
    	{
    	n+=(pow(-1, i)/(2*i+1)); 
    	++i;
    	}
    
    printf(" PI is &#37;lf (debug %d i)", n, i);
    getch();
    return (0);
    }

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    >> i am getting an error of "Error 1 error C2668: 'pow' : ambiguous call to overloaded function [...]"

    pow is declared in several ways. The compiler can choose more than one of them and be correct, which is ambiguous, and thus, you get the error.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's because you're using a C++ compiler: instead of having different names (like pow and powl), it uses one overloaded name (there's several different pow functions). But: none of them take integers. So, change -1 to -1.0. (Alternatively: use a C compiler.)

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    do not use pow function at all

    on step i you have value of (-1)^i

    just multiply it on (-1) to get the value of (-1)^(i+1) - you do not need power here...
    even simple var which will be fliiped on each iteration from x=-x; will be enough
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    11
    sweet the -1 to -1.0 did it, thanks so much, i figured it would a simple fix that i wouldnt have guessed in many years.

    now i just gotta play w/ the formula to get half a bit closer to actual pi.

    thanks alot

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pi - Hm, somethign is not right here.
    By MadnessRed in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2008, 01:07 PM
  2. PI Equation in C
    By wallysworld in forum C Programming
    Replies: 13
    Last Post: 10-23-2006, 08:12 PM
  3. Pi Calculation
    By EvilGuru in forum C Programming
    Replies: 2
    Last Post: 05-02-2005, 04:25 AM
  4. Pi and the standard library
    By FOOTOO in forum C Programming
    Replies: 7
    Last Post: 04-15-2005, 11:23 AM
  5. C for PI
    By Lynux-Penguin in forum C Programming
    Replies: 13
    Last Post: 04-28-2002, 07:37 PM

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