🔒 Locked
Calculating PI
Advertisement
Original Post
OK, well, you are summing i/k^2 from one to infinity - now you can''t do that precisely in a computer program, but you can use a loop construct and perform the calculation to some arbitrary degree of accuracy.
something like:
Enter loop
calculate 1/loopnumber^2
add this to a holder variable
End loop
Then, when you''ve done that you can multiply by 6 and take the square root, giving you Pi.
Hope that made some sense.
-Mezz
something like:
Enter loop
calculate 1/loopnumber^2
add this to a holder variable
End loop
Then, when you''ve done that you can multiply by 6 and take the square root, giving you Pi.
Hope that made some sense.
-Mezz
Here''s a faster method:
take the sum of the alternating infinite series:
4/n for every 2 n.
that means you take:
4/1 - 4/3 + 4/5 - 4/7 + 4/9 ... etc.
you can actually combine that to make:
4/n - 4/(n+2), increment n by 4.
Why does this work? I have no clue... hehee.
as for more accuracy, you are limited by what the current machine you are using can offer you. If you are using FLOATs, that is only 32 bits, and not too accurate. I would suggest increasing the bits to at least 64 by using a DOUBLE, or, if your machine suports it, use LONG DOUBLE''s, which is 80bits. (MSVC5''s documentation says it doesnt support it, but im not sure).
The final option would be to make your own custom fixed or floating point format, by chaining multiple WORDs or DWORDs together using assembly language.
It is a very complex task to do, and probably beyond the scope of most people''s talents, but every once in a few years, some whacko CS guys take the latest hardware and make a custom float format (they usually use something insane like a one million bit float, heh) and make the most accurate representation of PI, ever.
just use DOUBLE''s or LONG DOUBLE''s
===============================================
If there is a witness to my little life,
To my tiny throes and struggles,
He sees a fool;
And it is not fine for gods to menace fools.
take the sum of the alternating infinite series:
4/n for every 2 n.
that means you take:
4/1 - 4/3 + 4/5 - 4/7 + 4/9 ... etc.
you can actually combine that to make:
4/n - 4/(n+2), increment n by 4.
Why does this work? I have no clue... hehee.
as for more accuracy, you are limited by what the current machine you are using can offer you. If you are using FLOATs, that is only 32 bits, and not too accurate. I would suggest increasing the bits to at least 64 by using a DOUBLE, or, if your machine suports it, use LONG DOUBLE''s, which is 80bits. (MSVC5''s documentation says it doesnt support it, but im not sure).
The final option would be to make your own custom fixed or floating point format, by chaining multiple WORDs or DWORDs together using assembly language.
It is a very complex task to do, and probably beyond the scope of most people''s talents, but every once in a few years, some whacko CS guys take the latest hardware and make a custom float format (they usually use something insane like a one million bit float, heh) and make the most accurate representation of PI, ever.
just use DOUBLE''s or LONG DOUBLE''s
===============================================
If there is a witness to my little life,
To my tiny throes and struggles,
He sees a fool;
And it is not fine for gods to menace fools.
The problem with putting the equation onto PC is the infinity sign, but look at this:
As k increases, 1/(k*k) tends to 0 (obviously). So it would be better to start 1/(k*k) as 1 and decrease that by whatever degree of accuracy you want.
As for those of you who know how to differentiate and integrate, read this - the sum of all values of 1/(k*k) is equal to the integral between 1 and infinity. So, integrate it with respect to k and you get ((k to the power of -3) / -3). When k is 1, you get -1/3, plus an infinitely small value with coefficients... erh, oh damn I''ve lost it. Oh well...
But my first paragraph was right anyway...
========
Smidge
www.smidge-tech.co.uk
========
As k increases, 1/(k*k) tends to 0 (obviously). So it would be better to start 1/(k*k) as 1 and decrease that by whatever degree of accuracy you want.
As for those of you who know how to differentiate and integrate, read this - the sum of all values of 1/(k*k) is equal to the integral between 1 and infinity. So, integrate it with respect to k and you get ((k to the power of -3) / -3). When k is 1, you get -1/3, plus an infinitely small value with coefficients... erh, oh damn I''ve lost it. Oh well...
But my first paragraph was right anyway...
========
Smidge
www.smidge-tech.co.uk
========
Another way to calculate PI:
You have a circle of radius 1, inside a square of side length 2. You randomly plot a whole bunch of points. Calculate if they are inside or outside the circle. The ratio of inside points to total points is (if you plot enough points) PI/4, because the area of the circle is PI, and the area of the square is 4. I think that''s how you do it...
==================
/* todo: insert cool sig */
Martee
Magnum Games.NET
You have a circle of radius 1, inside a square of side length 2. You randomly plot a whole bunch of points. Calculate if they are inside or outside the circle. The ratio of inside points to total points is (if you plot enough points) PI/4, because the area of the circle is PI, and the area of the square is 4. I think that''s how you do it...
==================
/* todo: insert cool sig */
Martee
Magnum Games.NET
Here's some Java code based on a quadratically convergent formula for computing π .
Usage:
java ComputePI v
(where v is the number of loops. More loops doesn't always mean more precision though. Try it and see. I use v = 4, which translates to 3 iterations, and that's as good as Java will get it on my machine.)
And yes, I know Java sucks for numerical computation.
[edit] I don't know about you, but IE 5.5's rendering of the PI symbol is pretty poor...
Edited by - Graylien on October 16, 2000 3:29:56 PM
|
Usage:
java ComputePI v
(where v is the number of loops. More loops doesn't always mean more precision though. Try it and see. I use v = 4, which translates to 3 iterations, and that's as good as Java will get it on my machine.)
And yes, I know Java sucks for numerical computation.
[edit] I don't know about you, but IE 5.5's rendering of the PI symbol is pretty poor...
Edited by - Graylien on October 16, 2000 3:29:56 PM
quote:
Original post by Zipster
Another way to get PI:
Haha, thats impossible. PI is irrational, therefore you cant GET PI!! You can only approximate to ''x'' number of significant digits, but you will never truely get PI.
Gee...thanks Zipster. You''re such a genius. As you can see by the comments below, nobody knew that PI is irrational. You''ve saved us all alot of computing time.
quote:
Original post by Mezz
OK, well, you are summing i/k^2 from one to infinity - now you can''t do that precisely in a computer program, but you can use a loop construct and perform the calculation to some arbitrary degree of accuracy.
quote:
Original post by Mithrandir
as for more accuracy, you are limited by what the current machine you are using can offer you.
ZomeonE: Just out of curiosity, did you guys see Euler's proof of that fact in your class, or did you just see the result? The proof is pretty neat. 
Anyway, just for fun, a few other interesting formulas that yield pi:
where I is an integral and S is a summation.
-Ironblayde
Aeon Software
"Down with Nazrix!" --pouya --Ironblayde --firahs --Konrad --The Senshi
Edited by - Ironblayde on October 17, 2000 2:14:38 AM
Anyway, just for fun, a few other interesting formulas that yield pi:
###############################
1 dx
pi = 4 * I( ------ )
0 1 + x²
###############################
infinity (-1)^n
pi = 4 * S ( -------- )
0 2n + 1
###############################
pi = 16 arctan (1/5) - 4 arctan (1/239)
###############################
where I is an integral and S is a summation.
-Ironblayde
Aeon Software
"Down with Nazrix!" --pouya --Ironblayde --firahs --Konrad --The Senshi
Edited by - Ironblayde on October 17, 2000 2:14:38 AM
ZomeonE: Ah, I see. If you''re just learning summations in your class than the proof of that fact probably uses things you haven''t seen yet. That result, though, is actually a specific case of what''s called the Riemann zeta-function:
There are actually a number of interesting formulas that come out of this (Euler discovered a lot of them
. The cool thing is that this converges if and only if z>1. I just think that''s interesting... that if you use z=1, this will grow out to infinity, albeit rather slowly, but if you use something like z=1.00001, it converges to a finite value. Math is awesome.
-Ironblayde
Aeon Software
"Down with Nazrix!" --pouya --Ironblayde --firahs --Konrad --The Senshi
infinity
R(z) = S ( 1 / (k^z))
k=0
There are actually a number of interesting formulas that come out of this (Euler discovered a lot of them
-Ironblayde
Aeon Software
"Down with Nazrix!" --pouya --Ironblayde --firahs --Konrad --The Senshi
slightly off topic, but if you really want to mess with your head, watch "pi"; a film that won best directing at sundance in 97 (i think?)
It''s about this guy who is a maths genius and is looking for numerical patterns in the world.
"That's not a bug, it's a feature!"
--me
It''s about this guy who is a maths genius and is looking for numerical patterns in the world.
"That's not a bug, it's a feature!"
--me
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement
How would I implement this into a simple dos program. I tried but got strange results dealing with doubles. Please give me some tips, or other ways to calculate PI. Just a fun little 10 minute project