Topic
2 replies Latest Post - ‏2013-02-11T10:56:22Z by Rob_Nugent
Rob_Nugent
Rob_Nugent
2 Posts
ACCEPTED ANSWER

Pinned topic Intermediate result precision and ROUNDED

‏2013-02-08T13:23:30Z |
Hello,

I wonder if anyone could tell me what the following program does when compiled and run on an IBM Mainframe:

IDENTIFICATION DIVISION.
PROGRAM-ID. A.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 WS-VAR.
10 R PIC 9(8)v9(2).
10 A PIC S9(11) COMP-3.
10 B PIC S9(9) COMP-3.
PROCEDURE DIVISION.
MOVE 3 TO A.
MOVE 7 TO B.

COMPUTE R ROUNDED = ((A / B) * 100.0000).
DISPLAY R.

GOBACK.

I'm trying to understand if the use of ROUNDED forces the intermediate result of the division to 5 decimal places or not. If it does, I'd expect the result 42.86, or 42.85 if it does not.

I'm afraid I don't have access to a mainframe to try this, so any definitive answers as to what it does would be helpful.

As background, I am running the program under MicroFocus COBOL with IBM compatible arithmetic options: -C ARITHMETIC(ENTCOBOL) and it gives 42.85. I'm trying to find out if this is correct or not.

I understand from IBM's documentation that the ROUNDED clause can contribute an extra decimal place to an intermediate result division, but it's unclear to me from the documentation if it does this even when the division is already being done in more decimal places than that of the result field + 1.

Any help would be greatly appreciated

Rob
Updated on 2013-02-11T10:56:22Z at 2013-02-11T10:56:22Z by Rob_Nugent
  • BillWoodger
    BillWoodger
    83 Posts
    ACCEPTED ANSWER

    Re: Intermediate result precision and ROUNDED

    ‏2013-02-09T16:20:50Z  in response to Rob_Nugent
    With what you have coded, the answer is 42.85, whether you use ROUNDED or not.

    
    COMPUTE R ROUNDED = ( ( A * 100 ) / B )
    


    With this COMPUTE, the answer is 42.86. Without the ROUNDED the answer is 42.85.

    Because you do the "* 100" last, you have nothing to "round", as the low-order two digits are guaranteed to be zero.

    You always, in a COMPUTE, need to code the multiplies first and the divides last, otherwise you lose significance - unless the intention is to lose digits, in which case it will be much clearer for the next person along to code separate calculations to make the effect clear.
    • Rob_Nugent
      Rob_Nugent
      2 Posts
      ACCEPTED ANSWER

      Re: Intermediate result precision and ROUNDED

      ‏2013-02-11T10:56:22Z  in response to BillWoodger
      Thanks Bill - that's useful input.