Chapter One, Part B, Executive Computing
The Program INFLATE
Welcome back to my quixotic quest to convert a series of business programs written in Level II BASIC from the very early 80’s into runnable Forth code for Tali Forth 2 running on my 65C02 retro computer.
While we haven’t made it out of chapter 1 yet, our next program is more ambitious. Three different inflation rates are tracked. See pages 4-7 in Executive Computing (“EC”)1. See the comments in the code for an idea of the goal. Nevison’s code is very nicely organized and formatted, as we’ll see throughout the book.
BASIC version
100 REM INFLATE 1 JANUARY 1980 STEVE CAULDWELL
110 '
112 REM COPYRIGHT 1980 JOHN M. NEVISON ASSOCIATES
114 '
120 REM REVEALS PROFIT MARGIN IN AN INFLATIONARY
130 REM WORLD WHERE RAW MATERIAL COSTS, LABOR COSTS,
140 REM AND PRICES EACH GROW AT A DIFFERENT RATE.
145 '
150 REM VARIABLES:
154 REM L............ LABOR COST
155 REM P............ PRICE
156 REM R............ RAW MATERIAL COST
158 '
200 PRINT "YR RAW MAT.", "LABOR", "TOTAL COST",
210 PRINT "PRICE", "MARGIN (%)"
211 PRINT
220 '
270 LET P = 100
280 LET R = 56
290 LET L = 21
300 PRINT O; R, L, R+L, P, (P-(R+L))/P * 100
310 FOR Y = 1 TO 5
315 LET P = 1.12 * P
320 LET R = 1.03 * R
330 LET L = 1.15 * L
340 PRINT Y; R, L, R+L, P, (P-(R+L))/P * 100
350 NEXT Y
999 END
One possible Forth version
OK, let’s tackle this in Forth. First off, we need to handle percentages and
cents in a language without built-in support for floating point numbers. From
the last post Chapter One, part 1, Executive Computing, you
may remember the discussion of the scaling operators, */ and m*/. However,
because the numbers in this scenario are well within the range for single-cell
numbers in Tali Forth, we can just do a more straightforward port of the BASIC
program, except that we’ll enter all the numbers as cents rather than dollars
and cents.
Here’s a first go, wherein I write Python in Forth. :) As you can see,
this tracks the BASIC program pretty closely, except that we are making the
program a bit more interactive. And this time, I’m using Forth variables, in
place of doing everything on the stack. Also, I’ve added a couple of helper
words to insert a decimal point in the right place and to print a percent
symbol.
\ 100 * profit / price = % profit or margin
: tab 9 emit ;
: .00 0 <# # # [char] . hold #s #> type space ;
: .% [char] % emit ;
variable price
variable materials
variable labor
\ Expressed as percentages 103%, etc.
103 constant materials+
115 constant labor+
: % ( n percent -- n ) 100 */ ;
: cost labor @ materials @ + ;
: profit price @ cost - ;
: margin 100 profit price @ */ ;
: header ( --)
cr ." YR MATL LABOR TOT COST PRICE MARGIN" cr ;
: init
10000 price !
5600 materials !
2100 labor !
;
\ Call with % increase; 107 for 7% etc.
: scenario ( price+% -- )
init
header
6 0 do
i . tab materials @ .00 tab labor @ .00 tab
cost .00 tab price @ .00 tab margin 6 u.r .% cr
dup price @ swap % price !
materials @ materials+ % materials !
labor @ labor+ % labor !
loop drop ;
In the scenario in the book, the manager is trying to find the lowest price
increase (price+) that will preserve a 23% profit margin target. He begins
with a 12% price increase but eventually backs it down to 7% and achieves the
target. The BASIC version shows 12%. Here is the Forth output with both 12%
(the BASIC version) and 7% (the number required to actually achieve the goal).
112 scenario
YR MATL LABOR TOT COST PRICE MARGIN
0 56.00 21.00 77.00 100.00 23%
1 57.68 24.15 81.83 112.00 26%
2 59.41 27.77 87.18 125.44 30%
3 61.19 31.93 93.12 140.49 33%
4 63.02 36.71 99.73 157.34 36%
5 64.91 42.21 107.12 176.22 39%
ok
107 scenario
YR MATL LABOR TOT COST PRICE MARGIN
0 56.00 21.00 77.00 100.00 23%
1 57.68 24.15 81.83 107.00 23%
2 59.41 27.77 87.18 114.49 23%
3 61.19 31.93 93.12 122.50 23%
4 63.02 36.71 99.73 131.07 23%
5 64.91 42.21 107.12 140.24 23%
Well, that’s it for this week! I hope this will give ideas for your own little programs. Thanks for reading this far.
-
Nevison, J. M., (1981). Executive Computing–how to get it done on your own. Addison-Wesley Publishing Company, Inc. https://archive.org/details/executivecomputi00nevi/page/n1/mode/2up ↩