Microsoft KB Archive/58176

COBOL Rounding Problem Using ROUNDED with DIVIDE

PSS ID Number: Q58176 Article last modified on 04-20-1993

3.00 3.00a | 3.00 3.00a MS-DOS | OS/2

Summary: The sample program below demonstrates a problem with Microsoft COBOL versions 3.0 and 3.0a and numeric rounding with the ROUNDED clause. Microsoft has confirmed this to be a problem in Microsoft COBOL versions 3.0 and 3.0a. This problem has been corrected in Microsoft COBOL Professional Development System version 4.0.

More Information: Dividing numbers that range in the hundreds produces invalid calculations when using the ROUNDED clause. These problems only seem to be related to division of numbers in the hundreds. Modifying the divisor and the denominator smaller or larger, making sure that the same ratio is maintained, does not produce the problem. The following are possible workarounds for the problem: 1. Increase the precision of your working-storage variable by defining it as PIC S9(3)v9(6). You can then use COMPUTE data-item-1 ROUNDED = data-item-2 to a variable that you originally had, PIC S9(3)v9(5). To use this workaround, remove the single asterisk (*) from the code below. 2. You can use a COMP variable instead. To use this workaround, remove the double asterisks (**) from the code below.

Code
IDENTIFICATION DIVISION. PROGRAM-ID. ROUNDERROR. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-VARIABLES. 03 WS-TEMP                 PIC S9(3)V9(5). **   03  WS-TEMP                 PIC S9(3)V9(5) COMP. *   03  TEMP-TEMP               PIC S9(3)V9(6). PROCEDURE DIVISION. MAIN-LINE SECTION. 000.      DIVIDE  345 BY 365 GIVING WS-TEMP  ROUNDED. *   COMPUTE TEMP-TEMP ROUNDED = WS-TEMP. *   DISPLAY &quot;345  / 365 ROUNDED  = &quot; TEMP-TEMP. DISPLAY &quot;345 / 365 ROUNDED  = &quot; WS-TEMP. DIVIDE 345 BY 365 GIVING WS-TEMP. DISPLAY &quot;345 / 365          = &quot; WS-TEMP. DIVIDE 34 BY 36  GIVING WS-TEMP  ROUNDED. DISPLAY &quot;34  / 36  ROUNDED  = &quot; WS-TEMP. DIVIDE 34 BY 36  GIVING WS-TEMP. DISPLAY &quot;34  / 36           = &quot; WS-TEMP. DIVIDE 3450 BY 3650 GIVING WS-TEMP  ROUNDED. DISPLAY &quot;3450 / 3650 ROUNDED = &quot; WS-TEMP. DIVIDE 3450 BY 3650 GIVING WS-TEMP. DISPLAY &quot;3450 / 3650        = &quot; WS-TEMP. STOP RUN. Additional reference words: 3.00 3.00a Copyright Microsoft Corporation 1993.