Compaq COBOL
User Manual


Previous Contents Index

2.6 Using the MOVE Statement

The MOVE statement moves the contents of one item into another item. The following sample MOVE statement moves the contents of item FIELD1 into item FIELD2:


MOVE FIELD1 TO FIELD2. 

This section considers MOVE statements as applied to numeric and numeric edited data items.

2.6.1 Elementary Numeric Moves

If both items of a MOVE statement are elementary items and the receiving item is numeric, it is an elementary numeric move. The sending item can be numeric, alphanumeric, or numeric-edited. The elementary numeric move converts the data format of the sending item to the data format of the receiving item.

An alphanumeric sending item can be either of the following:

The elementary numeric move accepts the figurative constant ZERO and considers it to be equivalent to the numeric literal 0. It treats alphanumeric sending items as unsigned integers of DISPLAY usage.

When the sending item is numeric-edited, de-editing is applied to establish the unedited numeric value, which may be signed; then the unedited numeric value is moved to the receiving field.

If necessary, the numeric move operation converts the sending item to the data format of the receiving item and aligns the sending item's decimal point on that of the receiving item. Then it moves the sending item's digits to the corresponding receiving item's digits.

If the sending item has more digit positions than the receiving item, the decimal point alignment operation truncates the value of the sending item, with resulting loss of digits.

The end truncated (high-order or low-order) depends upon the number of sending item digit positions that find matches on each side of the receiving item's decimal point. If the receiving item has fewer digit positions on both sides of the decimal point, the operation truncates both ends of the sending item. Thus, if an item described as PIC 999V999 is moved to an item described as PIC 99V99, it loses one digit from the left end and one from the right end.

In the execution part of the following examples, the caret (^) indicates the assumed stored decimal point position:


01 AMOUNT1 PIC 99V99 VALUE ZEROS. 
   . 
   . 
   . 
   MOVE 123.321 TO AMOUNT1. 
 
Before execution:  00^00 
After execution:   23^32 

If the sending item has fewer digit positions than the receiving item, the move operation supplies zeros for all unfilled digit positions.


01  TOTAL-AMT PIC 999V99 VALUE ZEROS. 
    . 
    . 
    . 
    MOVE 1 TO TOTAL-AMT. 
 
Before execution:   000^00 
 
After execution:    001^00 

The following statements produce the same results:


MOVE 001.00 TO TOTAL-AMT. 
 
MOVE "1" TO TOTAL-AMT. 

Consider the following two MOVE statements and their truncating and zero-filling effects:


     Statement                 TOTAL-AMT After Execution 
 
MOVE 00100 TO TOTAL-AMT                  100^00 
MOVE "00100" TO TOTAL-AMT                100^00 

Literals with leading or trailing zeros have no advantage in space or execution speed in Compaq COBOL, and the zeros are often lost by decimal point alignment.

The MOVE statement's receiving item dictates how the sign will be moved. When the receiving item is a signed numeric item, the sign from the sending item is placed in it. If the sending item is unsigned, and the receiving item is signed, a positive sign is placed in the receiving item. If the sending item is signed and the receiving item is unsigned, the absolute value of the sending item is moved to the receiving item.

2.6.2 Elementary Numeric-Edited Moves

An elementary numeric move to a numeric-edited receiving item is considered an elementary numeric-edited move. The sending item of an elementary numeric-edited move can be numeric, numeric-edited, or alphanumeric. When the sending item is numeric-edited, de-editing is applied to establish the item's unedited numeric value, which may be signed; then the unedited numeric value is moved to the receiving field. Alphanumeric sending items in numeric-edited moves are considered unsigned DISPLAY usage integers.

A numeric-edited item PICTURE can contain 9, V, and P, but to qualify as numeric-edited, it must also contain one or more of the following editing symbols:

Z
B
Asterisk (*)
Period (.)
Plus sign (+)
Minus sign (--)
CR
DB
Currency symbol
Slash (/)
Comma (,)
Zero (0)

For a complete description of these symbols see the Compaq COBOL Reference Manual.

