ARITHMETIC COMPUTATIONS - IBM Mainframe

The COMPUTE statement is efficient and easy to read. You can code one COMPUTE statement instead of individual arithmetic statements. Perform arithmetic computations with the COMPUTE statement, except when division produces a remainder (if you need to process the remainder). You use arithmetic operators in the COMPUTE statement. These operators are evaluated in the hierarchic order:

  • Unary operator (+or-)
  • Exponentiation (**)
  • Division or Multiplication (/ or *)
  • Addition or subtraction (+ or -)

Operators at the same level are evaluated from left to right; however, you can use parentheses with these operators to change the order in which they are evaluated. Expressions within parentheses are evaluated before any of the individual operators are evaluated. In any case, parentheses, whether necessary or not, make your program easier to read. When using COMPUTE, you have to make sure the data is defined with the level of significance you need for an accurate answer. For large data items, intermediate results may be truncated.

Binary (BINARY) Items

A binary item (USAGE BINARY) is allocated storage ranging from 1 halfword to 2 fullwords, depending on the number of digits in the PICTURE clause. Table 56.2 shows three binary operations in which the result field is an item described as PIC S9 BINARY and the TRUNC(OPT) option is in effect. One halfword of storage has been allocated and no ON SIZE ERROR option is involved. (If the ON SIZE ERROR option had been specified, it would have been executed for cases B and C.)

Binary Operations

Binary Operations

Binary Operations

Binary Data Alignment

On some processors, binary items aligned on halfword or fullword boundaries can improve performance. You can align data using the SYNCHRONIZED clause, or by providing slack bytes.

  • When you use the SYNCHRONIZED clause for binary items in working storage, the compiler does all the adjusting for you by inserting slack bytes within the record. You need to consider how the compiler may change the record length.
  • When you use the SYNCHRONIZED clause for binary items in the File Section, you have to set up the level-01 records on the proper boundaries. To do this, you should consider the slack bytes between records needed for proper alignment.

The SYNCHRONIZED clause is meaningless for PACKED-DECIMAL and DISPLAY items, and the compiler ignores it.

Floating-Point (C0MP-1, C0MP-2, and DISPLAY) Items

If any operation in an arithmetic expression will be computed in floating point, the entire expression is computed as though all operands were converted to floating point. Therefore, be aware of what values may be moved between fixed and floating-point items without losing digits. In all cases, the basic COBOL rules for moving data items are followed.

The following results occur when values are moved between fixed and floating-point items. (Because fixed-point and external floating-point items both have decimal characteristics, reference to fixed-point items in the following examples generally includes external floating-point items as well. When external-floating point is handled differently, it is specifically stated.)

  • If a fixed-point data item with 6 or fewer digits is moved to a USAGE COMP-1 data item and then returned to the fixed-point data item, the original value is recovered.
  • If a USAGE COMP-1 data item is moved to a fixed-point data item of 9 or more digits and then returned to the USAGE COMP-1 data item, the original value is recovered. When a USAGE COMP-1 data item is moved to a fixed-point data item with more than 9 digits, the fixed-point data item will receive only 9 significant digits, and all the remaining digits will be zero.
  • If a fixed-point data item with 15 or fewer digits is moved to a USAGE COMP-2 data item and then returned to the fixed-point data item, the original value is recovered.
  • If a USAGE COMP-2 data item is moved to a fixed-point (not external floating-point) data item of 18 digits and then returned to the USAGE COMP-2 data item, the original value is recovered.
  • If a USAGE COMP-1 data item, a USAGE COMP-2 data item, an external floating-point data item, or a floating-point literal is moved to a fixed-point data item, the extra low-order digits are rounded.
  • If a USAGE COMP-2 data item is moved to a USAGE COMP-I data item, the extra low-order digits-are rounded.
  • If a fixed-point data item is moved to an external floating-point data item where the PICTURE of the fixed-point data item specifies more digit positions than the PICTURE of the external floating-point data item, the extra low-order digits are rounded.

Data Movement Support for Numeric Operands in VS COBOL II

Data Movement Support for Numeric Operands in VS COBOL II

Data Movement Support for Numeric Operands in VS COBOL II


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

IBM Mainframe Topics