Results 1 to 12 of 12

Thread: Calculating Pi and e

  1. #1

    Thread Starter
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Calculating Pi and e

    So I've been trying to figure this out for a good minutes, however all my attempts oddly result in infinity. What I'm trying to do is calculate pi and e to a certain level(or limit as I put it).

    Which confuses me.. But I was wondering if anyone here could help shed some light on the matter.


    This is what I have, and both produce "infinity"
    vbnet Code:
    1. Public ReadOnly Property e(ByVal Optional limit As Int32 = 10) As Double
    2.         Get
    3.             Dim result As Double
    4.  
    5.             For i As Int32 = 0 to limit
    6.                 result= result + 1/i
    7.             Next
    8.             Return result
    9.         End Get
    10.     End Property
    11.  
    12.     Public ReadOnly Property Pi(ByVal Optional limit As Int32 = 2) As Double
    13.         Get
    14.             'Pi = (6(1/1^2 + 1/2^2 + 1/3^2 + 1/4^2 + ...))^(1/2)
    15.             Dim tmp As Double = 0
    16.  
    17.             For i As Int32 = 0 To limit
    18.                 tmp = tmp + (1/i)^2
    19.             Next
    20.             Return (6*tmp)^(1/2)
    21.         End Get
    22.     End Property


    NOTE: I know someone will say "oh why don't you just use a constant?" Well that's because I don't want to. I'd rather have something that can produce pi to a certain length(limit), depending on which use for pi is being used.

    Honestly I'm not even sure why this occurs, but perhaps it's the limit of the double value that I'm maxing? Which if it was then I'd suspect an exception would have been thrown rather than producing infinity..
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  2. #2

    Thread Starter
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Calculating Pi and e

    Ok So I figured out what I was doing wrong with e, but still I'm wondering if anyone knows how to "lengthen" the result.

    vbnet Code:
    1. Public ReadOnly Property e(ByVal Optional limit As Int32 = 25) As Double
    2.         Get
    3.             Dim result As Double = 1.0 'Begin with the first 1.
    4.  
    5.  
    6.             For i As Int32 = 1 to limit
    7.                 Dim tmp As Double = 1
    8.  
    9.                 For ii As Int32 = 1 to i
    10.                     tmp = tmp*ii
    11.                 Next
    12.                 result = result + 1/(tmp)
    13.             Next
    14.             Return result
    15.         End Get
    16.     End Property

    Also I haven't figured out Pi yet.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  3. #3

    Thread Starter
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Calculating Pi and e

    Ok, got my equation for Pi to return a value rather than simply "infinity".. however it's not the correct value.

    vbnet Code:
    1. Public ReadOnly Property Pi(ByVal Optional limit As Int32 = 25) As Double
    2.         'Pi = (6(1/1^2 + 1/2^2 + 1/3^2 + 1/4^2 + ...))^(1/2)
    3.  
    4.         Get
    5.             Dim result As Double
    6.             Dim tmp As Double = 0.0
    7.  
    8.             For i As Int32 = 1 To limit
    9.                 tmp = tmp + (1/i)^2
    10.             Next
    11.             result = (6*tmp)^(1/2)
    12.  
    13.             Return result
    14.         End Get
    15.     End Property

    This produces: 3.10392339170058.. close but nowhere near correct.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  4. #4

    Thread Starter
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Calculating Pi and e

    Quote Originally Posted by DavesChillaxin View Post
    This produces: 3.10392339170058.. close but nowhere near correct.
    Well found out if I called Pi(100000000) I get closer to the correct answer, which makes sense. However I'm realizing this method wont work considering I need this calculation to be quick. Calculating what I had 100 million times took roughly 10-15 seconds.

    100 Million times gave me 3.14159264498239
    anything italicized is not correct. Well is to a certain precision but I'd really like to keep it going to the correct 15 digits+
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  5. #5
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Calculating Pi and e

    The series you've used for e comes from the Taylor series and happens to converge very rapidly. You can optimize it a bit, actually--you needlessly re-multiply the first bunch of integers in computing the factorial in your inner loop.

    The series you've used for pi [really pi^2 / 6] does not converge very quickly. A simpler series is actually
    pi/4 = 1 - 1/3 + 1/5 - 1/7 + ...

    though this also converges very slowly. There are many formulas which converge more quickly. See this page for some.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  6. #6
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Calculating Pi and e

    I tried one of the formulas from Wikipedia and it seems to work quite well.
    vb.net Code:
    1. Function PI(ByVal limit As UInteger) As Double
    2.         Dim Result As Double = 0
    3.         For i As UInteger = 0 To limit
    4.             Result += 1 / (16 ^ i) * ((4 / (8 * i + 1)) - (2 / (8 * i + 4)) - (1 / (8 * i + 5)) - (1 / (8 * i + 6)))
    5.         Next
    6.         Return Result
    7.     End Function

    And apparently this formula can be used to calculate the nth digit of pi without calculating the previous digits, but I'm struggling to implement it.
    Last edited by BlindSniper; Nov 12th, 2011 at 03:52 PM.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  7. #7

    Thread Starter
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Calculating Pi and e

    yeah see I was looking all those other formulas, but had no clue where to begin. First and foremost because I don't understand most of the formulas out there.. or rather how to translate them to code.

    I take it though that higher accuracy's of calculating Pi are pretty advanced stuff. I'm learning it's pretty much outside of my knowledge level at this point.


    In addition I've tried another method, one that was a little easer to understand.

    This was Leonhard Euler of 20*arctan(1/7)+8*arctan(3/79). I've found a function online to perform arctan. Hopefully correctly, but when I perform this equation I get 3.17082191042161. So it's not right. Am I doing this one right?
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  8. #8

    Thread Starter
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Calculating Pi and e

    Quote Originally Posted by BlindSniper View Post
    I tried one of the formulas from Wikipedia and it seems to work quite well.
    Which one was this? Cause my math skills are not where they should be to understand most of the equations Wikipedia has to show.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  9. #9
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Calculating Pi and e

    Your 20*arctan(1/7)+8*arctan(3/79) formula is correct, though I can't vouch for your arctan method. In .NET, arctan can be computed using Math.Atan. If there's a particular formula you'd like explained, I (or someone else) would be happy to explain the notation. The derivations for most of these formula are quite advanced. The simplest--the one I mentioned--uses chunks of calculus. The cleanest proof I've seen of the one you started with uses Fourier transforms and Parseval's theorem. The others in the section I linked use magic I haven't gone through. The BBP formula is particularly amazing, and was very surprising when it was announced.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  10. #10
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Calculating Pi and e

    Quote Originally Posted by DavesChillaxin View Post
    Which one was this? Cause my math skills are not where they should be to understand most of the equations Wikipedia has to show.
    It's the BBP forumla.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  11. #11
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Calculating Pi and e

    Oh, by the way, getting the nth digit of pi using the BBP formula uses a "spigot algorithm". Wikipedia has details in this case here.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  12. #12

    Thread Starter
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Calculating Pi and e

    Looks like I've gone way outside my comfort zone here lol

    I haven't taken calculus yet, though I know I can understand it quite easily if described to me in the right way. I did get the BBP formula to work, I'm actually trying to pass the formula through my own methods of solving summations and simplifying expression. Which actually arose problems when converting doubles to strings - that's besides the point though.


    But other than that, all these formulas to calculate Pi completely surpass my current mathematical skill levels I hope I didn't bite off more than I can chew.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width