The numeric-edited move operation first converts the sending item to DISPLAY usage and aligns both items on their decimal point locations. The sending item is truncated or zero-filled until it has the same number of digit positions on both sides of the decimal point as the receiving item. The operation then moves the sending item to the receiving item, following the Compaq COBOL editing rules.

The rules allow the numeric-edited move operation to perform any of the following editing functions:

Table 2-3 illustrates several of these functions, which are invoked by the statement:


MOVE FLD-B TO TOTAL-AMT. 

Assume that FLD-B is described as S9999V99. Note that the caret (^) indicates an assumed decimal point in Table 2-3. In all but two of the examples, the sign of FLD-B is leading separate. Trailing overpunch signs (the sign of the number encoded into the rightmost digit) are used in the other two FLD-B data examples.

Table 2-3 Numeric Editing
FLD-B TOTAL-AMT
  PICTURE String Contents After MOVE
+0023^00 ZZZZ.99 23.00
-0023^00 ZZZZ.99 23.00
0085^9P ++++.99 -85.97
+1234^00 Z,ZZZ.99 1,234.00
+0012^34 $,$$$.99 $12.34
+0000^34 $,$$9.99 $0.34
+1234^00 $$,$$$.99 $1,234.00
+0012^34 $$9,999.99 $0,012.34
+0012^34 $$$$,$$$.99 $12.34
+0000^00 $$$,$$$.$$  
0012^3M ++++.99 -12.34
+0012^34 $***,***.99 $*****12.34
+1234^56 Z,ZZZ.99+ 1,234.56+
-6543^21 $,$$$,$$$.99DB $6,543.21DB 1


1The output includes DB if a negative value is moved.

The currency symbol ($ or other currency sign) and the editing sign control symbols (+ and --) are the only floating symbols. To float a symbol, enter a string of two or more occurrences of that symbol, one for each character position over which you want the symbol to float.

2.6.3 Subscripted Moves

Any item (other than a data item that is not subordinate to an OCCURS clause) of a MOVE statement can be subscripted, and the referenced item can be used to subscript another name in the same statement.

For additional information, see Section 3.6.4, Subscripted Moves in Chapter 3, Handling Nonnumeric Data.

2.6.4 Common Move Errors

Programmers most commonly make the following errors when writing MOVE statements:

2.7 Using the Arithmetic Statements

The Compaq COBOL arithmetic statements allow programs to perform arithmetic operations on numeric data. Large values present various problems, and COBOL command qualifiers can help resolve or mitigate them. The following sections discuss these topics.

2.7.1 Temporary Work Items

Compaq COBOL allows numeric items and literals with up to 31 decimal digits (see Appendix A for specific limits). Hence it is quite easy to construct arithmetic expressions that produce more than 31 digits.

Most forms of the arithmetic statements perform their operations in temporary work locations, then move the results to the receiving items, aligning the decimal points and truncating or zero-filling the resultant values. The actual size of a temporary work item varies for each statement; it is determined at compile time, based on the sizes of the operands used by the statement and the arithmetic operation being performed. Should the temporary work item exceed the maximum size, truncation occurs. The maximum temporary work item size is 31 digits for standard arithmetic and for native CIT4 arithmetic, and is 38 digits for some operations using native float or native CIT3.

Programs should not arbitrarily specify sizes significantly larger than the values actually anticipated for the lifetime of the application. Although the generous limits in Compaq COBOL are useful for many applications, specifying 31 digits when 12 would suffice, for example, is likely to add extra processing cycles and complexity that is wasteful.

2.7.2 Standard and Native Arithmetic

Compaq COBOL supports two modes of arithmetic, standard and native. Standard arithmetic is preferable for greater precision with large values and for compatibility with other standard implementations of COBOL. These considerations are sometimes overridden by the need for compatibility with earlier versions of Compaq COBOL or for compatibility with Compaq COBOL for OpenVMS VAX, in which case native arithmetic is the appropriate mode.

Native arithmetic has three submodes: FLOAT, CIT3, and CIT4. (CIT stands for COBOL Intermediate Temporary).

