Checking for incompatible data (numeric class test)
The compiler assumes that values you supply for a data
item are valid for the PICTURE
and USAGE
clauses,
and does not check their validity. Ensure that the contents of a data
item conform to the PICTURE
and USAGE
clauses
before using the item in additional processing.
About this task
It
can happen that values are passed into your program and assigned to
items that have incompatible data descriptions for those values. For
example, nonnumeric data might be moved or passed into a field that
is defined as numeric, or a signed number might be passed into a field
that is defined as unsigned. In either case, the receiving fields
contain invalid data. When you give an item a value that is incompatible
with its data description, references to that item in the PROCEDURE
DIVISION
are undefined and your results are unpredictable.
You can use the numeric class test to perform data validation. For example:
Linkage Section.
01 Count-x Pic 999.
. . .
Procedure Division Using Count-x.
If Count-x is numeric then display "Data is good"
PICTURE
and USAGE
of the data item. For example, a packed decimal item is checked for hexadecimal values X'0' through
X'9' in the digit positions and for a valid sign value in the sign position (whether separate or
nonseparate). An external decimal data item that has
USAGE DISPLAY
is checked for hexadecimal values X'0' through X'9' in the digit
positions (the lower 4 bits of each byte), for a valid zone code in the upper 4 bits of each byte
and for a valid sign value in the sign position (whether separate or nonseparate). The sign code is
in the upper 4 bits of the sign byte or in a separate byte if SIGN IS SEPARATE
was
specified. If the SIGN IS SEPARATE
clause is used, the upper four bits of all bytes
must be x'F'. INVDATA
compiler option allows toleration of invalid zone codes in
USAGE DISPLAY
numeric (zoned decimal) data items in numeric comparisons, invalid
zone codes in zoned decimal data items will be treated as nonnumeric by the numeric class test.
For zoned decimal and packed decimal items, the numeric
class test is affected by the NUMPROC
compiler option
and the NUMCLS
option (which is set at installation
time). To determine the NUMCLS
setting used at your
installation, consult your system programmer.
If NUMCLS(PRIM)
is
in effect at your installation, use the following table to find the
values that the compiler considers valid for the sign.
NUMPROC(NOPFD) |
NUMPROC(PFD) |
|
---|---|---|
Signed | C, D, F | C, D, +0 (positive zero) |
Unsigned | F | F |
Separate sign | +, - | +, -, +0 (positive zero) |
If NUMCLS(ALT)
is in
effect at your installation, use the following table to find the values
that the compiler considers valid for the sign.
NUMPROC(NOPFD) |
NUMPROC(PFD) |
|
---|---|---|
Signed | A to F | C, D, +0 (positive zero) |
Unsigned | F | F |
Separate sign | +, - | +, -, +0 (positive zero) |
For more information about numeric class condition, see Class condition in the Enterprise COBOL for z/OS® Language Reference.
You can also use the NUMCHECK(ZON,PAC)
option
to have the compiler generate implicit numeric class tests for zoned
decimal (numeric USAGE DISPLAY
) and packed decimal
(COMP-3
) data items that are used as sending data
items. This numeric class test validates data and also validates sign
fields against the NUMPROC
compiler option to help
you decide whether you can use NUMPROC(PFD)
or not.
For details, see NUMCHECK.
For more information about other invalid data issues, in particular for COBOL migration purposes, see the FAQ about invalid data in the Enterprise COBOL Migration Guide.