3 sign conventions, 4 invalid values in numeric items – Compaq COBOL AAQ2G1FTK User Manual

Page 96

Advertising
background image

Handling Numeric Data
2.2 Specifying Alignment

OpenVMS VAX, or flexibility. (See Chapter 16, Managing Memory and Data
Access and Chapter 15, Optimizing Your Compaq COBOL Program in this
manual, and the SYNCHRONIZED clause in the Compaq COBOL Reference
Manual
for a complete discussion of alignment.)

2.3 Sign Conventions

Compaq 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 Compaq 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:

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

Then:

SUBTRACT B FROM A.

(A = 3)

SUBTRACT 1 FROM A.

(A = 2)

However:

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 Compaq 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 operating
system) 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) 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

2–2 Handling Numeric Data

Advertising