HP OpenVMS Systems Documentation

Content starts here

User Manual

Previous Contents Index

Chapter 2
Handling Numeric Data

Numeric data in HP COBOL is evaluated with respect to the algebraic value of the operands.

This chapter describes the following topics concerning numeric data handling:

2.1 How the Compiler Stores Numeric Data

Understanding how data is stored will help you in the following situations:

  • When you define data items to participate in group moves or to be the subject of a REDEFINES clause
  • When you move a complex record consisting of several levels of subordination, to be sure that the receiving item is large enough to prevent data truncation
  • When you need to use data storage concepts to minimize storage space, particularly when the data file is large

The storage considerations applicable to tables are described in Chapter 4.

For each numeric data item, HP COBOL stores the numeric value, and a sign (if an S appears in the PICTURE clause).

The USAGE clause of a numeric data item specifies the data's internal format in storage. When you do not specify a USAGE clause, the default usage is DISPLAY. For further information about internal representations, refer to the USAGE clause tables in the HP COBOL Reference Manual.

2.2 Specifying Alignment

In HP COBOL, all records, and elementary items with level 01 or 77, begin at an address that is a multiple of 8 bytes (a quadword boundary). By default, the HP COBOL compiler will locate a subordinate data item at the next unassigned byte location. However, the SYNCHRONIZED clause, the -align flag (on Tru64 UNIX), the /ALIGNMENT qualifier (on OpenVMS Alpha and I64), and alignment directives can be used to modify this behavior, causing some numeric data items to be aligned on a 2-, 4-, or 8-byte boundary. You can thus tune data alignment for optimum performance, compatibility with HP COBOL for OpenVMS VAX, or flexibility. (See Chapter 16, Managing Memory and Data Access and Chapter 15, Optimizing Your HP COBOL Program in this manual, and refer to the SYNCHRONIZED clause in the HP COBOL Reference Manual for a complete discussion of alignment.)

2.3 Sign Conventions

HP COBOL numeric items can be signed or unsigned. Note the following sign conventions:

  • If you store a signed result in an unsigned item, only the absolute value is stored. Thus, unsigned items only contain the value zero or a positive value.
  • The way HP COBOL stores signed results in signed data items depends on the usage and the presence or absence of the SIGN clause.
  • When an unsigned result is stored in a signed data item, the sign of the stored result is positive.

Do not use unsigned numeric items in arithmetic operations. They usually cause programming errors and are handled less efficiently than signed numeric items. The following example shows how unsigned numeric items can cause errors:

01 A PIC 9(5) COMP VALUE 2.
01 B PIC 9(5) COMP VALUE 5.


SUBTRACT B FROM A.       (A = 3)

SUBTRACT 1 FROM A.       (A = 2)


COMPUTE A = (A - B) - 1    (A = 4)

The absence of signs for the numeric items A and B results in two different answers after parallel arithmetic operations have been done. This occurs because internal temporaries (required by the COMPUTE statement) are signed. Thus, the result of (A--B) within the COMPUTE statement is --3; --3 minus 1 is --4 and the value of A then becomes 4.

2.4 Invalid Values in Numeric Items

All HP COBOL arithmetic operations store valid values in their result items. However, it is possible, through group moves or REDEFINES, to store data in numeric items that do not conform to the data definitions of those items.

The results of arithmetic operations that use invalid data in numeric items are undefined. You can use the -check decimal flag (on the Tru64 UNIX operating system) or the /CHECK=DECIMAL qualifier (on the OpenVMS Alpha or I64 operating systems) to validate numeric digits when using display numeric items in a numeric context; note that this flag or qualifier causes a program to terminate abnormally if there is invalid data. In the case of data with blanks (typically, records in a file), you can use the -convert leading_blanks flag (on Tru64 UNIX) or the /CONVERT qualifier (on OpenVMS Alpha or I64) to change all blanks to zeroes before performing the arithmetic operation. If you specify both the -check decimal and the -convert leading_blanks flags (on Tru64 UNIX), or both the /CHECK=DECIMAL and the /CONVERT qualifiers on OpenVMS Alpha or I64, the conversion of blanks will be done prior to the validation of the resulting numeric digits. Note that the use of either or both of these qualifiers increases the execution time of the program. Refer to HP COBOL online help (at the OpenVMS system prompt), or man cobol (on Tru64 UNIX) for more information.

2.5 Evaluating Numeric Items

HP COBOL provides several kinds of conditional expressions used for evaluating numeric items. These conditional expressions include the following:

  • The numeric relation condition that compares the item's contents to another numeric value
  • The sign condition that examines the item's sign to see if it is positive or negative
  • The class condition that inspects the item's digit positions for valid numeric characters
  • The success/failure condition that checks the return status codes of COBOL and non-COBOL procedures for success or failure conditions

The following sections explain these conditional expressions in detail.

2.5.1 Numeric Relation Test

A numeric relation test compares two numeric quantities and determines if the specified relation between them is true. For example, the following statement compares item FIELD1 to item FIELD2 and determines if the numeric value of FIELD1 is greater than the numeric value of FIELD2:


If the relation condition is true, the program control takes the true path of the statement.

Table 2-1 describes the relational operators.

Table 2-1 Numeric Relational Operator Descriptions
Operator Description
IS [NOT] >
The first operand is greater than (or not greater than) the second operand.
IS [NOT] <
The first operand is less than (or not less than) the second operand.
IS [NOT] =
The first operand is equal to (or not equal to) the second operand.
IS >=
The first operand is greater than or equal to the second operand.
IS <=
The first operand is less than or equal to the second operand.

Comparison of two numeric operands is valid regardless of their USAGE clauses.

The length of the literal or arithmetic expression operands (in terms of the number of digits represented) is not significant. Zero is a unique value, regardless of the sign.

Unsigned numeric operands are assumed to be positive for comparison. The results of relation tests involving invalid (nonnumeric) data in a numeric item are undefined.

Previous Next Contents Index