Page 1 of 1

Calculating Pi?

Posted: Mon Jan 16, 2006 1:52 pm
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?

Posted: Mon Jan 16, 2006 3:07 pm
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:

Posted: Mon Jan 16, 2006 3:10 pm
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.

Posted: Mon Jan 16, 2006 3:21 pm
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?

Posted: Mon Jan 16, 2006 5:40 pm
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

Posted: Mon Jan 16, 2006 5:50 pm
by Dare2
:D

Posted: Mon Jan 16, 2006 6:31 pm
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$)

Posted: Mon Jan 16, 2006 7:22 pm
by jack

Posted: Mon Jan 16, 2006 9:10 pm
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. :)

Posted: Mon Jan 16, 2006 10:55 pm
by Num3
Even simpler:

Copy/Paste from here : http://www.geom.uiuc.edu/~huberty/math5 ... igits.html

Re: Calculating Pi?

Posted: Fri Apr 14, 2006 5:10 pm
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)

Posted: Fri Apr 14, 2006 5:25 pm
by va!n

Code: Select all

OwnPi.d= ATan(1)*4
Debug OwnPi.d
Debug #PI 

Posted: Fri Apr 14, 2006 7:59 pm
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

Posted: Sat Apr 15, 2006 1:11 am
by SFSxOI
maybe i missed something, but isn't the #PI constant in PB enough already?

Posted: Sat Apr 15, 2006 3:16 am
by va!n
@SFSxOI
maybe some ppl only want to know the math / algo behind #pi ;)