Calculating Pi via the Gregory-Leibniz series in BASIC on the Tandy Color Computer 2
By Michael Doornbos
- 2 minutes read - 265 wordsBack in March, we did a simple set of programs to brute force calculate Pi using a simple and well-known series. It works, even if it’s inefficient.
Speaking of inefficient, it’s September and that means it’s time for #Septandy! I have a Tandy Color Computer 2 - 16k of RAM version. There are versions of this machine with more RAM and an enhanced version of BASIC called “Extended BASIC”, but I’ve had this machine since the 80s and it is the most entry-level version Tandy made.
BASIC Code
Ideally, we’d like to use the same code as before. It’s simple and easy to understand:
10 T=100
20 PI=0
30 FOR I=0TOT
40 PI=PI+((4*(-1)^I)/(2*I+1))
45 PRINT PI
50 NEXT I
This works on Coco machines that have Extended BASIC, but there’s a problem with Tandy BASIC. My machine has this older version and it curiously is missing the exponent math function. There’s no way to do something like -1 to the Nth
power and we need this in line 40.
Creating a subroutine is the best way to do this, but obviously, it will be really slow. Let’s implement it anyway. Our code could end up something like:
10 T=100
20 PI=0
30 FOR I=0TOT
35 PI=I:N=-1:GOSUB 2000
40 PI=PI+((4*E)/(2*I+1))
45 PRINT PI
50 NEXT I
100 END
2000 REM POWER
2010 E=1
2020 FOR X=1 TO P
2030 E=E*N
2040 NEXT X
2050 IF P=0 THEN E=1
2060 RETURN
Can it beat a VIC-20?
So here we have another episode of “stupid vintage computer races”. The results are not surprising, but it sure is fun!