Calculate rounded in cobola

I got confused with the rounded cob computation function.

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 VAR-A result be 18.27 or 18.26? What will the cobola do for computing? Will he round VAR-B to the decimal places indicated in VAR-A, or will cobol add 2 variables and then round them to decimal places indicated in VAR-A?

Any help would 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. 

Assuming VAR-A is an array, and its following 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 value:

 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 am I getting 597.87 for VAR-B as a result after calculation?

+7
source share
3 answers

I believe the default rounding behavior is COBOL: Nearest 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 adding would result in 18.26, because VAR-B rounds are 6.18 and VAR-C rounds are 0.00. The result is actually 18.27, so rounding occurs after evaluating the expression.

Answer to an editable question

Not a very good conclusion, but this is how my calculations are used 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                                               
+5
source

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

see this for more information:

D.13a Rounding

COBOL provides the ability to specify rounding in arithmetic operations and expressions at different points in the evaluation process and as values ​​are prepared for storage in the receiving data elements.

There are eight different rounding forms supported by this standard:

β€’ AWAY-FROM-ZERO: round to the nearest higher value.

β€’ NEAREST-AWAY-FROM-ZERO: round to the nearest value. If the two values ​​are equally close, a value with a larger value is selected. This mode has historically been associated with the ROUNDED clause in previous versions of the standard COBOL.

β€’ NEAREST-EVEN: round to the nearest value. If the two values ​​are equally close, then select the value whose rightmost digit is even. This regime is sometimes called "banker rounding."

β€’ NEAREST-TOWARD-ZERO: round to the nearest value. If the two values ​​are equally close, a value with a smaller value is selected.

β€’ FORBIDDEN: since the value cannot be represented exactly in the required format, the condition EC-SIZE-TRENCTION is set, and the results of the operation are undefined.

β€’ TOWARD-GREATER: Rounding is the closest value whose algebraic value is greater.

β€’ TOWARD-LESSER: Rounding goes to the nearest value whose algebraic value is less.

β€’ TRUNCATION: round to the nearest value that is smaller. This mode has historically been associated with the lack of a ROUNDED clause, as well as for generating intermediate results in the previous COBOL standard.

The programmer can specify how individual intermediate values ​​are rounded when they are stored in the receiving data elements through the ROUNDED clause; can choose the default rounding mode that will be used when the ROUNDED clause appears without additional qualification in the receiving data element through the DEFAULT ROUNDED MODE clause of the IDENTIFICATION SEPARATION OPTIONS section; and may indicate how arithmetic operations and conversions to and from intermediate forms are rounded through the SIZE OF ROUND statement.

D.13a.1 Intermediate rounding

Intermediate rounding is used when data elements are retrieved for inclusion in an arithmetic operation or arithmetic expression and during the execution of arithmetic operators to obtain an intermediate result.

In the previous standard for multiplication and division in standard arithmetic, the default rounding mode for inaccurate results was truncation to 32 significant digits. This default value does not change in this standard and is also standard for Standard-Binary and Standard-Decimal arithmetic.

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

In the event that the value cannot be accurately represented, the user can now also specify other rounding modes for arithmetic operations and for conversion to intermediate forms and from intermediate forms used in arithmetic operations, through the optional INTERMEDIATE ROUNDING clause of the OPTIONS section of the IDENTIFICATION SEPARATION.

In particular, the following options are available:

β€’ INTERMEDIATE EQUIPMENT NEAREST FROM ZERO β€’ INTERMEDIATE REDUCTION CLOSER - EVEN β€’ INTERMEDIATE REDUCTION IS FORBIDDEN β€’ INTERMEDIATE EQUIPMENT - TUBING

for which descriptions of subclauses are found in D.13a, Rounding.

If the INTERMEDIATE ROUNDING clause is not specified, it is assumed that the INTERMEDIATE DEFENSE is carried out - TRUCCATION. This has not changed compared to previous standards.

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

The final rounding is applied to the formation of the final result of the expression or operator when the evaluation of the operator or expression is completed immediately before the result is placed at the destination. This rounding form is what is associated with the ROUNDED clause.

In previous COBOL standards, only two β€œfinal” rounding methods were provided: rounding to a lower value (truncation signaled by the absence of a ROUNDED clause); and rounding to the nearest values, and if the two values ​​were equally close, select the value with a larger value (signaled by the presence of the ROUNDED clause).

The ROUNDED clause has been expanded to provide an explicit choice of any of eight rounding modes (including two previously available):

β€’ ROUND MODE NO RT-ZERO β€’ ROUND MODE NEAREST-FROM ZERO β€’ ROUND MODE TELE - EVEN β€’ ROUND MODE TELE - ROUND - the ZERO β€’ ROUND mode is disabled β€’ ROUND-MODE REAL-LARGE β€’ ROUND MODE violated-Leser β€’ ROUND MODE TRENATSIYA

If a ROUNDED clause is missing for this result, the rules for ROUNDED MODE IS TRUNCATION apply.

An optional DEFENSE OPERATOR clause is provided in the IDENTIFICATION SEPARATION OPTIONS section to allow the user to specify the rounding mode for any operation for which the ROUNDED clause appears without the MODE IS submenu.
The DEFAULT ROUNDED MODE clause may take any of these forms:

β€’ DEFAULT ROUNDED MODE IS AW-FROM-ZERO β€’ DEFAULT ROUND MODE - NEAREST-ZERO β€’ SPEED DEFAULT REDUCES β€’ DEFAULT ROUNDED MODE NEAREST-TOWARD-ZERO CUTTLE-CUTTED β€’ DEFAULT ROUNDED MODE - TOWARD-LESSER β€’ DEFAULT ROUNDED MODE TRRUCATION

for which the DEFAULT ROUNDED MODE clauses are described in D.13a, Rounding.

If the DEFAULT ROUNDED MODE clause is not displayed in the program, the effect of the ROUNDED clause without the MODE IS submenu will be as if the ROUNDED MODE IS NEAREST AWAY FROM ZERO has been specified. This provides the same functionality as previous COBOL standards.

If a DEFAULT ROUNDED MODE clause appears, ROUNDED clauses without the MODE IS submenu are processed as if they were specified in the rounding mode specified in the DEFAULT ROUNDED MODE clause.

+1
source
 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. 
-one
source

All Articles