Like Tree1Likes

Approximating PI using the Taylor Series - Help!

This is a discussion on Approximating PI using the Taylor Series - Help! within the C++ Programming forums, part of the General Programming Boards category; yes, I can clearly see that, but there is no explanation of how that formula does what std10093 says it ...

  1. #16
    Registered User
    Join Date
    Oct 2006
    Posts
    2,493
    yes, I can clearly see that, but there is no explanation of how that formula does what std10093 says it does. are you calculating the absolute relative error of this instance only, or of floating point operations in general? all I see is a couple of strings of mathematical operations, with no reference to why they're appropriate in this case, or where they came from. I can't even google it without a little more context.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    Oh, they're pretty standard for numerical calculations, and they make sense, too.
    They are used in general to see how well your approximation compares to the real number. Obviously very important when approximating numbers to see that you actually don't get a half-baked approximation.
    Approximation error - Wikipedia, the free encyclopedia
    Last edited by Elysia; 08-08-2012 at 03:42 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Elkvis these formulas come from the lesson numerical analysis of the theoretical informatics we study at DIT.As Elysia stated and you can see in the link,these formulas are in general and give the information i provided for an instance,not in general.The errors i posted were refering to the value you calculated by doing it 40 million times and for the actual value of π.However i would be very glad to see how the code that calculated this looks like.If you have problem posting it,you could send me a pm,please

  4. #19
    Registered User
    Join Date
    Oct 2006
    Posts
    2,493
    pm sent

    edit: and it was actually 40 billion times
    Last edited by Elkvis; 08-08-2012 at 08:49 AM.

  5. #20
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Thank you very much.I appreciate it.Yes you are right about the billion

  6. #21
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,332
    the taylor series is a really lousy approximation for pi though.
    That specific instance is really bad.

    Other forms of the "Taylor Series" work much better.

    Soma

  7. #22
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by phantomotap View Post
    That specific instance is really bad.

    Other forms of the "Taylor Series" work much better.

    Soma
    Could you suggest one just to compare the results?

  8. #23
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    The one that I am familiar with is Pi/4 = 1 - 1/3 + 1/5 - 1/7 + ... with the denominators staying odd.

  9. #24
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I tried what whiteflags said and got the following results :

    with criterion to stop
    Code:
    while (current > 1.0e-10);
    i had to calculate 1073741825 and π=3.14159
    With the one i suggest before i calculated 100000 and got p=3,14158 (should be 3.14159..)

    Then i changed the criterion to stop to
    Code:
    while (current > 1.0e-15);
    and got the same results with whiteflags's formula as with the previous criterion(that was not expected)
    and with the one i had suggested the result is
    Code:
    Summed 31622777 terms, pi is 3.14159
    Of course i have no problem posting the code,but this post ,supposedly,is for a homework of someone(who has lost interest too soon how ever),so i would ask what are the results for you?

  10. #25
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Not really sure what you're doing.
    Code:
    #include <iostream>
    #include <sstream>
    int main(int argc, char *argv[])
    {
        if (argc != 2)
        {
            std::cout << "Compute pi with the expansion of arctan 1\n";
            std::cout << "usage: exe [terms]\n";
            return 0;
        }
    
        std::stringstream args(argv[1]);
        int terms;
        args >> terms;
        bool toggle = false;
        double taylor = 1.0;
        for (int i = 3; i < terms; i+=2)
        {
            if (toggle)
                taylor += 1./i;
            else
                taylor -= 1./i;
    
            toggle = !toggle;
        }
        double pi = 4 * taylor;
        std::cout << pi << "\n";
    }
    // my output:
    // 3.14139
    With this I get 4 digits of precision after 10000 terms.

  11. #26
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You get four digits precision you said with 100000,but you did not state if these are significant or decimal.I think you mean significant,if your output is that in line 30(with 10000 terms).
    So i think that is clear that with your formula we have to calculate more terms to achieve the same precision as in 'my' formula.However your formula does less operations.But if we consider that the formula i posted has to calculate far less terms,i think that mine is better.

    EDIT - > you may give me a cookie again

  12. #27
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,332
    Could you suggest one just to compare the results?
    I'm sorry. I actually can't.

    What I know of this stuff comes from research about fast approximations, but it has been years since I read the paper.

    I don't remember the exact numbers or anything, but I noticed it because it was orders of magnitude difference in calculations which obviously struck me as being very significant.

    [Edit]
    I'm sure they are only a search away in any event.

    Probably something along the lines of "a faster Taylor series for pi".
    [/Edit]

    Soma
    Last edited by phantomotap; 08-08-2012 at 05:08 PM.

  13. #28
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by phantomotap View Post
    I'm sorry. I actually can't.

    What I know of this stuff comes from research about fast approximations, but it has been years since I read the paper.

    I don't remember the exact numbers or anything, but I noticed it because it was orders of magnitude difference in calculations which obviously struck me as being very significant.

    Soma
    Oh,that's ok Whiteflags suggested a formula by the way

  14. #29
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Quote Originally Posted by std10093 View Post
    EDIT - > you may give me a cookie again
    I don't trust your research this time. You did not show your implementation and I don't know the significance of what I saw. After 1 million terms I definitely get something that looks like a lot like pi. You might want to experiment more to find the differences for yourself.

    I don't care - would just do 4 * arctan(1.0) anyway.

  15. #30
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Nobody ask for it.So no cookie this time.As for the arctan it is true.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Taylor series regarding E^x
    By rafacnsoccer in forum C++ Programming
    Replies: 8
    Last Post: 05-05-2010, 11:08 AM
  2. taylor series using only stdio.h
    By b0ss139 in forum C Programming
    Replies: 4
    Last Post: 07-25-2009, 10:32 PM
  3. help on c programming involving taylor series
    By harlow23 in forum C Programming
    Replies: 4
    Last Post: 05-04-2007, 10:03 PM
  4. taylor series using only stdio.h
    By faruque in forum C Programming
    Replies: 1
    Last Post: 02-13-2003, 11:50 AM
  5. taylor series expansion
    By noor_mirza in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2002, 10:02 PM

Tags for this Thread


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