Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
how can i make Pi with PHP
Old 12-07-2007, 06:29 AM how can i make Pi with PHP
dansgalaxy's Avatar
Defies a Status

Posts: 6,522
Name: Dan
Location: Swindon
Trades: 0
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 <<

Please login or register to view this content. Registration is FREE

Last edited by dansgalaxy; 12-07-2007 at 06:32 AM..
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
 
Register now for full access!
Old 12-07-2007, 09:34 AM Re: how can i make Pi with PHP
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
http://uk.php.net/pi

http://uk.php.net/manual/en/function.round.php

PHP Code:
echo (round(pi(),$No_of_decimal_places)); 
__________________
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?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-07-2007, 06:14 PM Re: how can i make Pi with PHP
dansgalaxy's Avatar
Defies a Status

Posts: 6,522
Name: Dan
Location: Swindon
Trades: 0
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 <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 12-07-2007, 06:21 PM Re: how can i make Pi with PHP
vangogh's Avatar
Post Impressionist

Posts: 10,688
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
Did someone say Pie? Oh PI. Never mind.

Dan PI = 22/7. You could set a variable PI = 22.0/7.0
__________________
l Search Engine Friendly Web Design |
Please login or register to view this content. Registration is FREE

l Tips On Marketing, SEO, Design, and Development |
Please login or register to view this content. Registration is FREE

l
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 12-07-2007, 06:38 PM Re: how can i make Pi with PHP
johnncyber's Avatar
Extreme Talker

Posts: 216
Trades: 0
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
johnncyber is offline
Reply With Quote
View Public Profile Visit johnncyber's homepage!
 
Old 12-07-2007, 06:44 PM Re: how can i make Pi with PHP
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
http://en.wikipedia.org/wiki/Floating_point
__________________
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?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-07-2007, 07:48 PM Re: how can i make Pi with PHP
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
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.
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 12-07-2007, 07:53 PM Re: how can i make Pi with PHP
dansgalaxy's Avatar
Defies a Status

Posts: 6,522
Name: Dan
Location: Swindon
Trades: 0
Quote:
Originally Posted by vangogh View Post
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 <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 12-08-2007, 12:28 AM Re: how can i make Pi with PHP
vangogh's Avatar
Post Impressionist

Posts: 10,688
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
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?
__________________
l Search Engine Friendly Web Design |
Please login or register to view this content. Registration is FREE

l Tips On Marketing, SEO, Design, and Development |
Please login or register to view this content. Registration is FREE

l
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 12-08-2007, 12:57 AM Re: how can i make Pi with PHP
johnncyber's Avatar
Extreme Talker

Posts: 216
Trades: 0
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..
johnncyber is offline
Reply With Quote
View Public Profile Visit johnncyber's homepage!
 
Old 12-08-2007, 01:33 AM Re: how can i make Pi with PHP
joder's Avatar
Flipotron

Posts: 6,442
Name: James
Location: In the ocean.
Trades: 0
From what I've read PHP only allows 20 decimal places of precision on any number.
joder is offline
Reply With Quote
View Public Profile
 
Old 12-08-2007, 06:34 AM Re: how can i make Pi with PHP
Foundationflash's Avatar
Ultra Talker

Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
Trades: 0
For good reason I would assume - 20 d.p. is a lot of precision.
__________________
Foundation Flash tutorials :
Please login or register to view this content. Registration is FREE


New Dreamed Up Web Design:
Please login or register to view this content. Registration is FREE
Foundationflash is offline
Reply With Quote
View Public Profile Visit Foundationflash's homepage!
 
Old 12-08-2007, 09:20 AM Re: how can i make Pi with PHP
dansgalaxy's Avatar
Defies a Status

Posts: 6,522
Name: Dan
Location: Swindon
Trades: 0
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 <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 12-08-2007, 10:29 AM Re: how can i make Pi with PHP
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
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?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-08-2007, 10:39 AM Re: how can i make Pi with PHP
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
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($tbcmul($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 
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 12-08-2007, 12:52 PM Re: how can i make Pi with PHP
Spiros's Avatar
Experienced Talker

Posts: 40
Name: Spyros
Location: Athens, Hellas
Trades: 0
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.
Spiros is offline
Reply With Quote
View Public Profile
 
Old 12-08-2007, 02:43 PM Re: how can i make Pi with PHP
dansgalaxy's Avatar
Defies a Status

Posts: 6,522
Name: Dan
Location: Swindon
Trades: 0
Quote:
Originally Posted by maxxximus View Post
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($tbcmul($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 <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to how can i make Pi with PHP
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.61906 seconds with 12 queries