compute rounded in cobol

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                                               

It depends on the intermediate rounding and the final rounding set.

see this for more info :

D.13a Rounding

COBOL provides the capability of specifying rounding in arithmetic statements and expressions at various points in the evaluation process and as values are prepared for storing in receiving data items.

There are eight different forms of rounding supported by this standard:

• AWAY-FROM-ZERO: Rounding is to the nearest value of larger magnitude.

• NEAREST-AWAY-FROM-ZERO: Rounding is to the nearest value. If two values are equally near, the value with the larger magnitude is selected. This mode has historically been associated with the ROUNDED clause in previous versions of standard COBOL.

• NEAREST-EVEN: Rounding is to the nearest value. If two values are equally near, the value whose rightmost digit is even is selected. This mode is sometimes called “Banker's rounding”.

• NEAREST-TOWARD-ZERO: Rounding is to the nearest value. If two values are equally near, the value with the smaller magnitude is selected.

• PROHIBITED: Since the value cannot be represented exactly in the desired format, the EC-SIZE-TRUNCATION condition is set to exist and the results of the operation are undefined.

• TOWARD-GREATER: Rounding is toward the nearest value whose algebraic value is larger.

• TOWARD-LESSER: Rounding is toward the nearest value whose algebraic value is smaller.

• TRUNCATION: Rounding is to the nearest value whose magnitude is smaller. This mode has historically been associated with the absence of the ROUNDED clause as well as for the formation of intermediate results in the prior COBOL standard.

The programmer may specify how individual intermediate values are rounded when they are stored into receiving data items through the ROUNDED clause; may select a default mode of rounding to be used when the ROUNDED clause appears with no further qualification on a receiving data item through the DEFAULT ROUNDED MODE clause of the OPTIONS paragraph of the IDENTIFICATION DIVISION; and may specify how arithmetic operations and conversions to and from intermediate forms are rounded through the INTERMEDIATE ROUNDING clause.

D.13a.1 Intermediate rounding

Intermediate rounding applies when data items are retrieved for inclusion in an arithmetic operation or arithmetic expression, and during the execution of arithmetic operators to produce an intermediate result.

In the previous standard, for multiplication and division in Standard Arithmetic, the default mode of rounding for inexact results was truncation to 32 significant digits. This default is unchanged in this standard, and is also the default for Standard-Binary and Standard-Decimal arithmetic.

When the intermediate value can be represented exactly in the appropriate intermediate format, the exact value is used.

In the event the value cannot be exactly represented, the user may also now specify other modes of rounding for arithmetic operations and for the conversions to and from intermediate forms used in the arithmetic operations through the optional INTERMEDIATE ROUNDING clause of the OPTIONS paragraph of the IDENTIFICATION DIVISION.

Specifically, the following options are available:

• INTERMEDIATE ROUNDING IS NEAREST-AWAY-FROM-ZERO • INTERMEDIATE ROUNDING IS NEAREST-EVEN • INTERMEDIATE ROUNDING IS PROHIBITED • INTERMEDIATE ROUNDING IS TRUNCATION

for which the subclause descriptions are found in D.13a, Rounding.

If the INTERMEDIATE ROUNDING clause is not specified, INTERMEDIATE ROUNDING IS TRUNCATION is presumed. This is unchanged from previous standards.

D.13a.2 Final rounding (the ROUNDED clause)

Final rounding applies to the formation of the final result of the expression or statement, at the completion of evaluation of the statement or expression, immediately before the result is placed in the destination. This form of rounding is that which is associated with the ROUNDED clause.

In previous COBOL standards, only two methods of “final” rounding were provided: rounding toward the smaller magnitude (truncation, signaled by the absence of the ROUNDED clause); and rounding to the nearest values, and if two values were equally near, choose the value with the larger magnitude (signaled by the presence of the ROUNDED clause).

The ROUNDED clause has been enhanced to allow explicit selection of any of eight modes of rounding (including the two previously available):

• ROUNDED MODE IS AWAY-FROM-ZERO • ROUNDED MODE IS NEAREST-AWAY-FROM-ZERO • ROUNDED MODE IS NEAREST-EVEN • ROUNDED MODE IS NEAREST-TOWARD-ZERO • ROUNDED MODE IS PROHIBITED • ROUNDED-MODE IS TOWARD-GREATER • ROUNDED MODE IS TOWARD-LESSER • ROUNDED MODE IS TRUNCATION

If the ROUNDED clause is not present for a given result, the rules for ROUNDED MODE IS TRUNCATION apply.

The optional DEFAULT ROUNDED MODE clause in the OPTIONS paragraph of the IDENTIFICATION DIVISION is provided to allow the user to specify the mode of rounding to any operation for which the ROUNDED clause appears without the MODE IS subclause.
The DEFAULT ROUNDED MODE clause may take any of these forms:

• DEFAULT ROUNDED MODE IS AWAY-FROM-ZERO • DEFAULT ROUNDED MODE IS NEAREST-AWAY-FROM-ZERO • DEFAULT ROUNDED MODE IS NEAREST-EVEN • DEFAULT ROUNDED MODE IS NEAREST-TOWARD-ZERO • DEFAULT ROUNDED MODE IS PROHIBITED • DEFAULT ROUNDED MODE IS TOWARD-GREATER • DEFAULT ROUNDED MODE IS TOWARD-LESSER • DEFAULT ROUNDED MODE IS TRUNCATION

for which the subclauses of the DEFAULT ROUNDED MODE is clause are described in D.13a, Rounding.

If the DEFAULT ROUNDED MODE clause does not appear in the program, the effect of the ROUNDED clause without the MODE IS subclause is as if ROUNDED MODE IS NEAREST AWAY FROM ZERO had been specified. This provides the same functionality available in prior COBOL standards.

If the DEFAULT ROUNDED MODE clause appears, ROUNDED clauses without the MODE IS subclause are treated as if they had been specified with the rounding mode specified in the DEFAULT ROUNDED MODE clause.


IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.

01  VAR_NUM PIC  9(3)V9(02).
01  VAR_RESULT PIC 9(3).


PROCEDURE DIVISION.
    MOVE 256.50 TO VAR_NUM.
    COMPUTE VAR_NUM ROUNDED = VAR_NUM / 100. 
    MULTIPLY 100 BY VAR_NUM
    MOVE VAR_NUM TO VAR_RESULT.
   DISPLAY "Result : " VAR_RESULT.

STOP RUN.
链接地址: http://www.djcxy.com/p/55044.html

上一篇: 使用类表继承时的访问级别问题

下一篇: 计算在cobol四舍五入