|
-
Nov 12th, 2011, 09:29 AM
#1
Thread Starter
Hyperactive Member
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:
Public ReadOnly Property e(ByVal Optional limit As Int32 = 10) As Double
Get
Dim result As Double
For i As Int32 = 0 to limit
result= result + 1/i
Next
Return result
End Get
End Property
Public ReadOnly Property Pi(ByVal Optional limit As Int32 = 2) As Double
Get
'Pi = (6(1/1^2 + 1/2^2 + 1/3^2 + 1/4^2 + ...))^(1/2)
Dim tmp As Double = 0
For i As Int32 = 0 To limit
tmp = tmp + (1/i)^2
Next
Return (6*tmp)^(1/2)
End Get
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..
-
Nov 12th, 2011, 10:09 AM
#2
Thread Starter
Hyperactive Member
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:
Public ReadOnly Property e(ByVal Optional limit As Int32 = 25) As Double
Get
Dim result As Double = 1.0 'Begin with the first 1.
For i As Int32 = 1 to limit
Dim tmp As Double = 1
For ii As Int32 = 1 to i
tmp = tmp*ii
Next
result = result + 1/(tmp)
Next
Return result
End Get
End Property
Also I haven't figured out Pi yet.
-
Nov 12th, 2011, 10:47 AM
#3
Thread Starter
Hyperactive Member
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:
Public ReadOnly Property Pi(ByVal Optional limit As Int32 = 25) As Double 'Pi = (6(1/1^2 + 1/2^2 + 1/3^2 + 1/4^2 + ...))^(1/2) Get Dim result As Double Dim tmp As Double = 0.0 For i As Int32 = 1 To limit tmp = tmp + (1/i)^2 Next result = (6*tmp)^(1/2) Return result End Get End Property
This produces: 3.10392339170058.. close but nowhere near correct.
-
Nov 12th, 2011, 01:00 PM
#4
Thread Starter
Hyperactive Member
Re: Calculating Pi and e
 Originally Posted by DavesChillaxin
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+
-
Nov 12th, 2011, 02:46 PM
#5
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.
-
Nov 12th, 2011, 03:05 PM
#6
Re: Calculating Pi and e
I tried one of the formulas from Wikipedia and it seems to work quite well.
vb.net Code:
Function PI(ByVal limit As UInteger) As Double Dim Result As Double = 0 For i As UInteger = 0 To limit Result += 1 / (16 ^ i) * ((4 / (8 * i + 1)) - (2 / (8 * i + 4)) - (1 / (8 * i + 5)) - (1 / (8 * i + 6))) Next Return Result 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.
-
Nov 12th, 2011, 05:54 PM
#7
Thread Starter
Hyperactive Member
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?
-
Nov 12th, 2011, 06:04 PM
#8
Thread Starter
Hyperactive Member
Re: Calculating Pi and e
 Originally Posted by BlindSniper
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.
-
Nov 12th, 2011, 08:04 PM
#9
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.
-
Nov 13th, 2011, 02:34 AM
#10
Re: Calculating Pi and e
 Originally Posted by DavesChillaxin
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.
-
Nov 13th, 2011, 07:36 AM
#11
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.
-
Nov 15th, 2011, 01:01 PM
#12
Thread Starter
Hyperactive Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|