Skip to main content
gamedev.net
🔒 Locked

Calculating PI

Started by ZomeonE Oct 16, 2000 at 11:51 AM 24 replies 1k views
Original Post
ZomeonE
ZomeonE
Hi! I found out during my math class today a way to calculate PI: 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
Mezz
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.

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
ZomeonE
ZomeonE
Well I understand it so far, it's what I've done, but I need some help which variable types and so on to use. Preferably some C code. Maybe I didn't make myself clear in the first post

Edited by - ZomeonE on October 16, 2000 1:07:45 PM
ZomeonE
ZomeonE
Wohoo!

I wrote a program on my calculator. After a 1000 for loop it gave 3.1406, cool now I need to know what variable type to use on the PC It''s like 10000000 times faster

-----------------------------
My Site: HERE
ZomeonE
ZomeonE
quote:
Original post by Anonymous Poster

ok, i''m probably doing your homework for you but oh well:




No, it''s not homework, it''s todays project
Thanks anyway. Is sqrtf() for floats? Maybe I sqrt uses ints?


-----------------------------
My Site: HERE
ZomeonE
ZomeonE
I've got it to work now, thnx

BUT, I need some more accuracy.


Edited by - ZomeonE on October 16, 2000 1:51:52 PM
Mithrandir
Mithrandir
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.
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.
ZomeonE
ZomeonE
Cool. It''s a shame no big data types are available for PC''s and that it is so hard to make them...

-----------------------------
My Site: HERE
smidge_tech
smidge_tech
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
========
--Mr Smidge
Martee
Martee
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
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Graylien
Graylien
Here's some Java code based on a quadratically convergent formula for computing π .

            
public class ComputePI {
public static void main(String[] args) {
double a, b, c, d, e, f;
a = 0;
b = 1;
c = 1 / Math.sqrt(2);
d = 0.25;
e = 1;
f = 0;
for(int i=1; i<Integer.parseInt(args[0],10); i++) {
a = b;
b = (b + c) / 2;
c = Math.sqrt(c * a);
d = d - e * (b - a) * (b - a);
e = 2 * e;
f = (b + c) * (b + c) / (4 * d);
System.out.println(f);
}
}
}


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
------When thirsty for life, drink whisky. When thirsty for water, add ice.
Zipster
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.
Graylien
Graylien
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.

------When thirsty for life, drink whisky. When thirsty for water, add ice.
Ironblayde
Ironblayde
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:


###############################

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
"Your superior intellect is no match for our puny weapons!"
ZomeonE
ZomeonE
Thnx Ironblayde.

No, the teacher was just explaining the use of the sum thing, or large sigma, or what it is called and he just wrote that pi formula. It was a boring lesson really, cause I learned how to use them like 5 years ago!


_____________________________
My Site: HERE
microdot
microdot
Anyone interested in info on PI, or anything involving math, would do well to go to "http://mathworld.wolfram.com/". This is about the ultimate math resource I''ve found on the net yet.
<span class="smallfont">That is not dead which can eternal lieAnd with strange aeons even death may die.   -- "The Nameless City" - H. P. Lovecraft</span>
Ironblayde
Ironblayde
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:


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 . 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
"Your superior intellect is no match for our puny weapons!"
Stoffel
Stoffel
Iron:
just had to comment, that''s some pretty creative ASCII math drawing on your part.
Ratman
Ratman
3.14159265358979323

Is how many digits I have PI memorized to Heres a good joke:

What do you get if you take divide the circumference of a pumpkin by it''s diameter?





Pumpkin Pi(e)!

heh, ok I''ll go home now

---------------
http://ratfest.org
ChaosEngine
ChaosEngine
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
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.