I am confused with the rounded in the compute function in cobol.
Declaration:
VAR-A PIC S9(9)V99 COMP-3.
VAR-B PIC S9(9)V9(6) COMP-3.
Procedure.
MOVE +12.08 TO VAR-A.
MOVE +6.181657 TO VAR-B.
COMPUTE VAR-A ROUNDED = VAR-A + VAR-B.
Will the result of VAR-A be 18.27 or 18.26? What would cobol do upon computing? Would it round VAR-B first to the decimal places specified in VAR-A or will cobol add the 2 variables then round them up to the decimal places specified in VAR-A?
Any help will be appreciated.
@NealB,
How about this example:
DECLARATION:
01 VAR-ARRAY OCCURS 22 TIMES.
03 VAR-A PIC S9(9)V9(6) COMP-3.
01 VAR-B PIC S9(9)V99 COMP-3.
Supposing VAR-A is an array, and the following are its values:
VAR-A(01) = 123.164612
VAR-A(02) = 12.07865
VAR-A(03) = 6.181657
VAR-A(04) = 1.744353
VAR-A(05) = 6.118182
VAR-A(06) = 1.744353
VAR-A(07) = 6.158715
VAR-A(08) = 1.744353
VAR-A(09) = 6.194759
VAR-A(10) = 1.744353
VAR-A(11) = 3.037896
VAR-A(12) = 1.743852
VAR-A(13) = 6.14653
VAR-A(14) = 1.744353
VAR-A(15) = 0.000377
VAR-A(16) = 1.743852
VAR-A(17) = 6.144363
VAR-A(18) = 1.743852
VAR-A(19) = 0.007649
VAR-A(20) = 1.744353
VAR-A(21) = 0.000377
VAR-A(22) = 1.744353
VAR-B's value is:
VAR-B = 405.25
PROCEDURE:
PERFORM VAR-IDX FROM 1 BY 1 UNTIL VAR-IDX > 22
COMPUTE VAR-B ROUNDED = VAR-B + VAR-A(VAR-IDX)
END-PERFORM.
Why do I get 597.87 for VAR-B as a result after the computation?
I believe the default COBOL rounding behaviour is: Nearest away from zero.
COMPUTE VAR-A ROUNDED = VAR-A + VAR-B
should result in VAR-A
containing 18.26
Rounding occurs after the expression has been evaluated. A more interesting example might be:
01 VAR-A PIC S9(9)V99 COMP-3.
01 VAR-B PIC S9(9)V9(6) COMP-3.
01 VAR-C PIC S9(9)V9(6) COMP-3.
MOVE +12.08 TO VAR-A.
MOVE +06.182000 TO VAR-B.
MOVE +00.004000 TO VAR-C.
COMPUTE VAR-A ROUNDED = VAR-A + VAR-B + VAR-C.
The result is 18.27. Rounding VAR-B
and VAR-C
to 2 decimal places before doing the addition would have yielded 18.26 because VAR-B rounds to 6.18 and VAR-C rounds to 0.00. The result is actually 18.27 so the rounding occurs after evaluation of the expression.
Reply to edited question
Not pretty output, but this is how my calculation goes using IBM Enterprise COBOL for z/OS
VAR-IDX = 01 VAR-B = +405.25 VAR-A = +123.164612 VAR-B + VAR-A = +528.41 VAR-IDX = 02 VAR-B = +528.41 VAR-A = +012.078650 VAR-B + VAR-A = +540.49 VAR-IDX = 03 VAR-B = +540.49 VAR-A = +006.181657 VAR-B + VAR-A = +546.67 VAR-IDX = 04 VAR-B = +546.67 VAR-A = +001.744353 VAR-B + VAR-A = +548.41 VAR-IDX = 05 VAR-B = +548.41 VAR-A = +006.118182 VAR-B + VAR-A = +554.53 VAR-IDX = 06 VAR-B = +554.53 VAR-A = +001.744353 VAR-B + VAR-A = +556.27 VAR-IDX = 07 VAR-B = +556.27 VAR-A = +006.158715 VAR-B + VAR-A = +562.43 VAR-IDX = 08 VAR-B = +562.43 VAR-A = +001.744353 VAR-B + VAR-A = +564.17 VAR-IDX = 09 VAR-B = +564.17 VAR-A = +006.194759 VAR-B + VAR-A = +570.36 VAR-IDX = 10 VAR-B = +570.36 VAR-A = +001.744353 VAR-B + VAR-A = +572.10 VAR-IDX = 11 VAR-B = +572.10 VAR-A = +003.037896 VAR-B + VAR-A = +575.14 VAR-IDX = 12 VAR-B = +575.14 VAR-A = +001.743852 VAR-B + VAR-A = +576.88 VAR-IDX = 13 VAR-B = +576.88 VAR-A = +006.146530 VAR-B + VAR-A = +583.03 VAR-IDX = 14 VAR-B = +583.03 VAR-A = +001.744353 VAR-B + VAR-A = +584.77 VAR-IDX = 15 VAR-B = +584.77 VAR-A = +000.000377 VAR-B + VAR-A = +584.77 VAR-IDX = 16 VAR-B = +584.77 VAR-A = +001.743852 VAR-B + VAR-A = +586.51 VAR-IDX = 17 VAR-B = +586.51 VAR-A = +006.144363 VAR-B + VAR-A = +592.65 VAR-IDX = 18 VAR-B = +592.65 VAR-A = +001.743852 VAR-B + VAR-A = +594.39 VAR-IDX = 19 VAR-B = +594.39 VAR-A = +000.007649 VAR-B + VAR-A = +594.40 VAR-IDX = 20 VAR-B = +594.40 VAR-A = +001.744353 VAR-B + VAR-A = +596.14 VAR-IDX = 21 VAR-B = +596.14 VAR-A = +000.000377 VAR-B + VAR-A = +596.14 VAR-IDX = 22 VAR-B = +596.14 VAR-A = +001.744353 VAR-B + VAR-A = +597.88 FINAL RESULT = +597.88