Arithmetic Problem ?



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Arithmetic Problem ?

Postby gugul » Thu Jan 13, 2011 8:25 pm

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
gugul
 
Posts: 6
Joined: Thu Jan 13, 2011 8:13 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Arithmetic Problem ?

Postby Akatsukami » Thu Jan 13, 2011 9:11 pm

What makes you think that those results are wrong?
"You have sat too long for any good you have been doing lately ... Depart, I say; and let us have done with you. In the name of God, go!" -- what I say to a junior programmer at least once a day
User avatar
Akatsukami
Global moderator
 
Posts: 1058
Joined: Sat Oct 16, 2010 2:31 am
Location: Bloomington, IL
Has thanked: 6 times
Been thanked: 51 times

Re: Arithmetic Problem ?

Postby Robert Sample » Thu Jan 13, 2011 9:13 pm

Can anyone explain me way these results in WS-R1 AND WS-R2 ?
Yes.
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Arithmetic Problem ?

Postby gugul » Thu Jan 13, 2011 9:15 pm

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
gugul
 
Posts: 6
Joined: Thu Jan 13, 2011 8:13 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Arithmetic Problem ?

Postby Robert Sample » Thu Jan 13, 2011 9:21 pm

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.
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Arithmetic Problem ?

Postby gugul » Thu Jan 13, 2011 9:25 pm

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.
gugul
 
Posts: 6
Joined: Thu Jan 13, 2011 8:13 pm
Has thanked: 0 time
Been thanked: 0 time


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post