2. Create subroutines for data conversion, array sorting, and collecting terms using a high-level programming language.
3. Input the subroutines into your computer.
4. Get results based on numerical data.
5. Compile your experimentation paper.
Contents of the Experimentation Paper
1. Principles of the binary number system.
2. Principles of DL coding.
3. Algorithms of the subroutines of array sorting and collecting terms.
Questions for the Self-Testing
1. What is the DL representation of zero?
2. What is the DL structure of a fixed point number?
3. What is the DL structure of a floating point number?
Laboratory work 3.2
REALIZATION OF DL ADDITION AND SUBTRACTION OPERATIONS
Aim of the work: The aim of the work is to study DL addition and subtraction operations.
Basic Principles
The operation of addition is performed according to the bit-by-bit addition rule: if Ni ≠ Nk then Ni + Nk = Ni, Nk; if Ni = Nk then Ni + Nk = Ni + 1, where Ni and Nk are non-zero digits of the operands.
The summands are represented as arrays (sets) of DL codes. The operands are processed beginning from the least significant bits via the following procedures. Bit-by-bit comparison is performed on the numbers A→{sign, Q(A), N1, N2, , Ni, , Nm} and B→{sign, Q(B), N1, N2, , Nj , Nk}: if Ni ≠ Nj, both elements are written in the result C in the order of increasing. Otherwise (Ni = Nj), the following takes place: the value of Ni + 1 is assigned to some intermediate variable and is compared with the next elements of the numbers A and B, i.e., with Ni+1 and Nj+1. If equality is not detected then Ni + 1 is written in the result. Otherwise, another iteration of comparison is performed. Note, that in the case A = B the result C is equal to one of the summands shifted one position to the left, i.e.,
C→{sign, Q(A), N1+1, N2+1, , Ni+1, , Nm+1}.
Example. Given: A→{0,5,7.6.3.2.0} and B→{0,3,6.4.1}. Their sum is to be found. Having A compared bit-by-bit with B, we find out that both numbers contain the same element {6}. This element is incremented by 1→7 and compared with the next elements of A and B. B has no elements greater than 6 and A has {7}, thats why this element is incremented again by 1→8. Neither A nor B has more elements, so the final result is: C→{0,6,8.4.3.2.1.0}.
Subtraction is performed according to the bit-by-bit subtraction rule:
if Ni = Nk, then Ni - Nk.= 0 and nothing is written to the result;
if Ni > Nk, an array Ni-1, Ni-2, , Nk is generated and substituted to the result;
if Ni < Nk, two variants are possible:
1) the value Ni is substituted to the result and Nk is included as an element to the negative array of the difference thus forming a new subtrahend, which decreases the obtained difference in the next cycle of computation to have the result as one array;
2) the value Ni is substituted to the result, and Nk is included as an element to the negative array of the difference thus forming the result as two arrays.
The subtraction error may result from limitations on a maximal value of resultant array elements and limitations on the size of this ar-ray. The limitations on the size are imposed at the last stage of operation execution, so they are absent at the intermediate stages of rounding.
In the general case the operation of subtraction is performed as adding the minuend without the most significant digit to the subtrahend represented in the complement code. By analogy with the binary system, the least significant digit of a given range (0 for integers) is added at the last stage of operation execution. The complement code is formed like this: we take the most significant digit of the minuend decremented by one and write all the digits in the decreasing order without those in the subtrahend.
Example. Given: A→{0,3,8.3.2} and B→{0,4,6.5.4.3}. Their difference is to be found. A and B are positive numbers and A > B. The subtrahend B is written in the complement code like this: the most significant digit of the minuend A is 8, 8 1 = 7; B1→{0,4,7.2.1.0}. A without the most significant digit {0,2,3.2.} and B1 are added, the intermediate result is C1→{0,4,7.4.1.0}, the final result is
C=C1+0.→{0,3,7.4.2}.
It is advisable to unite the algorithms of addition and subtraction into one generalized subroutine because they have common stages of execution. Such a subroutine SumDL is shown in Fig. 3.4, where the following variables are used: a and b are the integer arrays of the first and second DL summands accordingly; c is the integer array of the DL sum; mod is a variable used to determine the greater operand in absolute terms; and i is a cycle index.
Before performing the operation of addition, the operands should be checked for their equality to zero. If a[1] = 0, the value of the array b are assigned to the array c. If b[1] = 0, the value of the array a are assigned to the array c. The signs of the operands must be checked for their equality before addition. If they are the same (a[0] = b[0]) then the sign of any of them is assigned to the result, e.g. c[0] = a[0] and the subroutine ADD of DL addition of the absolute values of the operands is carried out (Fig. 3.5).
In executing the subroutine ADD, values of the arrays a and b are written alternatively to the array s. After this, the array s is sorted in decreasing order of its elements (subroutine SORT) and collecting terms is done (subroutine COL) (see Laboratory work 3.1).
If the signs of the operands are different, the greater modulus is found using the subroutine MOD (Fig. 3.6), which restores the value mod=0 if a>b and mod=1 if a<b. In the first case the sign value of the operand a is assigned to the sign of the result (c[0] = a[0]) and the subroutine SUB of DL subtraction with the minuend a and subtrahend b is carried out. In the second case the sign value of the operand b is assigned to the sign of the result (c[0] = b[0]) and the subroutine SUB is performed with the minuend b and subtrahend a. The result is written to the array c.
The flowchart of the DL subtraction routine SUB is depicted in Fig. 3.7. The following variables are used here: a is the integer array of the DL minuend; b is the integer array of the DL subtrahend; c is the integer array of the DL remainder; ar is the minuend integer array without the most significant digit; br is the subtrahend integer array defining its complement code; variables i, j, k, n, and the integer array b1 are used for cycle organization.