TRUNC
TRUNC
affects the way that binary data
is truncated during moves and arithmetic operations.
Default is: TRUNC(STD)
Abbreviations are: None
TRUNC
has no effect on COMP-5
data
items; COMP-5
items are handled as if TRUNC(BIN)
is
in effect regardless of the TRUNC
suboption specified.
TRUNC(STD)
TRUNC(STD)
applies only toUSAGE BINARY
receiving fields inMOVE
statements and arithmetic expressions. WhenTRUNC(STD)
is in effect, the final result of an arithmetic expression, or the sending field in theMOVE
statement, is truncated to the number of digits in thePICTURE
clause of theBINARY
receiving field.TRUNC(OPT)
-
TRUNC(OPT)
is a performance option. WhenTRUNC(OPT)
is in effect, the compiler assumes that data conforms toPICTURE
specifications inUSAGE BINARY
receiving fields inMOVE
statements and arithmetic expressions. The results are manipulated in the most optimal way, either truncating to the number of digits in thePICTURE
clause, or to the size of the binary field in storage (halfword, fullword, or doubleword).Tips:- Use the
TRUNC(OPT)
option only if you are sure that the data being moved into the binary areas will not have a value with larger precision than that defined by thePICTURE
clause for the binary item. Otherwise, unpredictable results could occur. This truncation is performed in the most efficient manner possible; therefore, the results are dependent on the particular code sequence generated. It is not possible to predict the truncation without seeing the code sequence generated for a particular statement.
- Use the
TRUNC(BIN)
- The
TRUNC(BIN)
option applies to all COBOL language that processesUSAGE BINARY
data. WhenTRUNC(BIN)
is in effect, all binary items (USAGE COMP
,COMP-4
, orBINARY
) are handled as native hardware binary items, that is, as if they were each individually declaredUSAGE COMP-5
:BINARY
receiving fields are truncated only at halfword, fullword, or doubleword boundaries.BINARY
sending fields are handled as halfwords, fullwords, or doublewords when the receiver is numeric;TRUNC(BIN)
has no effect when the receiver is not numeric.- The full binary content of fields is significant.
DISPLAY
will convert the entire content of binary fields with no truncation.
Recommendations:
TRUNC(BIN)
is the recommended option for programs that use binary values set by other products. Other products, such as IMS, Db2®, C/C++, FORTRAN, and PL/I, might place values in COBOL binary data items that do not conform to thePICTURE
clause of the data items. You can useTRUNC(OPT)
with CICS® programs provided that your data conforms to thePICTURE
clause for yourBINARY
data items.USAGE COMP-5
has the effect of applyingTRUNC(BIN)
behavior to individual data items. Therefore, you can avoid the performance overhead of usingTRUNC(BIN)
for every binary data item by specifyingCOMP-5
on only some of the binary data items, such as those data items that are passed to non-COBOL programs or other products and subsystems. The use ofCOMP-5
is not affected by theTRUNC
suboption in effect.Large literals in
VALUE
clauses: When you use the compiler optionTRUNC(BIN)
, numeric literals specified inVALUE
clauses for binary data items (COMP
,COMP-4
, orBINARY
) can generally contain a value of magnitude up to the capacity of the native binary representation (2, 4, or 8 bytes) rather than being limited to the value implied by the number of9
s in thePICTURE
clause.Note: WhenTRUNC(BIN)
andNUMCHECK(BIN)
are both in effect and an error message or an abend is generated, if you intend to switch toTRUNC(STD|OPT)
later for better performance, you must correct the data; if not, you can turn offNUMCHECK(BIN)
to reduce the execution time of the application and avoid an error message or an abend.
TRUNC example 1
01 BIN-VAR PIC S99 USAGE BINARY.
. . .
MOVE 123451 to BIN-VAR
The following table shows values of
the data items after the MOVE
statement.
Data item | Decimal | Hex | Display |
---|---|---|---|
Sender | 123451 | 00|01|E2|3B | 123451 |
Receiver TRUNC(STD) |
51 | 00|33 | 51 |
Receiver TRUNC(OPT) |
-7621 | E2|3B | 2J |
Receiver TRUNC(BIN) |
-7621 | E2|3B | 762J |
A halfword of storage is allocated for BIN-VAR
.
The result of this MOVE
statement if the program
is compiled with the TRUNC(STD)
option is 51; the
field is truncated to conform to the PICTURE
clause.
If
you compile the program with TRUNC(BIN)
, the result
of the MOVE
statement is -7621. The reason for the
unusual result is that nonzero high-order digits are truncated. Here,
the generated code sequence would merely move the lower halfword quantity
X'E23B' to the receiver. Because the new truncated value overflows
into the sign bit of the binary halfword, the value becomes a negative
number.
It is better not to compile this MOVE
statement
with TRUNC(OPT)
, because 123451 has greater precision
than the PICTURE
clause for BIN-VAR
.
With TRUNC(OPT)
, the results are again -7621. This
is because the best performance was obtained by not doing a decimal
truncation.
TRUNC example 2
01 BIN-VAR PIC 9(6) USAGE BINARY
. . .
MOVE 1234567891 to BIN-VAR
The following table shows values of
the data items after the MOVE
statement.
Data item | Decimal | Hex | Display |
---|---|---|---|
Sender | 1234567891 | 49|96|02|D3 | 1234567891 |
Receiver TRUNC(STD) |
567891 | 00|08|AA|53 | 567891 |
Receiver TRUNC(OPT) |
567891 | 53|AA|08|00 | 567891 |
Receiver TRUNC(BIN) |
1234567891 | 49|96|02|D3 | 1234567891 |
When you specify TRUNC(STD)
, the sending
data is truncated to six integer digits to conform to the PICTURE
clause
of the BINARY
receiver.
When you specify TRUNC(OPT)
,
the compiler assumes the sending data is not larger than the PICTURE
clause
precision of the BINARY
receiver. The most efficient
code sequence in this case is truncation as if TRUNC(STD)
were
in effect.
When you specify TRUNC(BIN)
, no
truncation occurs because all of the sending data fits into the binary
fullword allocated for BIN-VAR
.