This section contains a number of detailed examples illustrating how various types of radix conversion operations can be performed by the 101 instruction. For a specification of the 101 instruction and the notation used see section 3.10.
If a number, when converted, can be represented by-eight characters or fewer, it can usually be converted by a single 101-instruction, provided that the radices are all integers.
Example 1
If the number in A1 is a binary integer in the range -9999999 ≤ (A1)I ≤ 99999999 (-107+1 ≤ (A1)I ≤ 108-1) it can be converted to characters by
101 CHARS RAD A1
with the program constants
RAD) +100000000
10,10,10,10,10,10,10,10+16
the +16 in the last radix-character ensuring that
zero is converted as 0.
Example 2
If (A1)I represents pence of a sterling quantity in the range -£99.19.11 to £999.19.11 inclusive, then it may be converted to characters with minus-sign if negative, and with full stops between pounds, shillings and pence by
101 CHARS STERL A1
with
STERL) +240000
10,10,10+16,1+32,2,10,1+32,12
This will convert a zero in the 10/- column as SP; to convert it as 0 the 5th radix should be 2+16.
Note that in these two examples, y is the product of the radices (i.e. y = ∏ ri); this is true generally when converting an integer to eight or fewer characters. Note that it is not possible to use the 101-function unless all radices are integral, e.g. to convert ounces to quarters, pounds and ounces would require radices such as 2.8 and 1.6 which cannot be produced.
A case in which y is not the product of the radices
is that of converting a binary fraction to
characters.
Example 3
If, in A1, is a non-negative fraction, it may be converted for printing, to 6 decimal places preceded by 0. Thus:-
51 A1 1
00 A1 RD
101 NUM CON A1
with
RD) +0.00000025
CON) 31,63,63,63,63,63,63,63
2+16,1+32,10,10,10,10,10,10
The word CON) is 01147 = 1.0-2-47, i.e. it is the best single-length approximation to +1.0 attainable. (A1) is shifted down 1 place to prevent the possibility of overflow (a) on adding the rounding constant which is in RD) and (b) on dividing by 1.0-2-47. This shift is compensated by giving the first radix r0 the value 2. This first radix forces a zero which is converted as 0 (because +16). The second radix (l) also forces a zero which is converted as full-stop (or decimal point). Thereafter, the digits proper are produced. Note that since the 0 produced by the radix -character 2+16 is treated as significant, β being set, therefore any left-hand zeros in the fraction will be converted as numeral 0 and not as SP.
If the converted form has more than 8 characters, a
single 101-instruction is not sufficient.
Example 4
Assuming that A1 contains any single-length integer, convert it for output.
FIRST) |
14 |
NUM |
0 |
|
| Clear NUM |
|
101 |
NUM+1 |
CON |
A1 |
| Convert l.s. 8 bits |
|
66 |
FIN |
4 |
|
| Test OVR, if clear, FIN |
|
40 |
A1 |
CON |
|
| if (A1) ≥ 10 |
|
65 |
SEC |
A1 |
|
| Jump if quotient ≥ 0 |
|
60 |
SEC |
A2 |
|
| Jump if remainder = 0 |
|
81 |
SEC |
A1 |
|
| if <0, add 1 & test if ≠ |
|
14 |
NUM |
30 |
|
| if =0, put minus sign in NUM |
|
75 |
... |
0 |
|
| and exit |
CON) |
+100000000 |
|
|||
|
10,10,10,10,10,10,10,10+16 |
|
|||
SEC) |
101 |
NUM |
CON |
A1 |
| convert m.s. 7 digits |
FIN) |
75 |
... |
0 |
|
| and exit |
The first 101-instruction produces the l.s. 8 digits in NUM+1. If the number occupies more than eight characters OVR is set, β is set and θ left clear. OVR is tested and left clear by the 66-instruction; if the number occupies 8 characters or fewer, OVR is clear, the conversion is completed by the one 101-instruction and so the 66-instruction causes a jump to the exit point. Otherwise the number is divided by 108. The quotient in A1 corresponds to the m.s. digits and the remainder in A2 to the l.s. 8 digits.
If this quotient is non-negative, it can be converted directly by a second 101-instruction: the 65-instructicn tests for this case. If the quotient is negative, it may be that all the l.s. 8 characters are zeros (i.e. the number is an integral multiple of 10). If so, the m.s. characters can be directly converted by a second 101-instruction. This case will be indicated by a zero remainder on division by 10, which is detected by the 60-instruction.
The 81-instruction is obeyed if the integer is
negative and converts to more than 8 characters, i.e. N
≤
-108. Suppose in fact, N=-(108a+b) where a and b are
integers. Then when N is divided by 108 (in the 40-instruction) the
quotient will be -(a+1) and the remainder 108-b. The digits of b
have been correctly converted by the first 101-instruction and thus it is
necessary to add 1 to the quotient and convert the result. In fact if, on
adding 1 to the quotient, the result is zero, it follows that OVR was set only
because b has eight digits and the minus sign could not be inserted into NUM+1.
In such a case the 81-instruction does not cause a jump, the minus sign
character is put in the l.s. end of NUM and the sequence left. Otherwise a
second 101-instruction is obeyed, converting the m.s. digits and inserting the
minus sign automatically.
Example 5
To convert a sterling quantity in the range
-£9,999,999,999.19.11 to £99,999,999,999.19.11 inclusive, held as pence in A1.
|
14 |
NUM |
0 |
|
|
101 |
NUM+1 |
CON |
A1 |
|
66 |
FIN |
4 |
|
|
40 |
A1 |
CON |
|
|
65 |
SEC |
A1 |
|
|
60 |
SEC |
A2 |
|
|
10 |
A1 |
1 |
|
|
61 |
SEC |
A1 |
|
|
14 |
NUM |
30 |
|
|
75 |
... |
0 |
|
CON) |
+240000 |
|
|
|
|
10,10,10+16,1+32,2,10,1+32,12 |
|||
|
+100000000 |
|
|
|
|
10,10,10,10,10,10,10,10 |
|||
SEC) |
101 |
NUM |
CON+2 |
A1 |
FIN) |
75 |
... |
0 |
|
The reasoning behind this sequence of instructions is
similar to that for Example 4. Note that the 81-instruction of Example 4
is'replaced here by a 10-instruction and a 61-instruction. This is because, if
a sterling quantity in the given range is divided by 240000 the quotient may
exceed 24 bits and thus cannot be tested completely by an 81-instruction.
Example 6
Given a signed fraction P in A1 , convert it to 13 decimal places, preceded by SP0. if positive or by -0. if negative, allowing also for the case of -1.0000000000000
|
14 |
A3 |
0 |
|
| Clear A3 |
|
65 |
EB |
A1 |
|
| Test if F ≥ 0 |
|
66 |
BB |
10 |
A1 |
| If < 0, test if F=-1.0 |
|
12 |
A1 |
0 |
|
| If ≠ -1.0, negate fraction |
|
115 |
A3 |
30 |
|
| and put minus-sign in A3 |
BB) |
32 |
A1 |
CON+4 |
|
| Multiply F by 105 |
|
31 |
A2 |
CON+2 |
|
| Multiply l.s. half by 108, rounded |
|
61 |
CC |
CON+2 |
A2 |
| Test if latter product = 108 |
|
10 |
A1 |
1 |
|
| If so, add 1 to m.s.digits |
|
14 |
A2 |
0 |
|
| and clear l.s.digits |
CC) |
101 |
NUM |
CON |
A1 |
| Convert m.s.digits |
|
101 |
NUM |
CON+2 |
A2 |
| Convert l.s.digits |
|
00 |
NUM |
A3 |
|
| Add sign if F negative |
|
75 |
.... |
O |
|
| Exit |
CON) |
+200000 |
|
|||
|
1,2+16,1+32,10,10,10,10,10 |
||||
|
+100000000 |
|
|||
|
10+16,10,10,10,10,10,10,10 |
||||
|
+100000 |
In this method of solution the two multiplication instructions are used to transform the fraction into two integers, that in A1 representing the five m.s. decimal places and that in A2 representing the l.s. eight decimal places. The first radix, 1, in CON+1 serves to force a zero, converted as SP in which the minus sign may be set when appropriate.