You can specify the arithmetic mode and submode with the two COBOL command-line qualifiers /ARITHMETIC and /MATH_INTERMEDIATE. The use of these qualifiers is described in this section.

2.7.2.1 Using the /MATH_INTERMEDIATE Qualifier

You can specify the intermediate data type to be used when the result of an arithmetic operation cannot be represented exactly. This data type affects the truncation of the intermediate result and the consequent precision. It also affects compatibility of arithmetic results with previous versions of COBOL and other implementations of COBOL.

The three options of the /MATH_INTERMEDIATE (or -math_intermediate ) qualifier are FLOAT (the default), CIT3, and CIT4, as follows:
FLOAT Selects double-precision binary floating-point for the intermediate data type. Intermediate values are truncated to the most significant 53 bits, with an 11-bit exponent, resulting in approximately 15 decimal digits of precision. FLOAT is the default, and it provides for compatibility with earlier versions of Compaq COBOL, but not with Compaq COBOL for OpenVMS VAX. FLOAT has been used since Version 1.0 of Compaq COBOL.
CIT3 Selects Cobol Intermediate Temporary (design 3) for the intermediate data type. Intermediate values are truncated to the most significant 18 decimal digits, with a 2-digit exponent. CIT3 provides for increased compatibility with Compaq COBOL for OpenVMS VAX; even with CIT3, however, there are still some differences, which are described in Section B.4.12.
CIT4 Selects Cobol Intermediate Temporary (design 4) for the intermediate data type. Intermediate values are truncated to the most significant 32 decimal digits, with a 2-digit exponent. CIT4 has the greatest compatibility with the draft ANSI Standard. CIT4 is the option of choice for greatest precision and for conformance to future standards and compatibility with other implementations of COBOL. CIT4 is strongly recommended for programs that use numeric items with more than 18 digits or that have complicated expressions.

In addition to the precision difference, CIT4 arithmetic has the same differences and restrictions as shown in Section B.4.12 for CIT3 arithmetic.

The default is /MATH_INTERMEDIATE=FLOAT (or -math_intermediate float ). If you specify /ARITHMETIC=STANDARD (discussed in Section 2.7.2.2), this will force /MATH_INTERMEDIATE=CIT4.

Example of Different Arithmetic Results

The following example illustrates the different results that you can get with FLOAT, CIT3, and CIT4:


IDENTIFICATION DIVISION. 
PROGRAM-ID. MUL31. 
DATA DIVISION. 
WORKING-STORAGE SECTION. 
01  XD PIC S9(31) VALUE 3. 
01  YD PIC S9(31) VALUE 258718314234781388692555698765. 
01  ZD PIC S9(31). 
PROCEDURE DIVISION. 
0. 
    MULTIPLY XD BY YD GIVING ZD 
        ON SIZE ERROR DISPLAY "Size error raised" 
        NOT ON SIZE ERROR DISPLAY ZD WITH CONVERSION. 

The compiler relies on the number of digits implied by the pictures of decimal and integer operands. Here it assumes that XD has 31 digits and YD has 31 digits. The product could require 62 digits, which is larger than the largest fixed-point arithmetic type available to the compiler. Depending on the intermediate data type chosen, this program gets several different results.


                                                   Intermediate maintains 
            MATH   ZD                              the most significant 
            -----  ------------------------------  ---------------------- 
            FLOAT  776154942704344283789821739008  53 bits 
            CIT3   776154942704344164000000000000  18 digits 
            CIT4   776154942704344166077667096295  32 digits 

Other Consequences of Intermediate Range Differences

Because each intermediate data type has a different maximum magnitude, an arithmetic statement can raise the size error condition with one arithmetic mode but not with another.

For example, the value +0.999 999 999 999 999 999E+99 (spaces added for readability) is representable in any of the intermediate data types. By contrast, the larger value +0.999 999 999 999 999 999 9E+99 cannot be represented in a CIT3 intermediate data item. Such an operation would cause an overflow, raising the size error condition. This value is representable, however, in a FLOAT or CIT4 intermediate data item; the size error condition would not be raised.

The value 1.0E+99 cannot be represented in either CIT3 or CIT4 form, but is representable in FLOAT form.

