COMPUTE and arithmetic expressions

COMPUTE is the one COBOL verb that takes an expression: the receiving field on the left, an equals sign, and arithmetic with the signs that every other language uses. It is not a replacement for ADD and MOVE, which a shop may prefer for auditability, but it is the only way to write a formula with brackets in one line.

One statement, one expression

Two, three and four, used in five expressions, with the receiving field printed each time:

IDENTIFICATION DIVISION. PROGRAM-ID. EXPR. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-VAL PIC 9 VALUE 2. 01 B-VAL PIC 9 VALUE 3. 01 C-VAL PIC 9 VALUE 4. 01 X-VAL PIC S9(3) VALUE 0. 01 Y-VAL PIC 9(3)V99 VALUE 0. PROCEDURE DIVISION. COMPUTE X-VAL = A-VAL + B-VAL * C-VAL. DISPLAY "A + B * C = " X-VAL. COMPUTE X-VAL = (A-VAL + B-VAL) * C-VAL. DISPLAY "(A + B) * C = " X-VAL. COMPUTE X-VAL = A-VAL ** 3. DISPLAY "A ** 3 = " X-VAL. COMPUTE X-VAL = -A-VAL * B-VAL. DISPLAY "-A * B = " X-VAL. COMPUTE Y-VAL = A-VAL / B-VAL. DISPLAY "A / B = " Y-VAL. STOP RUN.

cobc -x -free -Wall -o expr expr.cob
./expr

A + B * C = +014
(A + B) * C = +020
A ** 3 = +008
-A * B = -006
A / B = 000.66

The first two lines are the whole subject. Without brackets the multiplication is done first, so 2 + 3 * 4 is fourteen; with brackets the sum is done first and the answer is twenty. The receiving field prints its sign because its picture has an S, and the division prints two decimals because that is all its picture holds.

Precedence and brackets

Four levels, from tight to loose: unary minus, then **, then * and /, then + and -. Operators of the same level are applied from left to right, except **, which is applied from the right: 2 ** 3 ** 2 is 2 ** 9 and not 8 ** 2.

Brackets override all of it, and a formula from a specification should be written with them even where they are not needed. The reader of A * B + C * D has to reconstruct the precedence, and the reader of (A * B) + (C * D) does not.

One COMPUTE against several verbs

The same three values computed two ways. The verbs apply in the order they are written, so the first program multiplies before it adds:

IDENTIFICATION DIVISION. PROGRAM-ID. SAME. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-VAL PIC 9(3) VALUE 10. 01 B-VAL PIC 9(3) VALUE 5. 01 C-VAL PIC 9(3) VALUE 2. 01 SUM-F PIC 9(5) VALUE 0. 01 SUM2-F PIC 9(5) VALUE 0. PROCEDURE DIVISION. ADD A-VAL TO SUM-F. MULTIPLY B-VAL BY SUM-F. ADD C-VAL TO SUM-F. DISPLAY "verbs [" SUM-F "]". COMPUTE SUM2-F = (A-VAL + C-VAL) * B-VAL. DISPLAY "compute [" SUM2-F "]". COMPUTE SUM2-F = A-VAL + C-VAL * B-VAL. DISPLAY "nobrack [" SUM2-F "]". STOP RUN.

cobc -x -free -Wall -o same same.cob
./same

verbs [00052]
compute [00060]
nobrack [00020]

Ten, times five, plus two is fifty-two; ten plus two, times five is sixty; ten plus two times five is twenty. All three are correct for the formula they express, and the two COMPUTE lines differ only in the brackets. A chain of verbs is not wrong, it is a different formula from the one a reader may assume, and that is the argument for a single COMPUTE when the formula has more than one operation.

Powers, and the sign in front

** raises to a power and binds tighter than multiplication, and the unary minus binds tighter still, which is the opposite of what a reader of school algebra expects:

IDENTIFICATION DIVISION. PROGRAM-ID. UNARY. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-VAL PIC 9 VALUE 2. 01 X-VAL PIC S9(3) VALUE 0. PROCEDURE DIVISION. COMPUTE X-VAL = -A-VAL ** 2. DISPLAY "minus A ** 2 [" X-VAL "]". COMPUTE X-VAL = (-A-VAL) ** 2. DISPLAY "brackets [" X-VAL "]". COMPUTE X-VAL = 2 ** 3 ** 2. DISPLAY "2 ** 3 ** 2 [" X-VAL "]". STOP RUN.

cobc -x -free -Wall -o unary unary.cob
./unary

minus A ** 2 [+004]
brackets [+004]
2 ** 3 ** 2 [+512]

-A ** 2 is four and not minus four, because the minus is applied to A before the power is taken: the expression is (-2) ** 2. The brackets change nothing and are the way to write it if four is what is meant; a minus four needs -(A ** 2), which the compiler reads as a negation of the whole power. The third line is the right-to-left rule of **: 2 ** 3 is eight and 2 ** 9 is 512.

