EasyManua.ls Logo

Commodore PET - Page 221

Commodore PET
447 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Method 2: Multiple Integer Subtraction
The alternative method to numeric string subtraction
is
multiple integer
subtraction. Recall from the previous discussion of multiple integer addition
that
the multiple integer method divides a large number into smaller segments. calcu-
lates the segments separately, and joins the answers into one string. This method
evades the
9-digit
length limit.
The total process of multiple integer subtraction
is
as
follows:
1.
Input
the minuend and subtrahend
as
two
positive numeric strings.
2.
Determine
which
string
has
the larger value.
3.
Divide the numbers into parts: high, low.
4.
Calculate the difference of low- and high-order digits.
5.
Concatenate the differences into a one-string answer.
6.
Truncate leading zeros.
7.
Print the answer string.
At
the end of the subtraction section
is
the sample program used to demonstrate
multiple integer subtraction.
Step
1:
Input the minuend and the subtrahend
as
two
positive numeric
strings:
113
PRINT":")IE.lIEMULTIPLE
INTEGER
SUBTRACTIONlIElIElIt":PRINT
2(1
INPUT
A$,
B$
.1IE1IEMULTIPLE
INTEGER
SUBTRACTIONliElIElIE
?12345678ge12
??57943572
A$, the minuend, and B$, the subtrahend. are entered
as
strings to avoid the
9-digit
length limit.
Like multiple integer addition,
A$
and B$ are divided into smaller segments.
The maximum input length
is
arbitrarily set at 16 digits, so that
we
can divide the
largest possible string into equal segments of
eight
digits each.
Step 2: Determine
which
input string has the larger value. If
A$
is
equal
to B$ then the program drops
down
to line 1190 to
print
a zero answer.
If
B$
is
larger than A$ the difference
is
negative and extra steps
are
needed.
If the answer
is
to be negative. the contents of the
two
strings are switched
to put the larger value in
A$
and the smaller value in
B$.
They
are
then subtracted.
and a negative sign
("
-")
is
concatenated onto the
front
of
the difference
(C$)
as
was demonstrated in line 70
of
"Numeric String Subtraction" (page 204). Line 30
is
used here to direct the program past the
switching
routine if switching
is
not
needed.
30
IF
VALCA$»VALCB$)
THEN
1800
413
X$=A$:A$=B$:B$=X$
58
$=1
208

Table of Contents

Related product manuals