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 >
Text File  |  1990-05-15  |  14KB  |  279 lines

  1.                      Financial forcasting at your fingertips
  2.  
  3.                     Chor-ming Lung presents an unusual BASIC
  4.                     interpreter in a spreadsheet environment
  5.  
  6. I have used spreadsheet programs for quite a long time and have found them to
  7. be fantastic tools. However, one thing that I don't understand too well is the
  8. macro language of some sheets. Often it is not user friendly and I think BASIC
  9. is more powerful and yet easier to use.
  10.     Thinking that it may be profitable to write a BASIC interpreter in a
  11. spreadsheet environment, I started to write the program in October 1987. After
  12. a few months' struggle, I finally created the program and named it Sheet.
  13.     Sheet is a combination of spreadsheet and BASIC. The spreadsheet functions
  14. and commands make the BASIC interpreter handle data very easily and the BASIC
  15. interpreter makes the spreadsheet a lot powerful than many other spreadsheet
  16. programs commercially available.
  17.  
  18. Tutorial Example 1: AMORT.SHT
  19. Amortization means killing something bit by bit and that's the way we handle
  20. loans - we pay them off a bit at a time.
  21.     Suppose you are trying to borrow $100,000. Your financial company provides
  22. two options for this loan:
  23.  
  24. * 30 years with annual interest rate 9%
  25. * 20 years with annual interest rate 10.5%
  26.  
  27.     Intuitively, you may choose the first. Well, let us analyse this problem
  28. with Sheet and see whether it is a wise choice. First, we have to design our
  29. layout. We will use column A for labels, column B for option one and column C
  30. for option two. Following are the steps for creating the template:
  31.  
  32. * Find the menu title Sheet and select Defaults. Choose the Centre (^) text
  33. justification option and select OK.
  34. * Move the cell cursor to B1 using the arrow keys then type "Option 1" without
  35. the quotes) and press Return.
  36. * Press the right arrow key to move from cell B1 to C1.
  37. * Type "Option 2" (without the quotes) and press Return.
  38. * Move to cell B2 and type "\-" (without the quotes, of course).
  39. * Go to C2 and type "\-".
  40. * Find the menu title Sheet and choose Defaults again. Now choose Right (")
  41. text justification.
  42. * Go to A3 and enter "Amount borrowed".
  43. * Enter "Annual interest rate" in A4
  44. * Move to A5 and enter "Total number of terms".
  45. * Type "Terms per year" into A6.
  46. * Go to A7 and type "\-".
  47. * Go to B7 and enter "\-".
  48. * In C7 also type "\-".
  49. * And in A8 enter "Payment".
  50.  
  51.     The command "\-" means fill up the cell with the character following the
  52. slash - minus signs in this case. Notice that the entries in column A occupy
  53. some areas of column B and C, so we need to enlarge the width of column A to
  54. make room for the entries. To widen column A find the menu item Col Widths and
  55. enter the column name and your desired width.
  56.     The formula we need for our calculation is LOAN_PAY, which takes four
  57. arguments: amount borrowed, annual interest rate, total number of terms and
  58. terms per year. That is LOAN_PAY(B3,B4,B5,B6).
  59.     However, if you enter this line into cell B8, Sheet will treat it as a text
  60. string instead of a formula. That is because the leading character of a cell
  61. entry determines its type. A cell that starts with +, -, digit, ( or . is a
  62. formula and a cell that starts with alphabetic character is a text string.
  63. Therefore a + symbol before LOAN_PAY will tell Sheet to treat it as a formula.
  64.  
  65. * Go to cell B8 and enter "+LOAN_PAY(B3,B4,B5,B7)".
  66. * Stay at cell B8 and copy the content of B8 to C8 by pointing and clicking the
  67. mouse button on B8 while holding down the Control button. Point and click on C8
  68. while holding down the mouse button then press Return. The formular in B8
  69. should now appear (in a modified form) in C8. (Sheet is case-insensitive so it
  70. will treat loan_pay as  LOAN_PAY.)
  71.  
  72.      After those steps, you should see 0.1e+38 in both B8 and C8. Don't panic,
  73. this number simply means a calculation error.
  74.     Column B and column C are too close to each other, but you should be able
  75. to change the column width of column B and C without any difficulty now. How
  76. wide should they be? It's up to you.
  77.     It is time to enter our data. Type 100000 into B3, 0.09 into B4, 360 into
  78. B5 and 12 into B6. After typing in 100000, terminate your input by the down
  79. arrow key. This action changes the editing direction to down. Now whenever you
  80. terminate your input with Return, the cell cursor will automatically move
  81. down.
  82.     B3 holds the $100,000 you want to borrow. B4 holds the annual interest rate
  83. 9% and B5 holds the total number of terms you must pay back to your financial
  84. company. It is a monthly payment so you must pay 12 times each year. Hence, the
  85. total number of payments is 30*12 or 360.
  86.     Enter 100000, 0.105, 240 and 12 into cells C3, C4, C5, and C6
  87. respectively.
  88.     The payment for option one is 804.622617 and it is 998.379887 for option
  89. two. Your financial company will ask you to pay $804.62 and $998.38
  90. respectively and to reflect this, you must reformat the display. We need to
  91. change B3..C3 and B8..C8 to dollar format and B4..C4 to percent (%) format.
  92.  
  93. * Point and click on B3 while holding down the Control key.
  94. * Hold down Shift and point and click on C3.
  95. * Control+click on B8.
  96. * Shift+click on C8.
  97. * Press Control+R and select Dollar.
  98.  
  99.     The above operations define two blocks. Control+mouse click on a cell
  100. defines the start of a block and Shift+mouse click on a cell defines the end of
  101. a block. The first block is B3..C3 and the second block is B8..C8. Control+R
  102. activates the reformat dialog box. Selecting Dollar will change the cells
  103. contained in those blocks to Dollar format. It should now be easy for you to
  104. change B4..C4 to percent format.
  105.     The payment just tells us what we need to pay per month, but which option
  106. is best? Well, in option one we pay $804.62 each month and we need to do it 360
  107. times. So, the total amount we pay is 360*804.62. For option two we pay
  108. 240*998.38. In cell A9, enter "Total payment", in B9, enter 360*B8 and in C9
  109. enter 240*C8.
  110.     Comparing the results on B9 and C9, you will find you pay $50,000 less in
  111. option two. So, if you can affort $998.38 per month, you should choose option
  112. two.
  113.  
  114.  
  115. Tutorial Example 2: RANDOM.SHT
  116. Now, I'm going to show you the unusual part of this spreadsheet program - the
  117. BASIC interpreter. This example will generate 100 random numbers and calculate
  118. the average, median, minimum, maximum, variance and standard deviation of those
  119. numbers.
  120.     In the following listing "C1 : !R=0" means that cell C1 contains the
  121. statement "!R=0". To enter the program you should move the cell cursor to C1
  122. and enter "!R=0" (without the quotes). To run the BASIC program, enter "@run
  123. c1" (without the quotes).
  124.  
  125. C1  : !R=0
  126. C2  : ! LOOP
  127. C3  : ! EXIT  IF R=100
  128. C4  : !A1[0,R]= RAND
  129. C5  : !R=R+1
  130. C6  : ! ENDLOOP
  131. C7  : !B3= AVERAGE(A1..A100)
  132. C8  : !B4= STD(A1..A100)
  133. C9  : !B5= VAR(A1..A100)
  134. C10 : !B6= MIN(A1..A100)
  135. C11 : !B7= MAX(A1..A100)
  136. C12 : !B8= MEDIAN(A1..A100)
  137.  
  138. Line by line explanation:
  139. C1:  R is a variable and it is initialized to 0. (you must initialise all
  140.      variables).
  141. C2:  LOOP is a flow control command is from Modula-2.
  142. C3:  EXIT IF is part of LOOP statement. It is an exit for the LOOP. If the
  143.      conditional expression for EXIT IF is true, then the next command being
  144.      executed is the one following ENDLOOP.
  145. C4:  RAND is the random number generating function. The left hand side of the
  146.      assignment statement is A1[0,R] which is a new way to access or store
  147.      results in cells. We treat cells in spreadsheet as a two dimensional
  148.      array. Use the column letter and row number as a cell reference.
  149. C5:  Increase the variable R by one.
  150. C6:  The ENDLOOP statement will force the BASIC interpreter to jump back to
  151.      C3 and test whether R=100. If it is, then the next statement being
  152.      executed is in C7.
  153. C7:  Stores the average values of the 100 random number in cell B3
  154. C8:  Stores the standard deviation in cell B4
  155. C9:  Stores the variance in cell B5
  156. C10: Stores the minimum value to B6
  157. C11: Stores the maximum value to B7
  158. C12: Stores the median in cell B8
  159.  
  160.     The logic of the above program would be more straight forward using a FOR
  161. .. NEXT statement and you can generate 100 numbers by using the spreadsheet
  162. commands only. In cell A1, enter "+RAND". COPY A1 to A2..A100. Enter
  163. "+AVERAGE(A1..A100)" in cell B3 and so on.
  164.  
  165.  
  166. Tutorial Example 3: SINCURVE.SHT
  167. This example draws a sine curve on the current window. To run it, enter the
  168. command "@RUN A1".
  169.  
  170. A1  : ! WINDOW
  171. A2  : ! SCALE -7,-1.2,7,1.2
  172. A3  : ! CLEAR
  173. A4  : ! PLOT -2*PI, SIN(-2*PI)
  174. A5  : ! FOR X=-2*PI TO 2*PI+0.1 STEP 0.1
  175. A6  : ! LINE_TO X, SIN(X)
  176. A7  : ! NEXT X
  177. A8  : ! LINE -7,0,7,0: LINE 0,-1.2,0,1.2
  178. A9  : ! MARKX 0,-7,7,0.5,1
  179. A10 : ! MARKY 0,-1.2,1.2,0.1,0
  180. A11 : ! LABELX 0,-7,7,1,"f0"
  181. A12 : ! LABELY 0,-1.2,1.2,0.2,"f2"
  182. A13 : ! WAIT 5: EXITGRAPH
  183.  
  184. Explanation:
  185. A1:  WINDOW with no parameter means we are using current window for output.
  186. A2:  SCALE for our sine curve.
  187. A3:  Clear our window.
  188. A4:  Plot the first point of our sine curve.
  189. A5:  A FOR loop to generate a sine wave from -2*PI to 2*PI.
  190. A6:  Instead of plotting dots, we want to connect all the dots with lines.
  191. A8:  Draws the x- and y-axis.
  192. A9:  Marks x-axis.
  193. A10: Marks y-axis.
  194. A11: Draws the labels on x-axis. They are integers (f0). F means the number
  195.      is displayed in fixed format. 0 means decimal place is 0. So, it has no
  196.      decimal place or it is integer.
  197. A12: Draws the labels on y-axis. They are real number with two decimal points.
  198. A13: Waits for 5 seconds and then refreshes our window.
  199.  
  200.  
  201.  
  202.  
  203.  
  204. CONTROL KEYS
  205. -------------
  206.  
  207. Non-editing:
  208. Esc                     Stops a running BASIC program
  209. Arrow keys              Move cell cursor
  210. Control+mouse click     Define start cell of a block
  211. Shift+mouse click       Defines end cell of a block
  212. Alternate+1             Switches to the first window
  213. Alternate+2             Switches to the second window
  214. Alternate+3             Switches to the third window
  215. Alternate+4             Switches to the fourth window
  216. Alternate+F             Refreshes the current window
  217. Alternate+C             Recalculates the spreadsheet by column
  218. Alternate+N             Recalculates the spreadsheet by natural order
  219. Alternate+R             Recalculates the spreadsheet by row
  220. F2                      Modifies the current cell's content
  221. F5                      Goto a specified cell
  222.  
  223. Editing:
  224. Up arrow                Terminates input and moves cell cursor one row up.
  225.                         Editing direction changes to UP
  226. Down arrow              Terminates input and moves cell cursor one row down.
  227.                         Editing direction changes to DOWN.
  228. Left arrow              Terminates input and moves cell cursor one column
  229.                         left. Editing direction changes to LEFT
  230. Right arrow             Terminates inpuit and moves cell cursor one column
  231.                         right. Editing direction changes to RIGHT
  232. Shift+left arrow        Moves editing cursor to left
  233. Shift+Right arrow       Moves editing cursor to right
  234. Return                  Terminates input and moves cell cursor to direction
  235.                         specified by editing direction
  236. Mouse click             If the mouse cursor is within working area, the
  237.                         coordinate of the cell under the mouse cursor will
  238.                         be inserted into editing buffer; if the mouse cursor
  239.                         is within the editing line, the editing cursor will
  240.                         move to the character under the mouse cursor
  241. Control+mouse click     If the mouse cursor is within working area a comma and
  242.                         the coordinate of the cell under mouse cursor will  be
  243.                         inserted into editing buffer
  244. Shift+mouse click       If the mouse cursor is within working area, a through
  245.                         symbol ".." and the coordinate of the cell under the
  246.                         mouse cursor will be inserted into the editing buffer
  247.  
  248.  
  249.  
  250. Program Specification
  251. _____________________
  252.  
  253.     Sheet is written in Mark Williams C and the accuracy for real numbers is up
  254. to 17 digits. The range of calculation is -1E-37 to 1E+37. Each spreadsheet can
  255. accomdate up to 2,549,745 (255*9999) cells and the sheet is implemented by
  256. using Sparse Matrix. Any input to a cell will be tokenised before it is
  257. inserted. Sheet supports up to four windows. The BASIC interpreter can handle
  258. graphics, strings, and ASCII text files.
  259.  
  260. Arithmetic operators: +, -, *, /, ^, MOD
  261.  
  262. Logical operators: AND, OR, NOT, =, <>, >, <, >=, <=, =>, =<
  263.  
  264. Built-In functions:
  265.          Math: SIN, COS, TAN, ASIN, ACOS, ATAN, LOG, EXP, LN, ABS, INT, FRAC,
  266.                SQR, SQRT, DATE, FACT, RAD_DEG, DEG_RAD, PI (constant)
  267.    Statistics: AVERAGE, MEDIAN, STD, VAR, SQR_SUM
  268. Miscellaneous: MAX, MIN, SUM, COUNT, TODAY, DAY, MONTH, YEAR, TRUE, FALSE
  269.                (TRUE and FALSE are constant, TRUE is 1 and FALSE is 0), IFF,
  270.                ISEMPTY, ALERT, LAST_ROW, LAST_COL, SEARCH.
  271.       Finance: GRATE, GBASE, GFUTURE, INVEST_PV, INVEST_FV, INVEST_INTEREST,
  272.                INVEST_TERM, LOAN_PAY, LOAN_AMOUNT, LOAN_TERM, WDRAW_SAVE,
  273.                WDRAW_AMOUNT, WDRAW_TERM, DEPOSIT_FV, DEPOSIT_AMOUNT,
  274.                DEPOSIT_TERM
  275.        String: MID$, CHR$, LEFT$, RIGHT$, SPACE$, STR$, VAL, LEN, ASC,
  276.                FILENAME$(a string variable used for file item selector) and
  277.                BUF$(a string variable used by the command INPUT).
  278.        Matrix: MATRIX, TRANSPOSE, INVERSE, *, +, -
  279.