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
 

 

How to deduce the range using TRUNC option

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

New User


Joined: 15 May 2010
Posts: 4
Location: CN

PostPosted: Tue May 24, 2011 7:11 pm    Post subject: How to deduce the range using TRUNC option
Reply with quote

as captioned, Ie. S9(9) COMP.

Is this correct if i deduce this way -

it takes up 4 bytes. so 4 * 8 =32 bits

so the maxmum is 2 32 - 1 = 4294967295

or the sign takes one bit 2 31 - 1 = 2147483647
Back to top
View user's profile Send private message

Akatsukami

Global Moderator


Joined: 03 Oct 2009
Posts: 1738
Location: Bloomington, IL

PostPosted: Tue May 24, 2011 7:19 pm    Post subject:
Reply with quote

Yes, that is correct.
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7236

PostPosted: Tue May 24, 2011 7:21 pm    Post subject:
Reply with quote

Yes, four bytes. A Word, or Full-Word, of storage.

The sign is the high-order bit. Everything above 7FFFFFFF is -ve. I'm not going to check your maths, it looks about right.

So, with Cobol, the point is that for the size of a binary field, the compiler will go to the smallest out of half-/full-/double-word. If you go for S9(10) you will get a double-word of storage allocated. If you go for S9(4) a half-world, S9(5) a full-word, like your example.

S9 to S9(4), half-word
S9(5) to S9(9), full-word
S9(10)+, double-word

Turns out, S9(9) is about the worst possible thing to use for binary calculations.
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 May 24, 2011 7:31 pm    Post subject:
Reply with quote

Quote:
as captioned, Ie. S9(9) COMP.

Is this correct if i deduce this way -

it takes up 4 bytes. so 4 * 8 =32 bits

so the maxmum is 2 32 - 1 = 4294967295

or the sign takes one bit 2 31 - 1 = 2147483647
Maybe. COBOL has the TRUNC option that can be set differently at each site. Depending upon the TRUNC option, a PIC S9(9) COMP variable may have the range -2,147,483,648 to 2,147,483,647 OR -999,999,999 to 999,999,999.

Without looking at the compile options, you cannot tell which it is -- and since the TRUNC option can be changed for each compile, you cannot state an absolute rule for the variable range -- it will depend upon the TRUNC setting when the compile is done.
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2502
Location: Atlanta, Georgia, USA

PostPosted: Tue May 24, 2011 7:49 pm    Post subject: Reply to: how to deduce the range
Reply with quote

If your COBOL compiler supports COMP-5 (OS/390 COBOL 2.2.1 and greater), then the TRUNC option doesn't matter as COMP-5 (Native Binary) ignores the TRUNC option.

However, as Robert has said, the TRUNC option can have a surprising effect on the maximum-range in binary variables.

The TRUNC option to use for maximum-range (for non COMP-5), is TRUNC(BIN).

Bill
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7236

PostPosted: Tue May 24, 2011 7:53 pm    Post subject: Reply to: how to deduce the range
Reply with quote

Good point Robert. Effectively TRUNC is telling the compiler where to use the space allocated to determine the maximum value (your example) or the PICture to determine the maximum value, +/-999,999,999. Check out COMP-5 as well in the manual.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Wed May 25, 2011 11:47 am    Post subject:
Reply with quote

keep in mind, (cobol 2, trunc(bin))

when you move a literal 1,000,000,000 to a comp field,
the comp field must be defined as s9(10) or 9(10), which is a double word.

you can then move the doubleword to a single word comp field, [s9(9) or 9(9) comp]
and the trunc(bin) option will perserve the integrity of the value.

in other words, your literal and the receiving field have to match.
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7236

PostPosted: Wed May 25, 2011 12:33 pm    Post subject: Re: Reply to: how to deduce the range
Reply with quote

Bill Woodger wrote:
Good point Robert. Effectively TRUNC is telling the compiler where to use the space allocated to determine the maximum value (your example) or the PICture to determine the maximum value, +/-999,999,999. Check out COMP-5 as well in the manual.


Silly fingers.

Good point Robert. Effectively TRUNC is telling the compiler whether to use the space allocated to determine the maximum value (your example) or the PICture to determine the maximum value, +/-999,999,999. Check out COMP-5 as well in the manual.

In addition, it doesn't happen by magic that you get different maximum values. If you use the compile option to set the PICture as definining the maximum value, then the compiler will generate code, at each point at which the value of the field can change, to ensure that the value is not become greater than the PICture size (ie high-order tuncation).

Along with Dick's point about literal values (and try geting 1,000,000,000 on a VALUE clause with either compile option) also watch out for "edited" fileds for printing/display. Although you might have S9(9) as your PICture, you would need Z(9)9 (or equivalent, 10 printable digits) to ensure displaying everything with NOTRUNC.

While we are at it, again, the manual suggests avoiding binary fields with a PICture of 9 digits in situations where performance is important. For calculations, this is first converted to a double-word, then uses double-word artihmetic, then converted back to a full-word. A PICture with 10 digits is already a double-word, so avoids the two conversions. A PICture with 8 digits will use full-word arithmetic.
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7236

PostPosted: Wed May 25, 2011 4:13 pm    Post subject: Reply to: how to deduce the range
Reply with quote

Off-topic, but, just for fun, can you tell the range at run time?

Code:

move 1 to value

loop until
  ( ( value is zero  )
 Or
  ( value is negative ) )
 keep previous value
 add 1 to value
end-loop

previous value is positive limit

if value is negative
    value is negative limit
else
    if previous value / 2 is exact (and gets here)
        value is positive only
    else
         test value = 0 - previous value
         if previous value = test value
             value is positive only (with all 9's)
         else
             test value is negative limit
         end-if
    end-of
end-if


If it works and you don't know why, someone can explain.
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7236

PostPosted: Thu May 26, 2011 9:15 pm    Post subject: Re: Reply to: how to deduce the range
Reply with quote

Bill Woodger wrote:
Off-topic, but, just for fun, can you tell the range at run time?
[...]


An error, a why-do-it-like-that and a typo.

Code:

"value" is a COMP field of any size and any picture.

The idea is, using only value, to establish the positive/negative limits at run-time.

move 1 to value

loop until
      ( ( value is zero )
    Or
      ( value is negative ) )
     add 1 to value
end-loop

subtract 1 from value
display value as positive limit
add 1 to value

if value is negative
    display value as negative limit
else
    value = ( 0 - ( value - 1 ) )
    if value is positive
    * value is positive only
    else
        display value as negative limit
    end-if
end-if
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 BWO option in VSAM blayek CICS 3 Sat Nov 05, 2016 10:47 am
No new posts Regarding COBOL Stored Procedure opti... selvamsrinivasan85 DB2 4 Fri Nov 04, 2016 8:57 pm
No new posts Compiler option for override the exte... muralikrishnan_new COBOL Programming 9 Fri Jul 08, 2016 12:09 pm
No new posts ICETOOL higher option Anshul Das DFSORT/ICETOOL 14 Thu Apr 14, 2016 8:00 am
No new posts CEXEC not able to execute if no SLINE... boyti ko CLIST & REXX 2 Wed Apr 13, 2016 4:31 pm


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