MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Accuracy in Computing

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming
View previous topic :: View next topic  
Author Message
rasprasads
Beginner


Joined: 10 Dec 2002
Posts: 59
Topics: 20
Location: Chennai

PostPosted: Thu Sep 11, 2003 4:26 am    Post subject: Accuracy in Computing Reply with quote

I have defined 3 variables :

Code:
01  WS-A        PIC S9(4)V9(2) COMP-3 VALUE +900.03. 
01  WS-B        PIC S9(7)V9(2) COMP-3 VALUE +1800.33.
01  WS-C        PIC S9(3)V9(2) COMP-3 VALUE ZEROES.   


And proceeded with my calculation :
CASE 1 :
Code:
COMPUTE WS-C = ( WS-A / WS-B ) * 100. 


The correct result should have been : 49.99
But this case 1 yielded the result : WS-C = 49.00

From some manuals i learnt that there will be a intermediate value used in compute and the size of this is determined by the largest of WS-A and WS-C in this case and so could have been truncated as 0.49 in the first division and hence the result of 49.00. So i tried using the ROUNDED Option.

CASE 2:
I again tried with the following code :
Code:

COMPUTE WS-C ROUNDED = ( WS-A / WS-B ) * 100.   


Which yielded the result : WS-C = 49.90

1. How does ROUNDED option have effect in the COMPUTE ? (How it gives 49.90 here)
2. How to get the exact result ? (49.99 in this case)
3. Can i get the correct result without increasing the size of the variables ?


**********Thanks**********
_________________
Rasprasad S
Back to top
View user's profile Send private message
somuk
Beginner


Joined: 04 Feb 2003
Posts: 113
Topics: 37

PostPosted: Thu Sep 11, 2003 8:38 am    Post subject: Reply with quote

Hi Rasprasad ,
Let us take an equation F=(A/B)*C.

Compute performs the following operations in sequence.
1.Value A is divided by B and is stored in a temporary register, say R1.
2.Value C is multiplied with the register value R1.
Now the problem is with the picture clause of the (space allocation) register that stores the intermediate result.

Try the following method (I got this information from a friend).
Declare another variable
Code:

01 WS-CORRECTION-FACTOR     PIC 9(1)V9(17) VALUE ZEROES.

Then change your compute statement as follows.
Code:

COMPUTE WS-C = ( WS-A / WS-B ) * 100 + WS-CORRECTION-FACTOR .

hth
-Somu
Back to top
View user's profile Send private message Yahoo Messenger
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12372
Topics: 75
Location: San Jose

PostPosted: Thu Sep 11, 2003 8:59 am    Post subject: Reply with quote

Rasprasad,

As you already guessed the culprit is the INTERMEDIATE result of (A/B).check this link for Intermediate results and arithmetic precision.

http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/igy3pg10/APPENDIX1.1?DT=20020923143836#HDRWQ955

Now with rounding,after decimal point alignment, the number of places in the fraction of the result of an arithmetic operation is compared with the number of places provided for the fraction of the resultant identifier.

When the size of the fractional result exceeds the number of places provided for its storage, truncation occurs unless ROUNDED is specified. When ROUNDED is specified, the least significant digit of the resultant identifier is increased by 1 whenever the most significant digit of the excess is greater than or equal to 5.

When the resultant identifier is described by a PICTURE clause containing rightmost Ps, and when the number of places in the calculated result exceeds the number of integer positions specified, rounding or truncation occurs, relative to the rightmost integer position for which storage is allocated.

In a floating-point arithmetic operation, the ROUNDED phrase has no effect; the result of a floating-point operation is always rounded

When the ARITH(EXTEND) compiler option is in effect, the ROUNDED phrase is not supported for arithmetic receivers with 31 digit positions to the right of the decimal point.


Solutions:

Let us take your example.

COMPUTE WS-C = ( WS-A / WS-B ) * 100.

Define the decimal part of anyone of those variables WS-A or WS-C (not the divider or exponentiation) in such a way that it is greater than (No. of decimals defined for A + No. of integers defined for B) . This works with any pic clause and the % error is very less.

Since you did not want to change the pic cluse of the variables you can try as suggested by somu.Since you are adding a zero it is NOT going to change the actual value , but nullifies the Truncation errors.

Hope this helps...

cheers

kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
rasprasads
Beginner


Joined: 10 Dec 2002
Posts: 59
Topics: 20
Location: Chennai

