Page 1 of 1
Pi
Posted: Thu Nov 27, 2003 9:34 pm
by pdqbach
Code: Select all
;Pi = 4 - 4/3 + 4/5 - 4/7 + 4/9...
DefType.f p
DefType.f n
n = 1
p = 4
OpenConsole()
loop:
p = p -(4/(n+2)) + (4/(n+4)) ;the algorithm
n = n + 4
c = c + 1 ; update the result every 1000000th cycle
If c = 1000000
Gosub output
EndIf
Goto loop
output:
ConsoleLocate(1, 1)
PrintN(StrF(p,15))
c = 0
Return
End
---------------------------------------
What can i do to prevent it from stopping the calculation at 3.1418...
the algorithm is right, i've tested it with other basic versions.

Posted: Thu Nov 27, 2003 10:03 pm
by Num3
Code: Select all
n.f = 1
p.f = 4
OpenConsole()
loop:
p = p -(4/(n+2)) + (4/(n+4)) ;the algorithm
n + 4
c + 1 ; update the result every 1000000th cycle
If c = 1000000
Gosub output
EndIf
Goto loop
output:
ConsoleLocate(1, 1)
PrintN(StrF(p,15))
c = 0
Return
End
You don't need to Deftype... Use the type directly on the variable...
You also don't the X=X+1 you can add the variable directly X+1...
Anyway, purebasic seems only to output 3.141801595687866
Posted: Fri Nov 28, 2003 12:40 am
by akj
Here is a far better program. It is VERY quick.
(Another good routine is in Computing with the Amstrad October 1985 pages 19-20.)
Code: Select all
;PI AKJ 27-Nov-03
; Based upon articles in Popular Computing Weekly
; August 11-17 1988 page 27 and March 16-22 1989 page 67
; The result will be output to approximately the number of decimal places requested
; Initially try 770 decimal places. The last 8 digits should be 9999 9983
Declare addbtoa()
Declare subbfroma()
Declare divcby25()
Declare divdby57121()
Declare divcbyn()
Declare divdbyn()
Global d, x, n
OpenConsole()
Print("Number of decimal places? ")
np=Val(Input())
If np<4: np=4: EndIf
PrintN("")
x=10000 : xw=4
d=np/xw+2
; Working registers, each element holding an integer 0..9999
Dim a(d)
Dim b(d)
Dim c(d)
Dim d(d)
it=(np/Log10(25)+5)/2 ; Number of iterations
c(1) = 800 : d(1) = 9560 : n=-1
For j=1 To it
n + 2
divcby25()
divcbyn()
addbtoa()
divdby57121()
divdbyn()
subbfroma()
n + 2
divcby25()
divcbyn()
subbfroma()
divdby57121()
divdbyn()
addbtoa()
Next j
; Print result
Print(" 3.1")
For i=2 To d-1
Print(RSet(Str(a(i)),xw,"0")+" ")
If (i%15)=0
PrintN("")
EndIf
Next i
PrintN("")
PrintN("Press ENTER")
Input()
CloseConsole()
End
Procedure addbtoa() ; Multi-precision addition
c=0 ; Carry
For i=d To 1 Step -1
s = a(i)+b(i)+c
c = s/x
a(i) = s%x
Next i
EndProcedure
Procedure subbfroma()
c = 0
For i=d To 1 Step -1
s = a(i)-b(i)-c
If s<0
s + x: c = 1
Else
c=0
EndIf
a(i) = s%x
Next i
EndProcedure
Procedure divcby25()
r = 0 ; Remainder
For i=1 To d
c = c(i)+x*r
c(i) = c/25
r=c%25
Next i
EndProcedure
Procedure divdby57121()
r = 0
For i=1 To d
c = d(i)+x*r
d(i) = c/57121
r=c%57121
Next i
EndProcedure
Procedure divcbyn()
; b()=c()/n
r = 0
For i=1 To d
c = c(i)+x*r
b(i) = c/n
r=c%n
Next i
EndProcedure
Procedure divdbyn()
; b()=d()/n
r = 0
For i=1 To d
c = d(i)+x*r
b(i) = c/n
r=c%n
Next i
EndProcedure
Posted: Fri Nov 28, 2003 6:59 pm
by pdqbach
Thank you. I know there are much better ways to calculate Pi. But i intended to program all known algorithms, starting with the easiest, to learn programming (and maths). I think one solution would be to round the variables at a certain point, or somehow try to calculate with integers (wich, on the other hand, would decrease speed dramatically). Maybe because Purebasic supports only 32 bit float it rounds in a way that the terms with minus and with plus equal at some point (the relative difference between the last two terms is getting smaller as the calculation goes on). As a result the calculation stops at 3.1418...
maybe. dunno. im an absolute beginner...
Posted: Fri Nov 28, 2003 8:38 pm
by freak
yes, this is an accuracy problem. The change rate gets too low, and so is
rounded to 0.
Add this line where the output is displayed to see:
PrintN(StrF((4/(n+2)) + (4/(n+4))))
Timo