home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Archive Magazine 1996
/
ARCHIVE_96.iso
/
discs
/
mag_discs
/
volume_2
/
issue_06
/
pi_stuff
/
PIFORTH
< prev
next >
Wrap
Text File
|
1988-12-29
|
5KB
|
180 lines
EMPTY 0 MODE
100000 CONSTANT base 5 CONSTANT dp
0 CONSTANT fa
0 CONSTANT A 0 CONSTANT B
0 CONSTANT X 0 CONSTANT Y
VARIABLE places
VARIABLE pointer 0 pointer !
VARIABLE plusminus 0 plusminus !
( Define 4 arrays )
: A% 4 * A + ; : B% 4 * B + ;
: X% 4 * X + ; : Y% 4 * Y + ;
( Dimension space for 4 arrays )
: DIM HERE TO A places @ 4 * ALLOT
HERE TO B places @ 4 * ALLOT
HERE TO X places @ 4 * ALLOT
HERE TO Y places @ 4 * ALLOT ;
( Print ALL 5 digits from cell)
: .CELL <# # # # # # #> TYPE SPACE ;
( .PI prints final value)
: .PI CR 0 Y% @ . ." ." places @ 1- 1
DO I DUP Y% @ .CELL 13 MOD 0=
IF 2 SPACES THEN LOOP CR ;
( INITialise A%[] & B%[] arrays )
: INIT places @ 0 DO
0 I A% ! 0 I B% ! LOOP 1 0 B% !
0 pointer ! 0 plusminus ! ;
( TAKE one cell from another )
: TAKE + 2DUP < IF SWAP base +
-: 1 ELSE - 0 THEN SWAP ;
( DIVide B%[] by number into B%[]. )
: DIVB pointer @ DUP >R B% @
OVER UM/MOD DUP R@ B% ! 0=
IF 1 pointer +! THEN base *
places @ R> 1+ 2DUP > IF DO I B% @
+ OVER UM/MOD I B% ! base * LOOP
ELSE 2DROP THEN 2DROP ;
( DIVide B%[] by number into A%[] )
: DIVA 0 places @ pointer @
1- DO I B% @ + OVER UM/MOD I A% !
base * LOOP 2DROP ;
( Copy B%[] to A%[] )
: B->A places @ 0
DO I B% @ I A% ! LOOP ;
( Add A%[] to X%[] )
: ADDAX 0 0 places @ 1- DO I A% @
I X% @ + + base UM/MOD
SWAP I X% ! -1 +LOOP DROP ;
( Add X%[] to Y%[]. )
: ADDXY 0 0 places @ 1- DO I X% @
I Y% @ + + base UM/MOD
SWAP I Y% ! -1 +LOOP DROP ;
( Take A%[] from X%[]. )
: TAKEAX 0 0 places @ 1-
DO I X% @ I A% @ ROT TAKE
I X% ! -1 +LOOP DROP ;
( Times X%[] by number. )
: TIMES 0 0 places @ 1- DO I X% @
2 PICK UM* + base UM/MOD
SWAP I X% ! -1 +LOOP 2DROP ;
( Accumulate ARCTAN series in X%[] )
: ACCUM 1 plusminus @ - DUP
plusminus ! IF ADDAX
ELSE TAKEAX THEN ;
( Calculate Arctan )
: ARCTAN DUP DUP INIT DIVB B->A
ACCUM 147 < IF DUP * 2 TO fa
ELSE 1 TO fa THEN
1 BEGIN OVER DIVB fa + DUP 2 MOD
IF DUP DIVA ACCUM
THEN pointer @ places @ 1- >
UNTIL 2DROP ;
( Add all arctan series into Y%[])
: PI 0 !TIME EMPTY 1- dp / 3 +
places ! DIM places @ 0
DO 0 I X% ! 0 I Y% ! LOOP
8 ARCTAN 24 TIMES ADDXY
places @ 0 DO 0 I X% ! LOOP
57 ARCTAN 8 TIMES ADDXY
places @ 0 DO 0 I X% ! LOOP
239 ARCTAN 4 TIMES ADDXY
.PI CR ." Time:= " @TIME
.TIME ." seconds" ;
PROTECT
( Typing 1000 PI will run the )
( program, giving 1000 decimal )
( places in about 33 seconds. )
( 200 PI will give 200 places. etc )
( Typing .PI will repeat a printout )
( of the number. )
( )
( FORTH addicts? IF will understand )
( ELSE will not understand )
( THEN read on anyway! )
( The above should give you a clue )
( that FORTH is a bottom about )
( chest language. Here are some )
( comparisons between FORTH & BASIC )
( BASIC LET X%[I] = 5 )
( FORTH 5 I X% ! )
( BASIC LET X%[I] = X%[I] + A%[I] )
( FORTH I X% @ I A% @ + I X% ! )
( BASIC LET pointer = pointer + 1 )
( FORTH 1 pointer +! )
( FORTH is based around loading and )
( storing onto and from a stack. )
( @ means load onto the stack. )
( ! means store from the stack. )
( Eg. X @ Y @ + Z ! means load the )
( values in X and Y onto the stack )
( then add them together and store )
( the result in Z. )
( WORDs or procedures are defined )
( starting with a colon <:> and )
( ending with a semicolon <;> )
( Eg. in BASIC a procedure to make )
( a beep might be as follows. )
( DEFPROC_beep: VDU 7 :ENDPROC )
( In FORTH would be )
( : BEEP 7 VDU ; )
( Typing PROC_beep in BASIC or BEEP )
( in FORTH would make a beep. )
( Just as in BASIC where PROC_beep )
( can be used in other procedures )
( BEEP can be used in the definition)
( of other words in FORTH. Eg. )
( : NOISE 25 0 DO BEEP LOOP ; )
( Typing NOISE would make 25 beeps. )
( NOISE and BEEP become part of the )
( language and can be used just like)
( any other word in the language. )
( FORTH is sometimes called a DIY )
( language. You can define words to )
( do whatever you wish. )
( The PI program is surprisingly )
( only about 5 times faster than the)
( BASIC version. Compliment to BBC )
( BASIC!! )
( FORTH gets faster when only a few )
( variables are used and most of the)
( other values are kept on the stack)
( The PI program uses a lot of )
( variables so is not a good test of)
( the language. )
( Comments or REMs should be in )
( brackets which are ignored by the )
( compiler. )
( This program should be FILELOADed )
( into RiscFORTH these comments will)
( be ignored.)
( A stand alone program is 28K long )
( 3K for the PI code and 25K for the)
( FORTH kernel. I would think that )
( the size of the code is less than )
( the tokenised BASIC version. The )
( 25K for the FORTH kernel is not )
( excessive on a 1000K machine. )
( It would seem that RiscFORTH )
( produces fast compact code. I )
( would be interested to know how )
( fast and what size a compiled )
( PASCAL, BASIC or C version of the )
( PI program would be? Any offers? )