This is just for fun, and of course for those good memories with 8-bit famicom clones.
Subor SB2000 (as described here) is a 8-bit famicom clone with keyboard. Last time, a simple F-BASIC solution is presented to compute the approximation of PI. but today, we are going to compute more decimal places after dot using F-BASIC.
The algorithm we are going to use is described in this page. It is a infinite but straightforward forumla:
It took approximately one hour to compute for just 40 accuracy positions and 5 hours for 80 decimal places. Could you imagine how slow this is ? On modern PC, the same code will be executed less than a second without doubt.
Floating BASIC isn’t that floating!
The complete F-BASIC source is (may vary a little bit)
1 REM ********************************* 5 REM *JUST FOR FUN * 6 REM *F-BASIC PROGRAM BY HELLOACM.COM* 7 REM *TESTED ON SB2000-FBASIC * 8 REM *REALLY SLOW! * 9 REM ********************************* 10 N=40 20 DIM X(N) 30 DIM Z(N) 31 FOR I=1 TO N 32 X(I)=0 33 Z(I)=0 34 NEXT I 40 X(2)=2 50 Z(2)=2 60 A=1 70 B=3 80 D=0 90 J=N 95 IF J=0 THEN GOTO 140 100 C=Z(J)*A+D 110 Z(J)=C MOD 10 120 D=INT(C/10) 130 J=J-1 135 GOTO 95 140 D=0 145 J=1 150 IF J>N GOTO 200 160 C=Z(J)+D*10 170 Z(J)=INT(C/B) 180 D=C MOD B 190 J=J+1:GOTO 150 200 R=0 210 J=N 215 IF J=0 THEN GOTO 280 220 C=X(J)+Z(J) 230 X(J)=C MOD 10 240 X(J-1)=X(J-1)+INT(C/10) 250 R=R+Z(J) 260 J=J-1 270 GOTO 215 280 IF R=0 THEN GOTO 300 290 A=A+1 292 B=B+2 295 GOTO 80 300 T=1:S$="" 301 IF T MOD 20=0 PRINT S;:S$="" 302 S$=S$+STR$(X(T)) 315 T=T+1 320 IF T>N GOTO 330 310 GOTO 301 330 REM END
Some observations on above F-BASIC code:
1. the code is hard to read, because code is not indent but a mandatory line number should be there at the beginning of each statement.
2. using goto is messy. I tried to avoid them but the basic complains some syntax error (but in fact not) ‘FOR’ statement (and I don’t know why). Therefore, I have to use goto to simulate the ‘FOR’
3. line numbers (labels) are automatically sorted in the ascending order.
4. using list to see the source, and run to run the program. There is no debugging facilities at all.
It still takes approximately long if you run the program in the 8-bit emulator e.g. VirtualNES.
compute-pi
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: C/C++ Coding Exercise, Valid Parentheses - LeetCode Online Judge - Using Stack
Next Post: VBScript Coding Exercise - Compute PI using Monte Carlo Random Method