HP 3000 Manuals

DIVIDE Statement [ HP COBOL II/XL Reference Manual ] MPE/iX 5.0 Documentation


HP COBOL II/XL Reference Manual

DIVIDE Statement 

The DIVIDE statement divides one numeric data item into one or more
others, assigns the result to a data item, and optionally assigns a
remainder to another data item. 

Syntax 

There are five formats of the DIVIDE statement:

[]
[]
Parameters identifier- names of elementary numeric items, except that those 1, associated with a GIVING or REMAINDER phrase may be identifier- elementary numeric-edited items. 2, and so forth literal-1, numeric literals. literal-2, and so forth Description The ROUNDED and SIZE ERROR phrases are described under the heading, "Common Phrases", in Chapter 8 . The composite of operands for the DIVIDE statement is determined using all of the receiving data items of a particular statement except the data item associated with the REMAINDER phrase. This composite must not exceed 18 digits. Refer to Chapter 8 , under "Arithmetic Expressions" for details on how to determine the composite of operands. When format 1 of the DIVIDE statement is used, each identifier following the INTO keyword is divided, in turn, by the identifier or literal to the left of the INTO keyword. Each result is rounded if specified and necessary, and is then stored in the data item referenced by the identifier that acted as the dividend in that particular division. When format 2 is used, the literal or data item specified by the identifier between the keywords INTO and GIVING is divided by the literal or data item specified by identifier-1, and the result is stored in each identifier listed in the GIVING phrase. When the third format of the DIVIDE statement is used, the data item specified by identifier-1 or literal-1 is divided by literal-2 or the contents of identifier-2. The result is then stored in each identifier following the GIVING phrase, with rounding being used where specified and needed. Formats 4 and 5 can be used to obtain a remainder from a division operation. In COBOL, the remainder is defined as the difference between the product of the quotient and the divisor and the dividend. For example, in format 4 of the DIVIDE statement: DIVIDE A INTO B GIVING C REMAINDER D uparrow uparrow uparrow divisor dividend quotient The remainder D has the value determined by multiplying C times A and subtracting this product from B. Thus, if A=7 and B=16, then C=2 and D=2 because 16-7*2=2. If identifier-3 (the quotient) is defined as numeric-edited, the quotient used to calculate the remainder is an internal, intermediate field containing the unedited quotient. Also, if the ROUNDED phrase is specified, the quotient used to calculate the remainder is kept in an intermediate field and is truncated rather than rounded. Appropriate decimal alignment and truncation are performed on the remainder as needed. When the SIZE ERROR phrase is specified for a format 4 or 5 DIVIDE statement, and a size error condition occurs for the quotient, the contents of data items referenced by identifier-3 and identifier-4 are unchanged. However, if the size error condition occurs for the remainder and not the quotient, only the remainder is unchanged. Identifier-3 still contains the new quotient. Example FILE SECTION. FD PAY-FILE. 01 PAY-INFO. 02 EMP-NAME PIC X(30). 02 EMP-NUM PIC X(9). 02 PAY PIC 999V99. 02 HOURS PIC 99. WORKING-STORAGE SECTION. 77 RATE PIC 99 VALUE ZERO. 77 CHECK PIC V99 VALUE ZERO. : PROCEDURE DIVISION. MAIN-100. DIVIDE PAY BY HOURS GIVING RATE REMAINDER CHECK ON SIZE ERROR PERFORM SIZE-ERR. : SIZE-ERR. IF RATE = 0 THEN DISPLAY "SIZE ERROR IN RATE USING " PAY, HOURS ELSE DISPLAY "SIZE ERROR IN CHECK". : The DIVIDE statement above uses format 5. If a size error occurs, the SIZE-ERR routine is performed, and a check is made to determine whether the size error occurred because of RATE or CHECK.


MPE/iX 5.0 Documentation