Division inside an expression

A / B in the first program printed 0.66 because two thirds was cut to the two decimals of the receiving field. The cut happens once, at the end of the expression, and the digits before it are kept in a temporary field that is wider than the receiver; what the temporary holds and what it loses is the subject of intermediate precision, and the way to control the last digit is ROUNDED.

Exercises

  1. Write the formula for the mean of three fields with COMPUTE and with verbs. Which of the two reads closer to the formula?
  2. Change 2 ** 3 ** 2 to (2 ** 3) ** 2 and print the result. Why is it different?
  3. Compute -A ** 3 for A equal to 2 and explain the sign of the answer from the rule of this lesson.
  4. Write COMPUTE X = A / B * C and COMPUTE X = A * C / B with A=1, B=3, C=3, both into two decimals, and compare. Which one is exact?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which operation is done first in COMPUTE X = A + B * C?

    Answer

    The multiplication, then the addition

    Multiplication and division bind tighter than addition and subtraction, so the product is computed before the sum.

  2. What do brackets change in a COMPUTE?

    Answer

    The order of evaluation, and therefore the value when the operators differ

    Brackets override precedence, which is why A + B * C and (A + B) * C give different results.

  3. What does ** do, and in which direction does it group?

    Answer

    It raises to a power, and it groups from the right: 2 ** 3 ** 2 is 2 ** 9

    The power operator is right-associative in COBOL, so the rightmost pair is evaluated first.

  4. What is -A ** 2 when A is 2?

    Answer

    4, because the minus applies to A before the power

    The unary minus is part of the operand here, so the expression is (-2) ** 2; -(A ** 2) is the way to write minus four.

  5. What does this program print? (A is 2, B is 3, C is 4.) IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-VAL PIC 9 VALUE 2. 01 B-VAL PIC 9 VALUE 3. 01 C-VAL PIC 9 VALUE 4. 01 X-VAL PIC S9(3) VALUE 0. PROCEDURE DIVISION. COMPUTE X-VAL = A-VAL + B-VAL * C-VAL. DISPLAY "[" X-VAL "]". STOP RUN.

    Answer

    [+014]

    The product of 3 and 4 is 12, and 2 is added to it; the plus is a sign because the picture has an S.

  6. What does this program print? (A is 2, B is 3, C is 4.) IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-VAL PIC 9 VALUE 2. 01 B-VAL PIC 9 VALUE 3. 01 C-VAL PIC 9 VALUE 4. 01 X-VAL PIC S9(3) VALUE 0. PROCEDURE DIVISION. COMPUTE X-VAL = (A-VAL + B-VAL) * C-VAL. DISPLAY "[" X-VAL "]". STOP RUN.

    Answer

    [+020]

    The brackets add 2 and 3 first, and the sum is multiplied by 4.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-VAL PIC 9 VALUE 2. 01 X-VAL PIC S9(3) VALUE 0. PROCEDURE DIVISION. COMPUTE X-VAL = A-VAL ** 3. DISPLAY "[" X-VAL "]". STOP RUN.

    Answer

    [+008]

    Two cubed is eight, and the signed picture prints the plus in front of it.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-VAL PIC 9 VALUE 2. 01 B-VAL PIC 9 VALUE 3. 01 X-VAL PIC S9(3) VALUE 0. PROCEDURE DIVISION. COMPUTE X-VAL = -A-VAL * B-VAL. DISPLAY "[" X-VAL "]". STOP RUN.

    Answer

    [-006]

    The minus applies to A, so the product of minus two and three is minus six.

  9. What does this program print? (A is 2 and B is 3.) IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-VAL PIC 9 VALUE 2. 01 B-VAL PIC 9 VALUE 3. 01 Y-VAL PIC 9(3)V99 VALUE 0. PROCEDURE DIVISION. COMPUTE Y-VAL = A-VAL / B-VAL. DISPLAY "[" Y-VAL "]". STOP RUN.

    Answer

    [000.66]

    Two thirds is cut at the two decimal places of the receiving picture, and without ROUNDED the third digit is dropped.

  10. What does the chain of verbs print? (A is 10, B is 5, C is 2, SUM starts at 0.) IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-VAL PIC 9(3) VALUE 10. 01 B-VAL PIC 9(3) VALUE 5. 01 C-VAL PIC 9(3) VALUE 2. 01 SUM-F PIC 9(5) VALUE 0. PROCEDURE DIVISION. ADD A-VAL TO SUM-F. MULTIPLY B-VAL BY SUM-F. ADD C-VAL TO SUM-F. DISPLAY "[" SUM-F "]". STOP RUN.

    Answer

    [00052]

    The verbs run in the order written, so the sum is multiplied before the last addition: 10 * 5 + 2.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image