home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
No Fragments Archive 10: Diskmags
/
nf_archive_10.iso
/
MAGS
/
ST_USER
/
1990
/
USERJL90.MSA
/
TEXT_SHEET.DOC
< prev
next >
Wrap
Text File
|
1990-05-15
|
14KB
|
279 lines
Financial forcasting at your fingertips
Chor-ming Lung presents an unusual BASIC
interpreter in a spreadsheet environment
I have used spreadsheet programs for quite a long time and have found them to
be fantastic tools. However, one thing that I don't understand too well is the
macro language of some sheets. Often it is not user friendly and I think BASIC
is more powerful and yet easier to use.
Thinking that it may be profitable to write a BASIC interpreter in a
spreadsheet environment, I started to write the program in October 1987. After
a few months' struggle, I finally created the program and named it Sheet.
Sheet is a combination of spreadsheet and BASIC. The spreadsheet functions
and commands make the BASIC interpreter handle data very easily and the BASIC
interpreter makes the spreadsheet a lot powerful than many other spreadsheet
programs commercially available.
Tutorial Example 1: AMORT.SHT
Amortization means killing something bit by bit and that's the way we handle
loans - we pay them off a bit at a time.
Suppose you are trying to borrow $100,000. Your financial company provides
two options for this loan:
* 30 years with annual interest rate 9%
* 20 years with annual interest rate 10.5%
Intuitively, you may choose the first. Well, let us analyse this problem
with Sheet and see whether it is a wise choice. First, we have to design our
layout. We will use column A for labels, column B for option one and column C
for option two. Following are the steps for creating the template:
* Find the menu title Sheet and select Defaults. Choose the Centre (^) text
justification option and select OK.
* Move the cell cursor to B1 using the arrow keys then type "Option 1" without
the quotes) and press Return.
* Press the right arrow key to move from cell B1 to C1.
* Type "Option 2" (without the quotes) and press Return.
* Move to cell B2 and type "\-" (without the quotes, of course).
* Go to C2 and type "\-".
* Find the menu title Sheet and choose Defaults again. Now choose Right (")
text justification.
* Go to A3 and enter "Amount borrowed".
* Enter "Annual interest rate" in A4
* Move to A5 and enter "Total number of terms".
* Type "Terms per year" into A6.
* Go to A7 and type "\-".
* Go to B7 and enter "\-".
* In C7 also type "\-".
* And in A8 enter "Payment".
The command "\-" means fill up the cell with the character following the
slash - minus signs in this case. Notice that the entries in column A occupy
some areas of column B and C, so we need to enlarge the width of column A to
make room for the entries. To widen column A find the menu item Col Widths and
enter the column name and your desired width.
The formula we need for our calculation is LOAN_PAY, which takes four
arguments: amount borrowed, annual interest rate, total number of terms and
terms per year. That is LOAN_PAY(B3,B4,B5,B6).
However, if you enter this line into cell B8, Sheet will treat it as a text
string instead of a formula. That is because the leading character of a cell
entry determines its type. A cell that starts with +, -, digit, ( or . is a
formula and a cell that starts with alphabetic character is a text string.
Therefore a + symbol before LOAN_PAY will tell Sheet to treat it as a formula.
* Go to cell B8 and enter "+LOAN_PAY(B3,B4,B5,B7)".
* Stay at cell B8 and copy the content of B8 to C8 by pointing and clicking the
mouse button on B8 while holding down the Control button. Point and click on C8
while holding down the mouse button then press Return. The formular in B8
should now appear (in a modified form) in C8. (Sheet is case-insensitive so it
will treat loan_pay as LOAN_PAY.)
After those steps, you should see 0.1e+38 in both B8 and C8. Don't panic,
this number simply means a calculation error.
Column B and column C are too close to each other, but you should be able
to change the column width of column B and C without any difficulty now. How
wide should they be? It's up to you.
It is time to enter our data. Type 100000 into B3, 0.09 into B4, 360 into
B5 and 12 into B6. After typing in 100000, terminate your input by the down
arrow key. This action changes the editing direction to down. Now whenever you
terminate your input with Return, the cell cursor will automatically move
down.
B3 holds the $100,000 you want to borrow. B4 holds the annual interest rate
9% and B5 holds the total number of terms you must pay back to your financial
company. It is a monthly payment so you must pay 12 times each year. Hence, the
total number of payments is 30*12 or 360.
Enter 100000, 0.105, 240 and 12 into cells C3, C4, C5, and C6
respectively.
The payment for option one is 804.622617 and it is 998.379887 for option
two. Your financial company will ask you to pay $804.62 and $998.38
respectively and to reflect this, you must reformat the display. We need to
change B3..C3 and B8..C8 to dollar format and B4..C4 to percent (%) format.
* Point and click on B3 while holding down the Control key.
* Hold down Shift and point and click on C3.
* Control+click on B8.
* Shift+click on C8.
* Press Control+R and select Dollar.
The above operations define two blocks. Control+mouse click on a cell
defines the start of a block and Shift+mouse click on a cell defines the end of
a block. The first block is B3..C3 and the second block is B8..C8. Control+R
activates the reformat dialog box. Selecting Dollar will change the cells
contained in those blocks to Dollar format. It should now be easy for you to
change B4..C4 to percent format.
The payment just tells us what we need to pay per month, but which option
is best? Well, in option one we pay $804.62 each month and we need to do it 360
times. So, the total amount we pay is 360*804.62. For option two we pay
240*998.38. In cell A9, enter "Total payment", in B9, enter 360*B8 and in C9
enter 240*C8.
Comparing the results on B9 and C9, you will find you pay $50,000 less in
option two. So, if you can affort $998.38 per month, you should choose option
two.
Tutorial Example 2: RANDOM.SHT
Now, I'm going to show you the unusual part of this spreadsheet program - the
BASIC interpreter. This example will generate 100 random numbers and calculate
the average, median, minimum, maximum, variance and standard deviation of those
numbers.
In the following listing "C1 : !R=0" means that cell C1 contains the
statement "!R=0". To enter the program you should move the cell cursor to C1
and enter "!R=0" (without the quotes). To run the BASIC program, enter "@run
c1" (without the quotes).
C1 : !R=0
C2 : ! LOOP
C3 : ! EXIT IF R=100
C4 : !A1[0,R]= RAND
C5 : !R=R+1
C6 : ! ENDLOOP
C7 : !B3= AVERAGE(A1..A100)
C8 : !B4= STD(A1..A100)
C9 : !B5= VAR(A1..A100)
C10 : !B6= MIN(A1..A100)
C11 : !B7= MAX(A1..A100)
C12 : !B8= MEDIAN(A1..A100)
Line by line explanation:
C1: R is a variable and it is initialized to 0. (you must initialise all
variables).
C2: LOOP is a flow control command is from Modula-2.
C3: EXIT IF is part of LOOP statement. It is an exit for the LOOP. If the
conditional expression for EXIT IF is true, then the next command being
executed is the one following ENDLOOP.
C4: RAND is the random number generating function. The left hand side of the
assignment statement is A1[0,R] which is a new way to access or store
results in cells. We treat cells in spreadsheet as a two dimensional
array. Use the column letter and row number as a cell reference.
C5: Increase the variable R by one.
C6: The ENDLOOP statement will force the BASIC interpreter to jump back to
C3 and test whether R=100. If it is, then the next statement being
executed is in C7.
C7: Stores the average values of the 100 random number in cell B3
C8: Stores the standard deviation in cell B4
C9: Stores the variance in cell B5
C10: Stores the minimum value to B6
C11: Stores the maximum value to B7
C12: Stores the median in cell B8
The logic of the above program would be more straight forward using a FOR
.. NEXT statement and you can generate 100 numbers by using the spreadsheet
commands only. In cell A1, enter "+RAND". COPY A1 to A2..A100. Enter
"+AVERAGE(A1..A100)" in cell B3 and so on.
Tutorial Example 3: SINCURVE.SHT
This example draws a sine curve on the current window. To run it, enter the
command "@RUN A1".
A1 : ! WINDOW
A2 : ! SCALE -7,-1.2,7,1.2
A3 : ! CLEAR
A4 : ! PLOT -2*PI, SIN(-2*PI)
A5 : ! FOR X=-2*PI TO 2*PI+0.1 STEP 0.1
A6 : ! LINE_TO X, SIN(X)
A7 : ! NEXT X
A8 : ! LINE -7,0,7,0: LINE 0,-1.2,0,1.2
A9 : ! MARKX 0,-7,7,0.5,1
A10 : ! MARKY 0,-1.2,1.2,0.1,0
A11 : ! LABELX 0,-7,7,1,"f0"
A12 : ! LABELY 0,-1.2,1.2,0.2,"f2"
A13 : ! WAIT 5: EXITGRAPH
Explanation:
A1: WINDOW with no parameter means we are using current window for output.
A2: SCALE for our sine curve.
A3: Clear our window.
A4: Plot the first point of our sine curve.
A5: A FOR loop to generate a sine wave from -2*PI to 2*PI.
A6: Instead of plotting dots, we want to connect all the dots with lines.
A8: Draws the x- and y-axis.
A9: Marks x-axis.
A10: Marks y-axis.
A11: Draws the labels on x-axis. They are integers (f0). F means the number
is displayed in fixed format. 0 means decimal place is 0. So, it has no
decimal place or it is integer.
A12: Draws the labels on y-axis. They are real number with two decimal points.
A13: Waits for 5 seconds and then refreshes our window.
CONTROL KEYS
-------------
Non-editing:
Esc Stops a running BASIC program
Arrow keys Move cell cursor
Control+mouse click Define start cell of a block
Shift+mouse click Defines end cell of a block
Alternate+1 Switches to the first window
Alternate+2 Switches to the second window
Alternate+3 Switches to the third window
Alternate+4 Switches to the fourth window
Alternate+F Refreshes the current window
Alternate+C Recalculates the spreadsheet by column
Alternate+N Recalculates the spreadsheet by natural order
Alternate+R Recalculates the spreadsheet by row
F2 Modifies the current cell's content
F5 Goto a specified cell
Editing:
Up arrow Terminates input and moves cell cursor one row up.
Editing direction changes to UP
Down arrow Terminates input and moves cell cursor one row down.
Editing direction changes to DOWN.
Left arrow Terminates input and moves cell cursor one column
left. Editing direction changes to LEFT
Right arrow Terminates inpuit and moves cell cursor one column
right. Editing direction changes to RIGHT
Shift+left arrow Moves editing cursor to left
Shift+Right arrow Moves editing cursor to right
Return Terminates input and moves cell cursor to direction
specified by editing direction
Mouse click If the mouse cursor is within working area, the
coordinate of the cell under the mouse cursor will
be inserted into editing buffer; if the mouse cursor
is within the editing line, the editing cursor will
move to the character under the mouse cursor
Control+mouse click If the mouse cursor is within working area a comma and
the coordinate of the cell under mouse cursor will be
inserted into editing buffer
Shift+mouse click If the mouse cursor is within working area, a through
symbol ".." and the coordinate of the cell under the
mouse cursor will be inserted into the editing buffer
Program Specification
_____________________
Sheet is written in Mark Williams C and the accuracy for real numbers is up
to 17 digits. The range of calculation is -1E-37 to 1E+37. Each spreadsheet can
accomdate up to 2,549,745 (255*9999) cells and the sheet is implemented by
using Sparse Matrix. Any input to a cell will be tokenised before it is
inserted. Sheet supports up to four windows. The BASIC interpreter can handle
graphics, strings, and ASCII text files.
Arithmetic operators: +, -, *, /, ^, MOD
Logical operators: AND, OR, NOT, =, <>, >, <, >=, <=, =>, =<
Built-In functions:
Math: SIN, COS, TAN, ASIN, ACOS, ATAN, LOG, EXP, LN, ABS, INT, FRAC,
SQR, SQRT, DATE, FACT, RAD_DEG, DEG_RAD, PI (constant)
Statistics: AVERAGE, MEDIAN, STD, VAR, SQR_SUM
Miscellaneous: MAX, MIN, SUM, COUNT, TODAY, DAY, MONTH, YEAR, TRUE, FALSE
(TRUE and FALSE are constant, TRUE is 1 and FALSE is 0), IFF,
ISEMPTY, ALERT, LAST_ROW, LAST_COL, SEARCH.
Finance: GRATE, GBASE, GFUTURE, INVEST_PV, INVEST_FV, INVEST_INTEREST,
INVEST_TERM, LOAN_PAY, LOAN_AMOUNT, LOAN_TERM, WDRAW_SAVE,
WDRAW_AMOUNT, WDRAW_TERM, DEPOSIT_FV, DEPOSIT_AMOUNT,
DEPOSIT_TERM
String: MID$, CHR$, LEFT$, RIGHT$, SPACE$, STR$, VAL, LEN, ASC,
FILENAME$(a string variable used for file item selector) and
BUF$(a string variable used by the command INPUT).
Matrix: MATRIX, TRANSPOSE, INVERSE, *, +, -