Click to See Complete Forum and Search --> : Pi
THEROB
Mar 2nd, 2001, 07:40 AM
Does anyone know of a program that will find PI to any number of digits.
So the user would just type in 50, and returned you be the 3.14........to fifty digits.
Thanks
Rob
kedaman
Mar 2nd, 2001, 09:05 AM
pi is 4*arctangens(1) and you could use this series to evaluate it:
pi = sumof[n=0 to infinity]( 4/(2*n+1) - 4/(2*n+2))
THEROB
Mar 2nd, 2001, 09:49 AM
Is there something wrong with my code:
For n = 0 To 100000
pii = pii + (4 / (2 * n + 1) - 4 / (2 * n + 2))
Next
Debug.Print pii
I get 2.77257872236476 !!!!!!!
This is still only 14 digits - no matter how n increases VB will still round it to 14 digits - is there any formula or way to get any amount of digits
Thanks
Rob
Sam Finch
Mar 2nd, 2001, 10:06 AM
I think it's ked's formula that's a bit off, try
For n = 0 To 100000
pii = pii + (4 / ((4 * n) + 1)) - (4 / ((4 * n) + 3))
Next
Debug.Print pii
VB will always round it's numbers (I can't remember what to tho')
I'll do you something that'll give you more s.f.
kedaman
Mar 2nd, 2001, 11:42 AM
oh yeah, sorry about that. i was thinking there was 2 between each term, but i had 2 terms so there's 4 :o
well this remembered of an old thread about decimal numbers in strings, so i looked it up:
http://forums.vb-world.net/showthread.php?s=&postid=133046&highlight=pi+decimals#post133046
THEROB
Mar 2nd, 2001, 04:38 PM
Thanks Kedaman - absolutely brilliant code
Rob
THEROB
Jun 6th, 2006, 10:46 AM
The link moved to:
http://www.vbforums.com/showthread.php?s=&postid=133046&highlight=pi+decimals#post133046
BramVandenbon
Jun 8th, 2006, 03:50 PM
About that rounding of numbers. I'm not sure how it works in VB.NET these days but in C++ and C# you can solve it like this.
In C# I do it like this:
pii = pii + (4.0f / ((4.0f * n) + 1.0f)) - (4.0f / ((4.0f * n) + 3.0f))
I think back in C++ I did it like
pii = pii + (4.0 / ((4.0 * n) + 1.0)) - (4.0 / ((4.0 * n) + 3.0))
krtxmrtz
Jun 9th, 2006, 09:42 AM
To approach this from a totally different angle, you could try the so-called Montecarlo method, based on statistical sampling.
If you inscribe a circle in a square -so that the sides of the square are tengent to the circle- then if you randomly "throw" a point inside the square, the probability that it lands inside the circle is the ratio of the areas:
(Pi * r2) / (4 * r2)
so that by sampling a large number of points the ratio of those that fall in to the total number tends to Pi / 4.
For example:
Randomize
'Initialize counters
Total = 0
In = 0
'Pick any large number for N
N = 10000
For i = 1 To N
x = Rnd
y = Rnd
Total = Total + 1
'Compute (squared) distance to origin
Dist2 = x*x + y*y
'Check if the point is inside (compare with radius = 1)
If Dist2 <= 1 Then In = In + 1
Next
Pi = 4 * In / Total
The shame is this method has a very slow convergence so it's not particularly useful for this calculation, but sometimes the MC method is the only possible or more convenient way to go.
Rassis
Jun 9th, 2006, 12:53 PM
I attach an EXCEL file that I use as an introduction to simulation techniques in my classes. It is just a supplement to what Krtxmrtz (seems impossible to pronounce!) said in his last post. It might be interesting especially for those unfamiliar with simulation techniques. It is an experiment attributed to George Buffon (1707 – 1788) http://www-groups.dcs.st-and.ac.uk/~history/Mathematicians/Buffon.html an ancient French scientist.
One sheet presents a fixed number of iterations depending on the number of coded cells (2,000 precisely). The other can be run for any number you might want in order to increase statistical significance, but needs additional software for this purpose (I suggest Crystal Ball – there is a link in the attached file) or you program a macro yourself for the repetitions (adapting the code given by Krtxmrtz).
Enjoy.
Rui
krtxmrtz
Jun 9th, 2006, 03:11 PM
...Krtxmrtz (seems impossible to pronounce!) ...
What? What? It's easier than some Russian words...! :D
Btw, it's a very didactic and nice spreadsheet.
Rassis
Jun 9th, 2006, 06:14 PM
Fiquei contente por ter gostado das minhas aplicações. Uso sempre este design em formação desde há muitos anos e os formandos gostam. É já uma rotina! Gracias.
Rui
krtxmrtz
Jun 10th, 2006, 04:18 AM
Fiquei contente por ter gostado das minhas aplicações. Uso sempre este design em formação desde há muitos anos e os formandos gostam. É já uma rotina! Gracias.
Rui
Tant de bo que jo hagues tingut la possibilitat d'estudiar matemàtiques d'aquesta manera quan anava a l'escola, en lloc d'haver de llegir llibres avorrits...
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.