The results achieved were 14.90 and 15.00.
COMPUTER is poorly formed. When multiplying by 100, do it as soon as possible, because if you do this last, two decimal places will disappear (when divided by 100, do this last so that you do not lose significant numbers at an early stage).
This applies not only to 100, so think about it. Multiply first, divide the latter, whatever values ​​are involved. There is no need to use a field with five decimal places to get the “correct” answer if you do not need five decimal places in the final answer.
A computer is not a calculator or spreadsheet. It uses intermediate results depending on what you ask for, not others that give you a lot of decimal places. You request decimal precision using the number of decimal places in the fields you use.
One key to understanding what is going on is accurate guidance. The other is an experiment. This can often be the general answer.
COMPUTE WSAA-RESULT ROUNDED = (WSAA-AMOUNT-A / ( WSAA-AMOUNT-B-01 + WSAA-AMOUNT-C) * 100 )
DISPLAY WSAA-RESULT. gets a response of 14.90 (fourteen points nine-zero).
COMPUTE WSAA-RESULT = (WSAA-AMOUNT-A / ( WSAA-AMOUNT-B-01 + WSAA-AMOUNT-C) * 100 )
DISPLAY WSAA-RESULT. Gets a 14.00 (fourteen zero zero) response, removing ROUNDED.
COMPUTE WSAA-RESULT ROUNDED = (WSAA-AMOUNT-A / ( WSAA-AMOUNT-B-02 + WSAA-AMOUNT-C) * 100 )
DISPLAY WSAA-RESULT receives a 15.00 (fifteen zero zero) “correct” “expected” response using five decimal places.
Remember that you finally multiplied by 100. So before that, the values ​​were 0.149 (three decimal places), 0.14 (two decimal places) and 14.99999n (six decimal places, and I'm not going to do the calculation again same, so I left the sixth as "n").
Why are there three, two, and six decimal places? Six due to B-02 (with five places plus one for ROUNDED, which increase the required decimal numbers by one so that ROUNDED can be calculated), two due to B-01 (with two decimal places) three due to B -01 and ROUNDED.
The following is COMPUTE re-running in three versions:
COMPUTE WSAA-RESULT ROUNDED = ( ( WSAA-AMOUNT-A * 100 ) / ( WSAA-AMOUNT-B-01 + WSAA-AMOUNT-C ) ) COMPUTE WSAA-RESULT = ( ( WSAA-AMOUNT-A * 100 ) / ( WSAA-AMOUNT-B-01 + WSAA-AMOUNT-C) ) COMPUTE WSAA-RESULT ROUNDED = ( ( WSAA-AMOUNT-A * 100 ) / ( WSAA-AMOUNT-B-02 + WSAA-AMOUNT-C) )
Results: 15.00, 14.99 and 15.00.
This is with Enterprise Cobol. OpenCobol seems to use different intermediate results, which are probably documented.
Here is the Enterprise Cobol programming guide for intermediate results:
"To understand this information about subtotals, you need to understand the following terminology. ... d The number of decimal places carried for the subtotal. (If you use the phrase ROUNDED, another decimal place can be delivered if necessary for accuracy.) Dmax B a particular statement, the largest of the following items: v The number of decimal places required for the final result field or fields v The maximum number of decimal places defined for any operand except divisors or exponents v External-dmax for any functional operand "
and
"Operation ... Decimal places + or - ... d1 or d2, whichever is greater * ... d1 + d2 / ... (d2 - d1) or dmax, whichever is greater"
When using the “erroneous” example, adding gets two decimal places (both d1 and d2 - two for adding), division gets three decimal places (dmax is three), and multiplication gets three (two + zero + one for ROUNDED). The final answer after trying to round (it will never work, since the two least decimal places are always zero due to multiplication by 100), is truncated to two decimal places.
For ROUNDED B-01 dmax - three (a result field of two places, plus one for rounding). For a simple B-01, dmax is two. For ROUNDED B-02 dmax six.
Please note that not only is an example shown which is incorrect. ROUNDED never works in the first example in the question, and one significant decimal place is always discarded, so you only get the correct answer while the answer is in tenths before rounding. These correct answers are then masked by nine more answers that become the same due to truncation.
If you always write effective COMPUTE, you will not run into problems. ROUNDED only works with the end result. If you need any intermediate ROUNDED fields, calculate them separately and use the result in a new calculation.
COMPUTE works. COMPUTE does not work like a calculator / spreadsheet. Also no other verb is Cobol (they are not verb functions). Consider writing COMPUTE so as not to lose value. Read the manual. Experiment. Repeat until correct and understandable.