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
Printable View
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
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))
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
I think it's ked's formula that's a bit off, try
VB will always round it's numbers (I can't remember what to tho')Code:For n = 0 To 100000
pii = pii + (4 / ((4 * n) + 1)) - (4 / ((4 * n) + 3))
Next
Debug.Print pii
I'll do you something that'll give you more s.f.
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/showthrea...als#post133046
Thanks Kedaman - absolutely brilliant code
Rob
The link moved to:
http://www.vbforums.com/showthread.p...als#post133046
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))
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:
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.VB Code:
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
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/~...ns/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
What? What? It's easier than some Russian words...! :DQuote:
Originally Posted by Rassis
Btw, it's a very didactic and nice spreadsheet.
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...Quote:
Originally Posted by Rassis