how can i make Pi with PHP
12-07-2007, 06:29 AM
|
how can i make Pi with PHP
|
Posts: 6,522
Name: Dan
Location: Swindon
|
okay im not joking there is no e i AM talking about the infinant number which is used to calculate many aspects of circles.
So how can i generate Pi with PHP?
*Yes i do already know that if i didnt properly code it to have a cut off it will eventually over load my server and crash (if it didnt have precautions) but i know it would definatly muck up my home testing server
So how can i generate it? i would like to be able to get it into a function like
pi(Number of decimal places i want to show)
So how can i do it?
--edit--
just found this so how can i do that but to how ever many decimal places i want?
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
Last edited by dansgalaxy; 12-07-2007 at 06:32 AM..
|
|
|
|
12-07-2007, 09:34 AM
|
Re: how can i make Pi with PHP
|
Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
|
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
12-07-2007, 06:14 PM
|
Re: how can i make Pi with PHP
|
Posts: 6,522
Name: Dan
Location: Swindon
|
from a comment on the page on php.net for pi() he said it could only go to 20 decimal places?
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
12-07-2007, 06:21 PM
|
Re: how can i make Pi with PHP
|
Posts: 10,688
Name: Steven Bradley
Location: Boulder, Colorado
|
Did someone say Pie? Oh PI. Never mind.
Dan PI = 22/7. You could set a variable PI = 22.0/7.0
|
|
|
|
12-07-2007, 06:38 PM
|
Re: how can i make Pi with PHP
|
Posts: 216
|
I know that you can not do this with c++ due to overflow on longs (once you reach a really large number it starts over at a negative number). Would this not also be the same for PHP?
__________________
~Mark Romero
-Co Founder | Tech Guru, Please login or register to view this content. Registration is FREE
-FireFox Advocate, Please login or register to view this content. Registration is FREE
|
|
|
|
12-07-2007, 06:44 PM
|
Re: how can i make Pi with PHP
|
Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
|
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
12-07-2007, 07:48 PM
|
Re: how can i make Pi with PHP
|
Posts: 219
Name: Rob
Location: UK
|
Is calculating Pi necessary - are you doing some sort of server benchmark test.
If not then surely 22/7 would be sufficient - most engineers will only use pi to 6 decimal places at most.
|
|
|
|
12-07-2007, 07:53 PM
|
Re: how can i make Pi with PHP
|
Posts: 6,522
Name: Dan
Location: Swindon
|
Quote:
Originally Posted by vangogh
Did someone say Pie? Oh PI. Never mind.
Dan PI = 22/7. You could set a variable PI = 22.0/7.0
|
Im sorry steve but 22/7 is most definatly NOT pi.
firstly it may work out to 3.14**
but it has a recoring pattern which makes it a rational number where as pie is a irrational number with no pattern what so ever....
Anyway, im now bit confuse if this is possible because form the php docs it seems to imply there is a limit but im just erm confused 
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
12-08-2007, 12:28 AM
|
Re: how can i make Pi with PHP
|
Posts: 10,688
Name: Steven Bradley
Location: Boulder, Colorado
|
Dan, my bad, but believe it or not that's how I originally learned it in school. Of course that is based on my memory of an event that's now close to 30 years ago so what I learned may have been meant as an approximation.
Is there a reason you need beyond 20 decimal places?
|
|
|
|
12-08-2007, 12:57 AM
|
Re: how can i make Pi with PHP
|
Posts: 216
|
Dan: Check this out http://en.wikipedia.org/wiki/Pi#Calculating_.CF.80 . That article explains how to accurately calculate pi.
Chris: Floating points can still overflow.(At least in my experience.)
__________________
~Mark Romero
-Co Founder | Tech Guru, Please login or register to view this content. Registration is FREE
-FireFox Advocate, Please login or register to view this content. Registration is FREE
Last edited by johnncyber; 12-08-2007 at 01:00 AM..
|
|
|
|
12-08-2007, 01:33 AM
|
Re: how can i make Pi with PHP
|
Posts: 6,442
Name: James
Location: In the ocean.
|
From what I've read PHP only allows 20 decimal places of precision on any number.
|
|
|
|
12-08-2007, 06:34 AM
|
Re: how can i make Pi with PHP
|
Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
|
For good reason I would assume - 20 d.p. is a lot of precision.
|
|
|
|
12-08-2007, 09:20 AM
|
Re: how can i make Pi with PHP
|
Posts: 6,522
Name: Dan
Location: Swindon
|
Why cant this just have a simple answer? Lol
for the sake of argument say i want to work out pi to as many places as i can.
so would there be a way to write a loop which works out each point, pauses to try and be kind of the server running it, and keep appending the decimal to our Pi figure?
so we could have while pi is less than X long keep working out more, or just have it as a infinant loop. but have it save to a database/file so it dont keep working them all out each time its displayed.
am i making any sense, because this sounds easy to me, if i had the calculation to work it out.
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
12-08-2007, 10:29 AM
|
Re: how can i make Pi with PHP
|
Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
|
Quote:
|
Why cant this just have a simple answer?
|
There isn't a simple answer to pi, in fact there isn't an answer to pi
with php you are limited to a precision of 20 digits anyway, but here's a few formulas if you want to code the calculation
http://www.math.com/tables/constants/pi.htm
Yes floating point numbers can overflow, at what point is dependant on the OS implementation
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
12-08-2007, 10:39 AM
|
Re: how can i make Pi with PHP
|
Posts: 219
Name: Rob
Location: UK
|
PHP Code:
//bcpi function with Gauss-Legendre algorithm //by Chao Xu (Mgccl) function bcpi($precision){ $limit = ceil(log($precision)/log(2))-1; bcscale($precision+6); $a = 1; $b = bcdiv(1,bcsqrt(2)); $t = 1/4; $p = 1; while($n < $limit){ $x = bcdiv(bcadd($a,$b),2); $y = bcsqrt(bcmul($a, $b)); $t = bcsub($t, bcmul($p,bcpow(bcsub($a,$x),2))); $a = $x; $b = $y; $p = bcmul(2,$p); ++$n; } return bcdiv(bcpow(bcadd($a, $b),2),bcmul(4,$t),$precision); };
echo bcpi(75);// pi to 75 decimal places 3.141592653589793238462643383279502884197169399375105820974944592307816406286
|
|
|
|
12-08-2007, 12:52 PM
|
Re: how can i make Pi with PHP
|
Posts: 40
Name: Spyros
Location: Athens, Hellas
|
Why you need to calculate PI? It's a constant; its' value will never change.
Can't you define a constant containing PI? Then, you can round it at as many demical places as you want.
|
|
|
|
12-08-2007, 02:43 PM
|
Re: how can i make Pi with PHP
|
Posts: 6,522
Name: Dan
Location: Swindon
|
Quote:
Originally Posted by maxxximus
PHP Code:
//bcpi function with Gauss-Legendre algorithm //by Chao Xu (Mgccl) function bcpi($precision){ $limit = ceil(log($precision)/log(2))-1; bcscale($precision+6); $a = 1; $b = bcdiv(1,bcsqrt(2)); $t = 1/4; $p = 1; while($n < $limit){ $x = bcdiv(bcadd($a,$b),2); $y = bcsqrt(bcmul($a, $b)); $t = bcsub($t, bcmul($p,bcpow(bcsub($a,$x),2))); $a = $x; $b = $y; $p = bcmul(2,$p); ++$n; } return bcdiv(bcpow(bcadd($a, $b),2),bcmul(4,$t),$precision); }; echo bcpi(75);// pi to 75 decimal places 3.141592653589793238462643383279502884197169399375105820974944592307816406286
|
Thanks for this so far this seems to be accurate, im currently running a simple if the pi generated with the function matched pi i copied from the pi website matches. and because it trying to match it to a measly million points its taking a while...
Lol
Thanks for this, only one more thing, would there be some way to be able to have it work out to X places save the figure somewhere or the amount of places, and then be able to get it to work out more without it having to work out the first X places?
does this make sense?
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
|
« Reply to how can i make Pi with PHP
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|