Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
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.
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
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.
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
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).
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
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.
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
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.
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
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.
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
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