Calculating Pi?

Just starting out? Need help? Post your questions and find answers here.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Calculating Pi?

Post by Kale »

Has anyone got a way in PB to accurately calculate Pi? Here are a few ways but not very accurate:

Code: Select all

Debug StrF(4 * (ATan(1/2) + ATan(1/3)), 10)
Debug StrF(4 * (4 * ATan(1/5) - ATan(1/239)), 10)
Debug StrF((16 * ATan(1/5)) - (4 * ATan(1/239)), 10)
Debug StrF(4 * ATan(1), 10)
Any more?
--Kale

Image
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

Code: Select all

a.s=a.s+ StrF(4 * (ATan(1/2) + ATan(1/3)), 10) + Chr(13)
a.s=a.s+ StrF(4 * (4 * ATan(1/5) - ATan(1/239)), 10)+ Chr(13)
a.s=a.s+ StrF((16 * ATan(1/5)) - (4 * ATan(1/239)), 10)+ Chr(13)
a.s=a.s+ StrF(4 * ATan(1), 10) + Chr(13)

a.s=a.s+ StrD(4 * (ATan(1/2) + ATan(1/3)), 10) + Chr(13)
a.s=a.s+ StrD(4 * (4 * ATan(1/5) - ATan(1/239)), 10)+ Chr(13)
a.s=a.s+ StrD((16 * ATan(1/5)) - (4 * ATan(1/239)), 10)+ Chr(13)
a.s=a.s+ StrD(4 * ATan(1), 10) + Chr(13)

MessageRequester("Info",a.s ,0)
:mrgreen:
SPAMINATOR NR.1
MikeB
Enthusiast
Enthusiast
Posts: 183
Joined: Sun Apr 27, 2003 8:39 pm
Location: Cornwall UK

Post by MikeB »

Until we get higher precision floating point with PB (hopefully in V4.0) I don't think you can do an accurate calculation, even then you won't be able to get great precision without resorting to a huge loop calculating each decimal place one by one. This is an inherent problem with a never ending (as far as we know) calculation.
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

Rings wrote:

Code: Select all

a.s=a.s+ StrF(4 * (ATan(1/2) + ATan(1/3)), 10) + Chr(13)
a.s=a.s+ StrF(4 * (4 * ATan(1/5) - ATan(1/239)), 10)+ Chr(13)
a.s=a.s+ StrF((16 * ATan(1/5)) - (4 * ATan(1/239)), 10)+ Chr(13)
a.s=a.s+ StrF(4 * ATan(1), 10) + Chr(13)

a.s=a.s+ StrD(4 * (ATan(1/2) + ATan(1/3)), 10) + Chr(13)
a.s=a.s+ StrD(4 * (4 * ATan(1/5) - ATan(1/239)), 10)+ Chr(13)
a.s=a.s+ StrD((16 * ATan(1/5)) - (4 * ATan(1/239)), 10)+ Chr(13)
a.s=a.s+ StrD(4 * ATan(1), 10) + Chr(13)

MessageRequester("Info",a.s ,0)
:mrgreen:
Works great here: PB 3.94 :wink:

should I say anything else?
bye,
Daniel
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Post by RichardL »

Hi,

A bit off topic.

PI is about 3 and a bit ... good enough for gardening
PI is about 22/7... good enough for early schooling
PI is about 355/113... good to about ten decimal places.
(11 33 55 first three odd numbers with a stutter; that how I remember it.)

Stupid rhyme...

Sir I have a rhyme excelling in mystic force and magic spelling
Celestial sprites elucidate all my own striving can't relate

Twenty places, count the letters! (Assuming I can spell!)

Richard L
Last edited by RichardL on Mon Jan 16, 2006 5:51 pm, edited 2 times in total.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

:D
@}--`--,-- A rose by any other name ..
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

One way to "calculate" pi without reinventing the wheel:

Code: Select all

ProcedureDLL.s GetPi()  
  #INTERNET_FLAG_RELOAD = $80000000 
  Bytes.l = 0 
  Html.s  = Space(1024)
  r$ =""; 
  hInet.l = InternetOpen_("http://3.141592653589793238462643383279502884197169399375105820974944592.com/", 1, #Null, #Null, 0) 
  If hInet 
    hURL.l  = InternetOpenUrl_(hInet, "http://3.141592653589793238462643383279502884197169399375105820974944592.com/", #Null, 0, #INTERNET_FLAG_RELOAD, 0) 
    If hURL 
      If InternetReadFile_(hURL, @Html, Len(Html), @Bytes) 
        Html = Mid(html,FindString(Html,"3.14",1),400)
        For i= 1 To Bytes
          ;If (Mid(Html,i,1) >= "0" And Mid(Html,i,1) <= "9") Or Mid(Html,i,1) = "."
            r$ = r$ + Mid(Html,i,1)
          ;EndIf
        Next
        InternetCloseHandle_(hURL) 
      Else 
        ProcedureReturn "Failed"
      EndIf 
    Else 
      ProcedureReturn "Failed" 
    EndIf    
    InternetCloseHandle_(hInet) 
  Else 
    ProcedureReturn "Failed" 
  EndIf 
  ProcedureReturn r$
 
EndProcedure

pi$=Getpi()
MessageRequester("400 digits of pi",pi$)
Last edited by netmaestro on Wed Feb 22, 2006 3:31 am, edited 5 times in total.
BERESHEIT
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> Sir I have a rhyme excelling in mystic force and magic spelling
> Celestial sprites elucidate all my own striving can't relate

Wouldn't be any good in the USA. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

Even simpler:

Copy/Paste from here : http://www.geom.uiuc.edu/~huberty/math5 ... igits.html
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: Calculating Pi?

Post by Psychophanta »

Kale wrote:Has anyone got a way in PB to accurately calculate Pi? Here are a few ways but not very accurate:

Code: Select all

Debug StrF(4 * (ATan(1/2) + ATan(1/3)), 10)
Debug StrF(4 * (4 * ATan(1/5) - ATan(1/239)), 10)
Debug StrF((16 * ATan(1/5)) - (4 * ATan(1/239)), 10)
Debug StrF(4 * ATan(1), 10)
Any more?

Code: Select all

Debug ACos(-1)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

Code: Select all

OwnPi.d= ATan(1)*4
Debug OwnPi.d
Debug #PI 
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

For a method to calculate Pi to a large number of decimal places, see my PB program near the end of the forum topic:

www.purebasic.fr/english/viewtopic.php?t=8530
Anthony Jordan
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

maybe i missed something, but isn't the #PI constant in PB enough already?
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

@SFSxOI
maybe some ppl only want to know the math / algo behind #pi ;)
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
Post Reply