Similarly, because each intermediate data type has a different minimum magnitude, an arithmetic statement can raise the size error condition for underflow with one arithmetic mode but not another. (Underflow does not raise the size error condition when FLOAT arithmetic is used.)

A literal also can be valid with one arithmetic mode but not with another, resulting in different HIGHTRUNC and LOWTRUNC informational diagnostics. When a literal cannot be represented in an intermediate data item, the value used is undefined.

Arithmetic expressions in nonarithmetic statements are also affected. Nonarithmetic statements, such as the IF statement, allow arithmetic expressions to be used, but do not provide a mechanism like the ON SIZE ERROR phrase to detect errors in evaluation. If such an error occurs, the behavior of the statement is unpredictable; in the case of an IF statement, result of the comparison is undefined.

Similar considerations apply in other contexts, such as the use of arithmetic expressions as subscript expressions or reference-modification components.

2.7.2.2 Using the /ARITHMETIC Qualifier

You can specify /ARITHMETIC=NATIVE or STANDARD ( -arithmetic native or standard ) on the COBOL command line to control whether native arithmetic or standard arithmetic is used to evaluate arithmetic operations and statements. These options have the following effects:
NATIVE Arithmetic operations will produce results that are reasonably compatible with releases for Compaq COBOL for OpenVMS Alpha prior to Version 2.7 and also with Compaq COBOL for OpenVMS VAX.
STANDARD Most common arithmetic operations will produce results that are predictable, reasonable, and portable. In this context, portable means that the results will be identical from implementation to implementation. /ARITHMETIC=STANDARD forces /MATH_INTERMEDIATE=CIT4 (described in Section 2.7.2.1).

The default is /ARITHMETIC=NATIVE ( -arithmetic native ).

Using the OPTIONS Paragraph

An alternative way to specify native or standard arithmetic is to use the OPTIONS paragraph in the Identification Division of your Compaq COBOL program. There you can specify ARITHMETIC IS NATIVE or STANDARD. See the Compaq COBOL Reference Manual for the syntax and details.

2.7.3 Specifying a Truncation Qualifier

The -trunc flag (on Tru64 UNIX) or the /[NO]TRUNCATE qualifier (on OpenVMS Alpha) specifies how the Compaq COBOL compiler stores values in COMPUTATIONAL receiving items.

By default (assuming that the -trunc flag is turned off, or /NOTRUNCATE is set), Compaq COBOL truncates values according to the Alpha hardware storage unit (word, longword, or quadword) allocated to the receiving item.

If you specify -trunc or /TRUNCATE the compiler truncates values according to the number of decimal digits specified by the PICTURE clause.

2.7.4 Using the ROUNDED Phrase

Rounding is an important option that you can use with arithmetic operations.

You can use the ROUNDED phrase with any Compaq COBOL arithmetic statement. Rounding takes place only when the ROUNDED phrase requests it, and then only if the intermediate result has low-order digits that cannot be stored in the result.

Compaq COBOL rounds off by adding a 5 to the leftmost truncated digit of the absolute value of the intermediate result before it stores that result.

Table 2-4 shows several ROUNDING examples.

Table 2-4 ROUNDING
PICTURE clause   Initial Value
03 ITEMA PIC S9(5)V9999.   12345.2222
03 ITEMB PIC S9(5)V99.   54321.11
03 ITEMC PIC S9999.   1234
03 ITEMD PIC S9999P.   0
03 ITEME PIC S99V99 VALUE 9.   9.00
03 ITEMF PIC S99V99 VALUE 24.   24.00
Arithmetic Statement Intermediate
Result
ROUNDED
Result Value
ADD ITEMA TO ITEMB ROUNDED. 066666.3322 66666.33
MULTIPLY ITEMC BY 2
GIVING ITEMD ROUNDED.
02468 02470 1
DIVIDE ITEME INTO ITEMF
ROUNDED.
02.666 02.67
DIVIDE ITEME INTO ITEMF
GIVING ITEMC ROUNDED.
02.666 0003


1The trailing 0 is implied by the P in the PICTURE clause.


Previous Next Contents Index