Microsoft KB Archive/58176

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 19:25, 12 August 2020 by X010 (talk | contribs) (X010 moved page Microsoft KB Archive/Q58176 to Microsoft KB Archive/58176 without leaving a redirect: Text replacement - "Microsoft KB Archive/Q" to "Microsoft KB Archive/")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

The information in this article applies to:
- Microsoft COBOL for MS-DOS and OS/2, versions 3.0 and 3.0a

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 "345  / 365 ROUNDED  = " TEMP-TEMP.
       DISPLAY "345  / 365 ROUNDED  = " WS-TEMP.
       DIVIDE  345 BY 365 GIVING WS-TEMP.
       DISPLAY "345  / 365          = " WS-TEMP.
       DIVIDE  34 BY 36  GIVING WS-TEMP  ROUNDED.
       DISPLAY "34   / 36  ROUNDED  = " WS-TEMP.
       DIVIDE  34 BY 36  GIVING WS-TEMP.
       DISPLAY "34   / 36           = " WS-TEMP.
       DIVIDE  3450 BY 3650 GIVING WS-TEMP  ROUNDED.
       DISPLAY "3450 / 3650 ROUNDED = " WS-TEMP.
       DIVIDE  3450 BY 3650 GIVING WS-TEMP.
       DISPLAY "3450 / 3650         = " WS-TEMP.
       STOP RUN.

Additional reference words: 3.00 3.00a Copyright Microsoft Corporation 1993.