Page 1 of 1

Arithmetic Problem ?

PostPosted: Thu Jan 13, 2011 8:25 pm
by gugul
Hi.

I´ve these variables:
01 WS-R1 PIC 9(3)V99 VALUE ZEROS.
01 WS-R2 PIC 9(3)V99 VALUE ZEROS.
01 WS-R3 PIC 9(3)V99 VALUE ZEROS.
01 WS-R4 PIC 9(3)V99 VALUE ZEROS.
01 WS-8N99 PIC 9(3)V999 VALUE 8,99.
01 WS-99N9 PIC 9(3)V999 VALUE 99,9.

COMPUTE WS-R1 ROUNDED = WS-8N99 / WS-99N9 * 100
COMPUTE WS-R2 = WS-8N99 / WS-99N9 * 100
COMPUTE WS-R3 = WS-8N99 * 100 / WS-99N9
COMPUTE WS-R4 ROUNDED = WS-8N99 * 100 / WS-99N9
DISPLAY WS-R1 '/' WS-R2 '/' WS-R3 '/' WS-R4

Results:
00890/00890/00899/00900

Why WS-R2 and WS-R3 are diferent? And WS-R1 & WS-R4 are different ?
The correct result is WS-R3 and WS-R4
WS-R1 & WS-R2 are wrong, try using a calculator.

Can anyone explain me way these results in WS-R1 AND WS-R2 ?

Thanks

Re: Arithmetic Problem ?

PostPosted: Thu Jan 13, 2011 9:11 pm
by Akatsukami
What makes you think that those results are wrong?

Re: Arithmetic Problem ?

PostPosted: Thu Jan 13, 2011 9:13 pm
by Robert Sample
Can anyone explain me way these results in WS-R1 AND WS-R2 ?
Yes.

Re: Arithmetic Problem ?

PostPosted: Thu Jan 13, 2011 9:15 pm
by gugul
Akatsukami wrote:What makes you think that those results are wrong?


Beacause if you do it in a calculater, the result is different !

Its 8.99 rounded = 9.00

And "Cobol" says is 8.90

Re: Arithmetic Problem ?

PostPosted: Thu Jan 13, 2011 9:21 pm
by Robert Sample
The mere fact that a calculator gives you different results does not mean COBOL is wrong. You do not understand how COBOL calculates and uses intermediate results -- and therefore your statements about what the results "should" be are completely and utterly WRONG.

Example: for WS-R2, COBOL divides first and comes up with 0.890 and some fraction. The fraction is truncated since the maximum number of digits after the decimal point is 3 in your two variables, so the intermediate result is 0.890. This is multiplied by 100 giving 8.90 -- and this is not a wrong calculation; this is the way COBOL works.

If you ever take a class in numerical computation, you will discover that such results are normal (not specific to COBOL, either), expected, and one reason that multiplication should be done before division when doing calculations. Do not compare the results of a computer language calculation with results from a calculator, or Excel, or anything else and expect the results to match -- they may, but they may not. And this is normal.

Re: Arithmetic Problem ?

PostPosted: Thu Jan 13, 2011 9:25 pm
by gugul
Robert Sample wrote:The mere fact that a calculator gives you different results does not mean COBOL is wrong. You do not understand how COBOL calculates and uses intermediate results -- and therefore your statements about what the results "should" be are completely and utterly WRONG.

Example: for WS-R2, COBOL divides first and comes up with 0.890 and some fraction. The fraction is truncated since the maximum number of digits after the decimal point is 3 in your two variables, so the intermediate result is 0.890. This is multiplied by 100 giving 8.90 -- and this is not a wrong calculation; this is the way COBOL works.

If you ever take a class in numerical computation, you will discover that such results are normal (not specific to COBOL, either), expected, and one reason that multiplication should be done before division when doing calculations. Do not compare the results of a computer language calculation with results from a calculator, or Excel, or anything else and expect the results to match -- they may, but they may not. And this is normal.


Ok thanks.