File: kforth2d.html

package info (click to toggle)
kforth 20010227-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 508 kB
  • ctags: 652
  • sloc: asm: 2,026; cpp: 1,795; ansic: 575; makefile: 64
file content (135 lines) | stat: -rw-r--r-- 5,825 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<html>
<head>
<title>Using kForth IV</title>
</head>
<body>
<h2><img src="kforth.gif"> Using kForth</h2>
<br>
<a name="Simple Word Examples"></a><h3>2.6 Simple Word Examples</h3>
<p>
Now let us practice writing some simple and useful words.<br><br>
<i>Example 1:</i> Compounding Interest<br>
<p>
Suppose we invest $1000 and we expect that it will grow with
a yearly interest of 6%, which is compounded annually. What will
be the final amount after 10 years?<br>
<p>
We can determine the amount of interest accumulated after each year
by taking 6% of the current amount and adding that to the current 
amount. For example, you can type the following to compute and print 
the amount at the end of the first year:<br><br>
<code>1000 dup 6 * 100 / + .</code>
<br><br>
We placed the starting amount on the stack, then <code>dup</code>licated
this value on the stack to compute 6% interest. Finally we add the top two
numbers on the stack, the starting amount and the interest, and print the
sum. If you are confused by the above example, it will help to
print the contents of the stack using <code>.S</code> after you enter
each word on a separate line, e.g.
<pre><code>
1000 .S
dup .S
6 .S
* .S
100 .S
/ .S
+ .S
.</code></pre>
To solve the problem for 10 years, we simply need to repeat this calculation
ten times. Notice that we must skip the first word, <code>1000</code> and 
the last word, <code>.</code>, in between years so that we can use the 
compounded amount from one year as the starting amount for the next year. 
The final result may be printed at the end.<br>
<p>
Performing a repetetive calculation is easy in Forth -- it is done with
a <code>DO...LOOP</code>. The word <code>DO</code> expects
two numbers on the stack. The difference between the two numbers is the
number of times that the words between <code>DO</code> and <code>LOOP</code>
will be executed. The smaller number should be on top of the stack 
The following word illustrates using the <code>DO...LOOP</code> to
solve this problem:
<pre><code>
: compound10 ( -- | compound 6% interest on $1000 for 10 years and print answer )
    1000                \ starting amount
    10 0 do             \ do this for ten years
      dup 6 * 100 /     \ compute 6% interest of the current amount
      +                 \ add interest to the current amount
    loop                \ loop to next year
    .                   \ finally print the result
;
</code></pre>
Executing the word <code>compound10</code> will display the answer<br><br>
<code>1786</code><br><br>
<p>
Now let's generalize our word so that it is more useful. We want to be able
to specify the starting amount, the interest, and the number of years to
compound the interest. Finally, we want to print the result as before. The
following word takes inputs from the stack, computes the final amount, and
prints the answer:
<pre><code>
: compound ( nstart npercent nyears -- )
     0 do                \ do this for nyears
       2dup * 100 /      \ compute interest on the current amount
       rot +             \ add interest to the current amount
       swap              \ swap items on stack to keep same order for each loop
     loop                \ loop to next year
     drop .              \ drop the interest and print the final amount
;
</code></pre>
The word <code>compound</code> assumes that we have entered the starting
amount, the percent interest per year, and the number of years onto the
stack, as indicated in its stack diagram. Therefore, to solve the problem
of our previous example using the more general word we would type<br><br>
<code>1000 6 10 compound</code><br><br>
and press Enter. The same answer found previously will be displayed. But with
our new word we can also determine the compounded growth after any number
of years (except zero), at any interest rate, and for any starting amount.
To see what our investment will grow to after 20 years, type:<br><br>
<code>1000 6 20 compound</code><br><br>
<p>
To conclude this example, let's modify the word <code>compound</code> so
that it prints a table of the accumulated amount at the end of each year:
<pre><code>
: compound ( nstart npercent nyears -- )
     0 do
       2dup * 100 / 
       rot + 

       i 1+ 2 .r         \ print the year right justified in 2 character field 
       9 emit            \ print a tab 
       dup 6 .r          \ print year ending amount right justified in 6 char field
       cr                \ advance to the next line

       swap
     loop
     2drop
;
</code></pre>
Notice that we made use of the word <code>I</code> in the above
example. <code>I</code> gets the <i>loop index</i> and places it on the
stack. The loop index starts at the number on top of the stack when
<code>DO</code> executes, which is 0 in this example. The loop index 
increments by one after each <code>LOOP</code>. You can look up in the 
<a href="kforth3.html">dictionary</a> other words that may not be 
familiar to you in this example, such as 
<code>1+, .R, EMIT,</code> and <code>CR</code>.<br>
<p>
Finally, it is easy in kForth to send the output from the last example
to a file instead of printing it on the screen. This is done by typing
<pre><code>
>file interest.txt
1000 6 20 compound
console
</code></pre>
The word <code>>FILE</code> redirects output from the screen (console) 
to the file name specified subsequently, <code>interest.txt</code> in
the above example. The word <code>CONSOLE</code> closes the file and 
redirects output back to the screen. We used <code>>FILE</code> and
<code>CONSOLE</code> to send the results of our interest calculations to 
a file, which can then be imported into a spreadsheet to make a 
chart!<br><br>
<hr>
<a href="kforth2c.html"><img src="left.gif"></a><br><br>
&copy;1998--2000 Creative Consulting for Research and Education
</body>
</html>