• Aucun résultat trouvé

ARITHMETIC EXPRESSION PROCESSING

Dans le document TRAX COBOL User's Guide (Page 131-135)

All arithmetic operations (including the numeric elementary MOVE) deliver the ASCII characters 1 through 9 and 0 into all digit

2. ADD ZERO TO BIGFOOT GIVING LITTLEFOOT ON SIZE ERROR

4.6 ARITHMETIC EXPRESSION PROCESSING

NUMERIC CHARACTER HANDLING Forgetting that the MULTIPLY statement, phrase, stores the result back into

(multiplier) .

without the GIVING the second operand

• Performing a series of calculations in such a way as to generate an intermediate result that is larger than 18 digits when the final result will be fewer digits. (The programmer should be careful to intersperse divisions with multiplications or to drop non-significant digits that result from multiplying large numbers (or numbers with many decimal places).

Performing an operation on a field that contains greater than the precision of its data description.

happen only if the field was disarranged by a group redefinition.

a value This can move or

• Forgetting that, in an arithmetic statment containing multiple receiVIng fields, the ROUNDED phrase must be specified for each receiving field that is to be rounded.

• Forgetting that, in an arithmetic statement containing multiple receIving fields, the ON SIZE ERROR phrase, if specified, applies to all receiving fields. Only those receiving operands for which a size error condition is raised are left unaltered. The ON SIZE ERROR imperative statement is executed after all the receiving fields are processed by the OTS.

4.6 ARITHMETIC EXPRESSION PROCESSING

4.6.1 Motivation for Intermediate Results

COBOL provides language facilities for manipulating user-defined data arithmetically. In particular, the language provides the arithmetic statements ADD, SUBTRACT, MULTIPLY, and DIVIDE and the facilities of arithmetic expressions using the +, -,

*, I,

and

**

operators. In simple terms, a given arithmetic functionality may be expressed in one of several ways. For example, consider a COBOL application in which the total yearly sales of a salesman are to be computed as the sum of the four individual sales quarters. Figure 4-6 illustrates one method of expressing a solution to this problem in COBOL:

MOVE 0 TO TEMP.

ADD 1ST-SALES TO TEMP.

ADD 2ND-SALES TO TEMP.

ADD 3RD-SALES TO TEMP.

ADD 4TH-SALES TO TEMP GIVING TOTAL-SALES.

Figure 4-6 Explicit Programmer-Defined Temporary Work Area

In figure 4-6, the COBOL programmer chooses to use a series of single ADD statements to develop the final value for TOTAL-SALES. In the process of computing TOTAL-SALES, a COBOL data-name, called TEMP, is used to develop the partial sums (i.e., intermediate results). The important point here is that the programmer explicitly defines and declares the temporary work area TEMP in the data division of the COBOL program. That is, the attributes (i.e., class, USAGE, number of integer and decimal places to be maintained) are specified explicitly by the COBOL programmer.

Figure 4-7 below illustrates another way of expressing a solution to the problem:

ADD 1ST-SALES, 2ND-SALES, 3RD-SALES, 4TH-SALES GIVING TOTAL-SALES.

Figure 4-7

Arithmetic Statement Intermediate Result Field Attributes Determined from Composite of Operands

NUMERIC CHARACTER HANDLING

In this example, the programmer chooses to compute TOTAL-SALES with a single ADD statement. Analogous to the previous example, an intermediate result. field is required to develop the partial sums of the four quarterly sales quantities. In Figures 4-6, the programmer is cognizant of this requirement, but chose to define the intermediate result area TEMP explicitly in the data division of his COBOL program.

However, for the example in Figure 4-7, the software defines the intermediate result field in a manner transparent to the COBOL source program. That is, the software allocates storage for and assigns various attributes to this "transparent" intermediate result field according to a well-defined set of rules defined by the COBOL language specification. In particular, the attributes of number-of-integer-places, number-of-decimal-places, and USAGE assigned by the software to the intermediate result field are a function of the composite of source operands in the ADD statement. (The reader should read the TRAX COBOL Language Reference Manual for details concerning the composite of operands for the arithmetic statements.) The important point here is that the ANS-74 COBOL language standard prescribes rules for determining the attributes of intermediate result fields for the arithmetic statements and the associated language processor (e.g., TRAX COBOL compiler) must implement those rules.

As a final example, consider the following solution to our problem:

COMPUTE TOTAL-SALES

=

1ST-SALES + 2ND-SALES + 3RD-SALES + 4TH-SALES.

Figure 4-8

Arithmetic Expression Intermediate Result Field Attributes Determined by Implementor-Defined Rules

In Figure 4-8, the programmer solves the problem by using a single COMPUTE statement with an embedded arithmetic expression. Again, an intermediate result field is required and, as in Figure 4-7, is defined by the software. However, in defining the attributes of intermediate result fields for COBOL arithmetic expressions, the ANS-74 COBOL language standard is not as helpful to the user as it could be. In fact, the COBOL language standard gives almost complete freedom to the implementor in defining the attributes of the arithmetic expression intermediate result fields. The only rules imposed by the ANS-74 COBOL language specifications are:

1. Arithmetic operations are to be combined without restrictions on the composite of operands and/or receiving fields.

2. Each implementor will indicate techniques used in handling arithmetic expressions.

Thus, the user can and should expect differences between various implementations of ANS-74 COBOL. The purpose of the remainder of this section is to specify the conceptual algorithms used by the software to compute the attributes for the arithmetic expression intermediate result field; i.e., the number-of-integer-places and number-of-decimal-places to be maintained (for a given intermediate result field) as a function of a particular arithmetic operator and its associated operands.

4.6.2 Intermediate Results for Arithmetic Expressions

In the compile-time and object-time processing of arithmetic expressions, the software maintains a maximum precision of 18 decimal digits for intermediate result fields. One of the major compile-time functions in processing an arithmetic expression operator x is to determine the following attributes for the associated object-time

intermediate result field IR(x):

1. USAGE type,

2. number. of integer places, I(IR(x», to be maintained, and 3. number of decimal places, D(IR(x», to be maintained.

All arithmetic expression intermediate result fields signed data. The software must determine the intermediate result in order to determine the form representation; the number of integer places and determined to know how much object-time storage is

intermediate result.

are treated as USAGE type for an of its internal decimal places are required for the With the exception of the exponentiation operator (**), all infix arithmetic operators yield an intermediate result field whose USAGE type is determined as a function of the USAGE types of its two source operands. That is, if both source operands are DISPLAY, the USAGE type for the intermediate result field is also DISPLAY. If one of the operands associated with an infix operator has a USAGE IS COMPUTATIONAL declaration, the USAGE type of the intermediate result field is also COMPUTATIONAL (i.e., the intermediate result is represented as COMPUTATIONAL data at object-time). The USAGE type of an intermediate result for the exponentiation operator is the same USAGE type as its first operand. Moreover, the only unary operator which requires an intermediate result field is the unary negate operator. In this case, the USAGE type of its intermediate result is the same as its singular operand. The unary plus operator is essentially a "no-op" operator and, thus, is ignored at compile-time and has no impact at object-time.

The process of determining the final number of integer places I(IR(x»

and the final number of decimal places D(IR(x» to be maintained for an intermediate result field IR(x) resulting from an arithmetic expression operator x is conceptually the five step pr~cedure outlined in Figure 4-9 below. In computing the final values for I(IR(x» and D(IR(x», remember that these values are subject to the following criterion:

1<= I(IR(x» + D(IR(x» <= 18.

That is, a maximum prec~s~on of 18 decimal digits is maintained for an intermediate result field.

NUMERIC CHARACTER HANDLING

1. Record the largest number of integer places, lMAX, declared

Dans le document TRAX COBOL User's Guide (Page 131-135)