[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP COBOL
User Manual


Previous Contents Index

2.7.2 Standard and Native Arithmetic (Alpha, I64)

HP 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 HP COBOL or for compatibility with HP 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 (or -arithmetic ) and /MATH_INTERMEDIATE (or -math_intermediate ). The use of these qualifiers is described in this section.

2.7.2.1 Using the /MATH_INTERMEDIATE Qualifier (Alpha, I64)

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 HP COBOL, but not with HP COBOL for OpenVMS VAX. FLOAT has been used since Version 1.0 of HP COBOL on Alpha.
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 HP 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 (Alpha, I64)

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 (Alpha, I64)

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 (Alpha, I64)

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 HP COBOL for OpenVMS Alpha prior to Version 2.7 and also with HP 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 (Alpha, I64)

An alternative way to specify native or standard arithmetic is to use the OPTIONS paragraph in the Identification Division of your HP COBOL program. There you can specify ARITHMETIC IS NATIVE or STANDARD. Refer to the HP 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) specifies how the HP COBOL compiler stores values in COMPUTATIONAL receiving items.

By default (assuming that the -trunc flag is turned off, or /NOTRUNCATE is set), HP COBOL truncates values according to the Alpha or I64 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.


Previous Next Contents Index