PostPosted: Fri Sep 12, 2003 1:27 am    Post subject: Reply with quote

I am not still clear about the rounded option. In CASE - 2 the result is 49.90.

Here 900.03/1800.33 would give 0.499925. This would be stored as 0.49 if rounded option is not sepcified. But as Rounded is specified, the no. of decimal place will be increased by 1 (to 3). So value should have been (0.500 ---> as 0.4999 would have been rounded so) and finally should be 50.00. But this is not the case and the result is 49.90. Can you explain why this is happening ?
_________________
Rasprasad S
Back to top
View user's profile Send private message
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 700
Topics: 63
Location: USA

PostPosted: Fri Sep 12, 2003 5:51 am    Post subject: Reply with quote

Try "COMPUTE WS-C = ( WS-A * 100)/ WS-B )". You might get beter result.
Back to top
View user's profile Send private message Send e-mail
rasprasads
Beginner


Joined: 10 Dec 2002
Posts: 59
Topics: 20
Location: Chennai

PostPosted: Fri Sep 12, 2003 6:12 am    Post subject: Reply with quote

I am ok with the solution(s). But want to know the reason behind the value generated when using ROUNDED option. Please help...
_________________
Rasprasad S
Back to top
View user's profile Send private message
rasprasads
Beginner


Joined: 10 Dec 2002
Posts: 59
Topics: 20
Location: Chennai

PostPosted: Fri Sep 12, 2003 8:57 am    Post subject: Reply with quote

How will this solve the issue :

Quote:

COMPUTE WS-C = ( WS-A / WS-B ) * 100 + WS-CORRECTION-FACTOR .



Can you provide me some insight into this...
_________________
Rasprasad S
Back to top
View user's profile Send private message
slade
Intermediate


Joined: 07 Feb 2003
Posts: 266
Topics: 1
Location: Edison, NJ USA

PostPosted: Sat Sep 13, 2003 12:13 am    Post subject: Reply with quote

Hi Rasp,

WS-C..-F.. forces the compiler to create an intermediate result field with more than enough decimal places to accommodate the result and, therefore, no erroneous truncation results.

The attraction of it is that it's a "one size fits all" solution, unless you're using the ARITH=EXTEND option.

Regards, Jack.
Back to top
View user's profile Send private message
neilxt
Beginner


Joined: 01 Mar 2004
Posts: 23
Topics: 1

PostPosted: Wed May 12, 2004 5:05 pm    Post subject: Reply with quote

Quote:

I again tried with the following code :
Code:

COMPUTE WS-C ROUNDED = ( WS-A / WS-B ) * 100.

There are two questions here; how do you get the best answer and what's going on with "Rounded".

It's been stated here already, but I'll repeat that your best solution is to do the multiplication first. Division is always likely to produce some rounding errors and when you multiply afterwards you are multiplying the rounding error.

As for the rounding, that only happens at the end and is the last step before moving your data to your output so the result will depend on the comparative difference between your intermediate data item and your recieving field.

From your actual results it looks as if the prescence of the ROUNDING option increased the size of the intermediate field by 1 decimal digit creating 1 extra digit of accuracy but once you'd multiplied the truncated data by 100 it had nothing to round - your last 2 digits were 00.
Back to top
View user's profile Send private message Send e-mail
js01
Beginner


Joined: 13 Oct 2005
Posts: 84
Topics: 32
Location: INDIA

PostPosted: Thu Mar 26, 2009 5:10 pm    Post subject: Rounded in cobol Reply with quote

Hello friends,

could you please help me in below question

declared

05 VAR1 S9(5)V99
05 VAR2 S9(5)V99 value 2070

Compute VAR1 ROUNDED = VAR2 * 1.4

what is the value of VAR1

thank you
Back to top
View user's profile Send private message
Terry_Heinze
Supermod


Joined: 31 May 2004
Posts: 391
Topics: 4
Location: Richfield, MN, USA

PostPosted: Thu Mar 26, 2009 10:19 pm    Post subject: Reply with quote

As suggested, please refrain from asking the same question in 2 different threads.
_________________
....Terry
Back to top
View user's profile Send private message Send e-mail
Nic Clouston
Advanced


Joined: 01 Feb 2007
Posts: 1075
Topics: 7
Location: At Home

PostPosted: Fri Mar 27, 2009 11:37 am    Post subject: Reply with quote

simple - write a little program to test it
_________________
Utility and Program control cards are NOT, repeat NOT, JCL.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group