Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Computing statement truncation

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
sivasaras

New User


Joined: 29 Sep 2007
Posts: 90
Location: chenna/i-

PostPosted: Tue Jan 06, 2015 9:33 pm    Post subject: Computing statement truncation
Reply with quote

Hi,

I have a compute statement in my cobol code.

A PIC X(02)

MOVE 32 TO A.

C PIC 9(2)

COMPUTE C = 16 * A

value is 512 but it is storing the value as 12 not 51.

please help me how to store the first 2 numbers.

Thanks
Back to top
View user's profile Send private message

David Robinson

Active User


Joined: 21 Dec 2011
Posts: 175
Location: UK

PostPosted: Tue Jan 06, 2015 9:54 pm    Post subject:
Reply with quote

So you just want the 51 to be stored, ignoring the 2 ???
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 7913
Location: Bellevue, IA

PostPosted: Tue Jan 06, 2015 9:56 pm    Post subject:
Reply with quote

If the PIC clause is too small to hold the result, COBOL aligns to the decimal point (whether explicit or implicit). Your C variable has an implicit decimal point after the second digit, hence the 12 will be stored and the 5 dropped. The easiest way to capture the 51 would be to define C as PIC 9(03) and then REDEFINE a variable on C that is PIC X(02).
Back to top
View user's profile Send private message
Terry Heinze

JCL Moderator


Joined: 14 Jul 2008
Posts: 1238
Location: Richfield, MN, USA

PostPosted: Tue Jan 06, 2015 10:42 pm    Post subject:
Reply with quote

It can be dangerous using a PIC X (alphanumeric) field in a calculation. I'd either redefine it as numeric, or better yet, define A as numeric. Also, read very carefully the section in the Programming Language Manual about the precision of intermediate results when using the COMPUTE statement. COBOL doesn't do what you might expect it to.
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7236

PostPosted: Tue Jan 06, 2015 11:02 pm    Post subject: Reply to: Computing statement
Reply with quote

How did you use a PIC X(2) as a source field in a COMPUTE?

Why would you expect 51 to be the answer? I'm not sure I know of any programming language which right-truncates results to fit in a field.

For an answer, you can also truncate your result by dividing by 10. Not normally something I recommend, but since you are multiplying by 16 already, why don't you multiply instead by 1.6?

Terry,

It obviously took me 20 minutes to type that amongst other things. The PIC X definition for A will cause the compiler to upchuck and refuse to even attempt the COMPUTE.

COMPUTE works exactly how it should work. The problem people have with COMPUTE is that they don't read the intermediate results section that you suggested. They tend to think of a computer as a big calculator, or something which uses numbers-of-unlimited-size-and-unlimited-decimal-places-even-though-it-can't-do-decimal-calculations-accurately-in-all-cases.
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1610
Location: NY,USA

PostPosted: Thu Jan 08, 2015 3:42 am    Post subject:
Reply with quote

Quote:
value is 512 but it is storing the value as 12 not 51


Red Alram--> Is it a bug or a requirement? if bug then simply increase the size of C otherwise use the above methods as suggested.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts -913/-911 Deadlock during UPDATE stat... NoSleep319 DB2 5 Fri Nov 18, 2016 12:37 am
No new posts COBOL DB2 - CALL statement - high CPU... TS70363 DB2 15 Sun Sep 11, 2016 6:07 am
No new posts Converting NULL column into NOT NULL ... Raghu navaikulam DB2 5 Sat Aug 06, 2016 3:45 pm
This topic is locked: you cannot edit posts or make replies. Updating value of key using rewrite s... ParminderKumar COBOL Programming 4 Thu Aug 04, 2016 12:46 am
No new posts FTP from windows server into Mainfram... sathyajes All Other Mainframe Topics 24 Wed Jul 20, 2016 1:33 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us