HP OpenVMS Systems Documentation |
HP COBOL
|
Previous | Contents | Index |
Rounding is an important option that you can use with arithmetic operations.
You can use the ROUNDED phrase with any HP COBOL arithmetic statement. Rounding takes place only when the ROUNDED phrase requests it, and then only if the intermediate result has low-order digits that cannot be stored in the result.
HP COBOL rounds off by adding a 5 to the leftmost truncated digit of the absolute value of the intermediate result before it stores that result.
Table 2-4 shows several ROUNDING examples.
PICTURE clause | Initial Value | |
---|---|---|
03 ITEMA PIC S9(5)V9999. | 12345.2222 | |
03 ITEMB PIC S9(5)V99. | 54321.11 | |
03 ITEMC PIC S9999. | 1234 | |
03 ITEMD PIC S9999P. | 0 | |
03 ITEME PIC S99V99 VALUE 9. | 9.00 | |
03 ITEMF PIC S99V99 VALUE 24. | 24.00 | |
Arithmetic Statement | Intermediate Result |
ROUNDED Result Value |
ADD ITEMA TO ITEMB ROUNDED. | 066666.3322 | 66666.33 |
MULTIPLY ITEMC BY 2
GIVING ITEMD ROUNDED. |
02468 | 02470 1 |
DIVIDE ITEME INTO ITEMF
ROUNDED. |
02.666 | 02.67 |
DIVIDE ITEME INTO ITEMF
GIVING ITEMC ROUNDED. |
02.666 | 0003 |
The remainder computation uses an intermediate field that is truncated,
rather than rounded, when you use the DIVIDE statement with both the
ROUNDED and REMAINDER options.
2.7.5 Using the SIZE ERROR Phrase
The SIZE ERROR phrase detects the loss of high-order nonzero digits in the results of HP COBOL arithmetic operations. It does this by checking the absolute value of an arithmetic result against the PICTURE character-string of each resultant identifier. For example, if the absolute value of the result is 100.05, and the PICTURE character-string of the resultant identifier is 99V99, the SIZE ERROR phrase detects that the high-order digit, 1, will be lost, and the size error condition will be raised.
You can use the phrase in any HP COBOL arithmetic statement.
When the execution of a statement with no ON SIZE ERROR phrase results in a size error, and native arithmetic is used, the values of all resultant identifiers are undefined. When standard arithmetic is used, or when the same statement includes an ON SIZE ERROR phrase, receiving items for which the size error exists are left unaltered; the result is stored in those receiving items for which no size error exists. The ON SIZE ERROR imperative phrase is then executed.
If the statement contains both ROUNDED and SIZE ERROR phrases, the result is rounded before a size error check is made.
The SIZE ERROR phrase cannot be used with numeric MOVE statements. Thus, if a program moves a numeric quantity to a smaller numeric item, it can lose high-order digits. For example, consider the following move of an item to a smaller item:
01 AMOUNT-A PIC S9(8)V99. 01 AMOUNT-B PIC S9(4)V99. . . . MOVE AMOUNT-A TO AMOUNT-B. |
This MOVE operation always loses four of AMOUNT-A's high-order digits. The statement can be tailored in one of three ways, as shown in the following example, to determine whether these digits are zero or nonzero:
1. IF AMOUNT-A NOT > 9999.99 MOVE AMOUNT-A TO AMOUNT-B ELSE ... 2. ADD ZERO AMOUNT-A GIVING AMOUNT-B ON SIZE ERROR ... 3. COMPUTE AMOUNT-B = AMOUNT-A ON SIZE ERROR ... |
All three alternatives allow the MOVE operation to occur only if AMOUNT-A loses no significant digits. If the value in AMOUNT-A is too large, all three avoid altering AMOUNT-B and take the alternate execution path.
You can also use a NOT ON SIZE ERROR phrase to branch to, or perform,
sections of code only when no size error occurs.
2.7.6 Using the GIVING Phrase
The GIVING phrase moves the intermediate result of an arithmetic operation to a receiving item. The phrase acts exactly like a MOVE statement in which the intermediate result serves as the sending item, and the data item following the word GIVING serves as the receiving item. When a statement contains a GIVING phrase, you can have a numeric-edited receiving item.
The receiving item can also have the ROUNDED phrase. If the receiving item is also numeric-edited, rounding takes place before the editing.
The GIVING phrase can be used with the ADD, SUBTRACT, MULTIPLY, and DIVIDE statements. For example:
ADD A,B GIVING C. |
Both the ADD and SUBTRACT statements can contain a series of operands preceding the word TO, FROM, or GIVING.
If there are multiple operands in either of these statements, the operands are added together. The intermediate result of that operation becomes a single operand to be added to or subtracted from the receiving item. In the following examples, TEMP is an intermediate result item:
1. | Statement: | ADD A,B,C,D, TO E,F,G,H. |
Equivalent coding: |
ADD A, B, GIVING TEMP.
ADD TEMP, C, GIVING TEMP. ADD TEMP, D, GIVING TEMP. ADD TEMP, E, GIVING E. ADD TEMP, F, GIVING F. ADD TEMP, G, GIVING G. ADD TEMP, H, GIVING H. |
|
2. | Statement: | SUBTRACT A, B, C, FROM D. |
Equivalent coding: |
ADD A, B, GIVING TEMP.
ADD TEMP, C, GIVING TEMP. SUBTRACT TEMP FROM D, GIVING D. |
|
3. | Statement: | ADD A,B,C,D, GIVING E. |
Equivalent coding: |
ADD A,B, GIVING TEMP.
ADD TEMP, C, GIVING TEMP. ADD TEMP, D, GIVING E. |
As in all HP COBOL statements, the commas in these statements are
optional.
2.7.8 Common Errors in Arithmetic Statements
Programmers most commonly make the following errors when using arithmetic statements:
Previous | Next | Contents | Index |