Problem:
We have a program that is failing the IF NUMERIC test. The field in question is a Pic 9(18) but it is Redefined as a Pic S9(18) and the value at the time of the numeric test contains something like x'F0F1C1' instead of x'F0F1F1'. Is this okay? We coded the test naming the unsigned field.
Resolution:
IBM has long documented this and so has Micro Focus.
Here is the quote from IBM:
--------------------------------------------------------------------------------------
IBM Enterprise Cobol for z/OS Programming Guide V3R4
Numeric Class test
http://publibfp.boulder.ibm.com/cgi-bin/bookmgr/BOOKS/igy3pg31/1.3.7?DT=20060329003636
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.
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"
The numeric class test checks the contents of a data item against a set of
values that are valid for the 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).
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.
Table 7. NUMCLS(PRIM) and valid signs
NUMPROC(NOPFD) NUMPROC(PFD) NUMPROC(MIG)
Signed C, D, F C, D, 0 (positive C, D, F
zero)
Unsigned F F F
Separate sign , - , -, 0 (positive ,-
zero)
--------------------------------------------------------------------------------------
The key sentences from IBM are these:
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.
and
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.
So as you can see the IF NUMERIC test will fail if any other sign except for
a 'F' exists for an unsigned data item. IBM will not check data. Micro Focus will
check data when Animating. But for the numeric class test MFE and IBM are
consistent in this. One could re-code to add a MOVE statement such as in
this sample program. For the MFE reference go to the online Help and type
in 'numeric test'.
Here is a sample program to show when items will pass the IF NUMERIC test:
Identification Division.
Program-Id. LORINCE.
* This program should produce these results:
*
*Test Positive Number
* Move 55 to Both Numeric Fields
* U9-18 item is not numeric
* S9-18 item is numeric
*Test Negative Number
* Move -33 to Both Numeric Fields
* U9-18 item is not numeric
* S9-18 item is numeric
*Move Signed Number to Test Field
* Move -33 to TestField From S9-18
* TestField is numeric
* Move 55 to TestField From S9-18
* TestField is numeric
*Move Unsigned Number to Test Field
* Move 33 to TestField From U9-18
* TestField is numeric
* Move 55 to TestField From U9-18
* TestField is numeric
*
Environment Division.
Data Division.
Working-Storage Section.
1 Ws1.
2 U9-18 Pic 9(18).
2 S9-18 Redefines U9-18
Pic S9(18).
2 TestField Pic S9(18).
Procedure Division.
P1.
* Test Positive number
Display 'Test Positive Number'
Move 55 to S9-18
Display ' Move 55 to Both Numeric Fields '
If U9-18 is Numeric
Display ' U9-18 item is numeric '
Else
Display ' U9-18 item is not numeric '
End-If
If S9-18 is Numeric
Display ' S9-18 item is numeric '
Else
Display ' S9-18 item is not numeric '
End-If
* Test Negative number
Display 'Test Negative Number'
Move -33 to S9-18
Display ' Move -33 to Both Numeric Fields '
If U9-18 is Numeric
Display ' U9-18 item is numeric '
Else
Display ' U9-18 item is not numeric '
End-If
If S9-18 is Numeric
Display ' S9-18 item is numeric '
Else
Display ' S9-18 item is not numeric '
End-If
* Move Signed numbers to Test Field
Display 'Move Signed Number to Test Field'
Move -33 to S9-18
Move S9-18 to TestField
Display ' Move -33 to TestField From S9-18 '
If TestField is Numeric
Display ' TestField is numeric '
Else
Display ' TestField is not numeric '
End-If
Move 55 to S9-18
Move S9-18 to TestField
Display ' Move 55 to TestField From S9-18 '
If TestField is Numeric
Display ' TestField is numeric '
Else
Display ' TestField is not numeric '
End-If
* Move Unsigned numbers to Test Field
Display 'Move Unsigned Number to Test Field'
Move 33 to U9-18
Move S9-18 to TestField
Display ' Move 33 to TestField From U9-18 '
If TestField is Numeric
Display ' TestField is numeric '
Else
Display ' TestField is not numeric '
End-If
Move 55 to U9-18
Move S9-18 to TestField
Display ' Move 55 to TestField From U9-18 '
If TestField is Numeric
Display ' TestField is numeric '
Else
Display ' TestField is not numeric '
End-If
Stop Run.
The program above illustrates that the data must match its
PICTURE definition. So the best thing to do is REDEFINE
an item using the same definitions (signed or unsigned,
numeric or alphanumeric, COMP, COMP-3) and do not
mix PICTURE definitions (numeric COMP redefined as
numeric COMP-3 or numeric redefined as alphanumeric).
#EnterpriseDeveloper
#MFDS




