ROUNDED and truncation
COBOL does not round unless it is told to. A value that needs more decimal places than the receiving picture has is cut, digit by digit, and the statement succeeds. ROUNDED is the one word that changes that, and the rule it follows is not the one most programmers expect.
The digit a cut costs
Two thirds in a field of two decimals, with and without the word, and a receipt that is a third of a cent short when the cut is used:
IDENTIFICATION DIVISION.
PROGRAM-ID. ROUND.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 DX-F PIC 9(1)V99 VALUE 0.
01 R9-F PIC 9 VALUE 0.
01 NEG-F PIC S9 VALUE 0.
PROCEDURE DIVISION.
COMPUTE DX-F = 2 / 3.
DISPLAY "cut [" DX-F "]".
COMPUTE DX-F ROUNDED = 2 / 3.
DISPLAY "rounded [" DX-F "]".
COMPUTE R9-F = 2.5.
DISPLAY "2.5 cut [" R9-F "]".
COMPUTE R9-F ROUNDED = 2.5.
DISPLAY "2.5 rounded [" R9-F "]".
COMPUTE R9-F ROUNDED = 3.5.
DISPLAY "3.5 rounded [" R9-F "]".
COMPUTE NEG-F ROUNDED = -2.5.
DISPLAY "-2.5 rounded [" NEG-F "]".
STOP RUN.
cobc -x -free -Wall -o round round.cob
./round
cut [0.66]
rounded [0.67]
2.5 cut [2]
2.5 rounded [3]
3.5 rounded [4]
-2.5 rounded [-3]
The first pair is the ordinary case: 0.666 and a half rounds up to 0.67. ROUNDED is written between the receiving field and the equals sign, and it applies to the whole statement.
The half-up rule
A value that ends in exactly five goes away from zero, not to the nearest even digit: 2.5 becomes 3, 3.5 becomes 4 and minus 2.5 becomes minus 3. The rule is the one taught at school, and it is not the rule of IEEE floating point, which sends 2.5 to 2. A COBOL program that rounds a price and a C program that rounds the same price can differ by a cent, and the difference is this line.
The two integer cases in the run are the proof. Without ROUNDED, 2.5 stored in a PIC 9 is 2. With it, the same statement gives 3. Nothing about the field changed; the statement was told to round.
The third decimal that is exactly half
The interesting case is a literal with one digit too many for the receiver, because the cut and the rounding differ by a whole cent and the literal is the kind of value a program is full of:
IDENTIFICATION DIVISION.
PROGRAM-ID. HALF.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A-FLD PIC 9(1)V99 VALUE 0.
01 B-FLD PIC 9(1)V99 VALUE 0.
PROCEDURE DIVISION.
COMPUTE A-FLD = 1.005.
DISPLAY "cut [" A-FLD "]".
COMPUTE B-FLD ROUNDED = 1.005.
DISPLAY "rounded [" B-FLD "]".
STOP RUN.
cobc -x -free -Wall -o half half.cob
./half
cut [1.00]
rounded [1.01]
One and five thousandths is closer to 1.01 than to 1.00, and the plain statement stores 1.00 because it looks at nothing beyond the second decimal. The rounded one looks at the digit after the last that fits, sees a five, and goes up. This is the cent that a tax calculation loses when the statement that computes it has no ROUNDED.
ROUNDED on the verbs
The word works the same on DIVIDE, MULTIPLY and ADD, and each of them has a statement whose last digit is decided by it:
IDENTIFICATION DIVISION.
PROGRAM-ID. VERBS2.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUT-F PIC 9(1)V99 VALUE 0.
01 ROUND-F PIC 9(1)V99 VALUE 0.
PROCEDURE DIVISION.
DIVIDE 6 INTO 1 GIVING CUT-F.
DISPLAY "divide cut [" CUT-F "]".
DIVIDE 6 INTO 1 GIVING ROUND-F ROUNDED.
DISPLAY "divide rounded [" ROUND-F "]".
MULTIPLY 0.005 BY 1 GIVING CUT-F.
DISPLAY "multiply cut [" CUT-F "]".
MULTIPLY 0.005 BY 1 GIVING ROUND-F ROUNDED.
DISPLAY "multiply round [" ROUND-F "]".
STOP RUN.
cobc -x -free -Wall -o verbs2 verbs2.cob
./verbs2
divide cut [0.16]
divide rounded [0.17]
multiply cut [0.00]
multiply round [0.01]
A sixth is 0.1666 and a half of a cent is 0.005; both are values whose rounding is visible in a field of two decimals. The first of each pair loses the third digit, the second looks at it.
Where the word goes
The position is fixed and small: after the receiving field, and after GIVING when there is one. COMPUTE X ROUNDED = ..., DIVIDE A INTO B GIVING C ROUNDED, ADD A B GIVING C ROUNDED. In a statement without GIVING the receiver is also an operand, so the word sits at the end: DIVIDE 6 INTO 1 ROUNDED is a division into a field, rounded.
The rule of thumb for a program that handles money is to round at the point where a decimal field is written, and not earlier. Rounding every intermediate value to cents and then adding is not the same as adding and rounding once, and the difference accumulates; the lesson on a running total measures it, and the one after that finds the bug it causes.
Exercises
- Compute 1 / 7 into a field with four decimals, with and without ROUNDED. Which digit differs?
- Round 0.015, 0.025 and 0.035 into two decimals and compare with the values a binary float would give. Which one is not the school answer?
- Add ROUNDED to the division of 100.00 by 7 in the lesson before this one and check the total with REMAINDER again. What changed?
- Write a statement that computes a price with three decimals of tax, rounds it into a field of two, and then multiplies by a quantity. Is the result the same as rounding after the multiplication?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a COMPUTE do with a result that has more decimals than the receiver?
Answer
It cuts the extra digits, unless the statement says ROUNDED
Truncation is the default for every arithmetic statement; ROUNDED is the clause that changes it.
-
How does COBOL round an exact half?
Answer
Away from zero: 2.5 becomes 3 and -2.5 becomes -3
ROUNDED follows the half-up rule of school arithmetic, which differs from the IEEE rule for exactly representable halves.
-
Where does ROUNDED go in
DIVIDE A INTO B GIVING C?Answer
After C, at the end of the statement
The clause follows the receiving field, which is the last operand of the statement.
-
Why does rounding once at the end differ from rounding every step?
Answer
Each rounding moves the value by up to half a unit, and the moves accumulate
A rounded intermediate is already wrong by up to half a cent before the next operation reads it, and the errors add up.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 DX-F PIC 9(1)V99 VALUE 0. PROCEDURE DIVISION. COMPUTE DX-F = 2 / 3. DISPLAY "[" DX-F "]". STOP RUN.Answer
[0.66]
The third decimal of 0.666 is dropped without ROUNDED, so the field holds 0.66.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 DX-F PIC 9(1)V99 VALUE 0. PROCEDURE DIVISION. COMPUTE DX-F ROUNDED = 2 / 3. DISPLAY "[" DX-F "]". STOP RUN.Answer
[0.67]
0.666 rounds up at the second decimal to 0.67, because the digit after it is six.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 R9-F PIC 9 VALUE 0. PROCEDURE DIVISION. COMPUTE R9-F = 2.5. DISPLAY "[" R9-F "]". STOP RUN.Answer
[2]
Without ROUNDED the field of one digit takes the integer part, so 2.5 becomes 2.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 R9-F PIC 9 VALUE 0. PROCEDURE DIVISION. COMPUTE R9-F ROUNDED = 3.5. DISPLAY "[" R9-F "]". STOP RUN.Answer
[4]
An exact half rounds away from zero, so 3.5 becomes 4 rather than the even 4 or the truncated 3.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-FLD PIC 9(1)V99 VALUE 0. 01 B-FLD PIC 9(1)V99 VALUE 0. PROCEDURE DIVISION. COMPUTE A-FLD = 1.005. COMPUTE B-FLD ROUNDED = 1.005. DISPLAY "[" A-FLD "][" B-FLD "]". STOP RUN.Answer
[1.00][1.01]
The plain statement cuts at two decimals and the rounded one looks at the five and goes up.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 NEG-F PIC S9 VALUE 0. PROCEDURE DIVISION. COMPUTE NEG-F ROUNDED = -2.5. DISPLAY "[" NEG-F "]". STOP RUN.Answer
[-3]
The half goes away from zero, so minus 2.5 becomes minus 3 and the signed picture prints the minus.
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