Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref

Author Message
truly198

New User

Joined: 15 May 2010
Posts: 4
Location: CN

 Posted: Tue May 24, 2011 7:11 pm    Post subject: How to deduce the range using TRUNC option 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

Akatsukami

Global Moderator

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

 Posted: Tue May 24, 2011 7:19 pm    Post subject: Yes, that is correct.
Bill Woodger

DFSORT Moderator

Joined: 09 Mar 2011
Posts: 7314

 Posted: Tue May 24, 2011 7:21 pm    Post subject: 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.
Robert Sample

Global Moderator

Joined: 06 Jun 2008
Posts: 8339
Location: Dubuque, Iowa, USA

Posted: Tue May 24, 2011 7:31 pm    Post subject:

 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.
Bill O'Boyle

CICS Moderator

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

 Posted: Tue May 24, 2011 7:49 pm    Post subject: Reply to: how to deduce the range 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
Bill Woodger

DFSORT Moderator

Joined: 09 Mar 2011
Posts: 7314

 Posted: Tue May 24, 2011 7:53 pm    Post subject: Reply to: how to deduce the range 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.
dbzTHEdinosauer

Global Moderator

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

 Posted: Wed May 25, 2011 11:47 am    Post subject: 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.
Bill Woodger

DFSORT Moderator

Joined: 09 Mar 2011
Posts: 7314

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

 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.
Bill Woodger

DFSORT Moderator

Joined: 09 Mar 2011
Posts: 7314

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

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.
Bill Woodger

DFSORT Moderator

Joined: 09 Mar 2011
Posts: 7314

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

 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
 All times are GMT + 6 Hours
 Page 1 of 1

Search our Forum:

 Topic Author Forum Replies Posted Similar Topics VALIDATE option in BIND jcl Poha Eater DB2 2 Mon Feb 26, 2018 7:08 pm Extract Record using range of Data scorp_rahul23 DFSORT/ICETOOL 3 Wed Nov 15, 2017 11:54 pm Where is output from PARM ABEXIT (SNA... packerm CA Products 3 Thu Sep 28, 2017 4:06 pm MQ command in option 6 of ISPF! Vignesh Sid All Other Mainframe Topics 3 Tue Sep 26, 2017 6:01 pm DFSORT - Overriding default option TU... Steve Ironmonger DFSORT/ICETOOL 3 Tue Apr 04, 2017 3:54 pm

 © 2003-2017 IBM MAINFRAME Software Support Division
 Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us