IBM - VisualAge Cobol for Windows Programming Guide
634 pág.

IBM - VisualAge Cobol for Windows Programming Guide


DisciplinaCobol25 materiais168 seguidores
Pré-visualização50 páginas
Displayable numbers coded in a floating-point format are called external
floating-point items. As with external decimal items, you define external
floating-point items explicitly by coding USAGE DISPLAY or implicitly by omitting
the USAGE clause. You cannot use the VALUE clause for external floating-point items.
In the following example, Compute-Result is implicitly defined as an external
floating-point item. Each byte of storage contains one of the characters (except for
the v).
05 Compute-Result Pic -9v9(9)E-99.
The minus signs (-) do not mean that the mantissa and exponent must necessarily
be negative numbers. Instead, they mean that when the number is displayed, the
sign appears as a blank for positive numbers or a minus sign for negative
numbers. If you instead code a plus sign (+), the sign appears as a plus sign for
positive numbers or a minus sign for negative numbers.
As with external decimal numbers, external floating-point numbers have to be
converted (by the compiler) to an internal representation of their numeric value
before they can be used in arithmetic operations.
34 Programming Guide
Binary (COMP) items
BINARY, COMP, and COMP-4 are synonyms on all platforms.
Binary format numbers occupy 2, 4, or 8 bytes of storage. Except for byte-reversed
binary data (where the sign bit is the leftmost bit of the rightmost byte), this
format is fixed point with the leftmost bit as the operational sign.
A binary number with a PICTURE description of four or fewer decimal digits
occupies 2 bytes; five to nine decimal digits, 4 bytes; and 10 to 18 decimal digits, 8
bytes. Binary items with nine or more digits require more handling by the
compiler.
You can use binary items, for example, for indexes, subscripts, switches, and
arithmetic operands or results.
Use the TRUNC(STD|OPT|BIN) compiler option to indicate how binary data (BINARY,
COMP, or COMP-4) is to be truncated.
Native binary (COMP-5) items
COMP-5 is a USAGE type based on the X/OPEN COBOL specification.
Data items that you declare as USAGE COMP-5 are represented in storage as binary
data. They can contain values of magnitude up to the capacity of the native
binary representation (2, 4, or 8 bytes) rather than being limited to the value
implied by the number of 9s in the PICTURE clause. When you move or store
numeric data into a COMP-5 item, truncation occurs at the binary field size rather
than at the COBOL PICTURE size limit. When you reference a COMP-5 item, the full
binary field size is used in the operation.
COMP-5 is thus particularly useful for binary data items originating in non-COBOL
programs where the data might not conform to a COBOL PICTURE clause.
The table below shows the ranges of values possible for COMP-5 data items.
Picture Storage representation Numeric values
S9(1) through S9(4) Binary halfword (2 bytes) -32768 through +32767
S9(5) through S9(9) Binary fullword (4 bytes) -2,147,483,648 through +2,147,483,647
S9(10) through
S9(18)
Binary doubleword (8
bytes)
-9,223,372,036,854,775,808 through
+9,223,372,036,854,775,807
9(1) through 9(4) Binary halfword (2 bytes) 0 through 65535
9(5) through 9(9) Binary fullword (4 bytes) 0 through 4,294,967,295
9(10) through 9(18) Binary doubleword (8
bytes)
0 through 18,446,744,073,709,551,615
You can specify scaling (that is, decimal positions or implied integer positions) in
the PICTURE clause of COMP-5 items. If you do so, you must appropriately scale the
maximal capacities listed above. For example, a data item you describe as PICTURE
S99V99 COMP-5 is represented in storage as a binary halfword, and supports a range
of values from -327.68 through +327.67.
Regardless of the setting of the TRUNC compiler option, COMP-5 data items behave
like binary data does in programs compiled with TRUNC(BIN).
Chapter 3. Working with numbers and arithmetic 35
Byte reversal of binary data
On the workstation you sometimes need to be concerned with byte reversal. How
binary data is stored depends on your hardware and software. For example, Intel
platforms by default store binary data in little-endian format (most significant digit
at the highest address). System/390 and AIX store binary data in big-endian format
(least significant digit at the highest address).
The BINARY(NATIVE|S390) compiler option lets you specify whether the binary data
types (BINARY, COMP, and COMP-4) are to be stored in big-endian or little-endian
format.
The compiler handles COMP-5 as the native binary data format regardless of the
BINARY(NATIVE|S390) setting.
Use COMP-5 when your application interfaces with other languages (such as C or
C/C++) or other products (such as CICS or DB2) that assume native binary data
formats. However, a SORT or MERGE statement must not contain both big-endian and
little-endian binary keys. For example, if the BINARY(S390) option is in effect and a
SORT or MERGE key is a COMP-5 data item, no other SORT or MERGE key can be a COMP,
BINARY, or COMP-4 data item.
Packed-decimal (COMP-3) items
PACKED-DECIMAL and COMP-3 are synonyms on all platforms.
Packed-decimal items occupy 1 byte of storage for every two decimal digits you
code in the PICTURE description, except that the rightmost byte contains only one
digit and the sign. This format is most efficient when you code an odd number of
digits in the PICTURE description, so that the leftmost byte is fully used.
Packed-decimal items are handled as fixed-point numbers for arithmetic purposes.
Floating-point (COMP-1 and COMP-2) items
COMP-1 refers to short floating-point format and COMP-2 refers to long floating-point
format, which occupy 4 and 8 bytes of storage, respectively.
COMP-1 and COMP-2 data items are represented in IEEE format if the FLOAT(NATIVE)
compiler option is in effect.
RELATED CONCEPTS
\u201cAppendix C. Intermediate results and arithmetic precision\u201d on page 481
RELATED REFERENCE
\u201cTRUNC\u201d on page 192
\u201cBINARY\u201d on page 161
\u201cFLOAT\u201d on page 176
Examples: numeric data and internal representation
This table shows the internal representation of numeric items for native data types.
Assume that the BINARY(NATIVE), CHAR(NATIVE), and FLOAT(NATIVE) compiler
options are in effect.
36 Programming Guide
Numeric type
PICTURE and USAGE and
optional SIGN clause Value Internal representation
External
decimal
PIC S9999 DISPLAY + 1234
- 1234
1234
31 32 33 34
71 32 33 34
31 32 33 34
PIC 9999 DISPLAY 1234 31 32 33 34
PIC S9999 DISPLAY
SIGN LEADING
+ 1234
- 1234
31 32 33 34
71 32 33 34
PIC S9999 DISPLAY
SIGN LEADING SEPARATE
PIC S9999 DISPLAY
SIGN TRAILING SEPARATE
+ 1234
- 1234
+ 1234
- 1234
2B 31 32 33 34
2D 31 32 33 34
31 32 33 34 2B
31 32 33 34 2D
Binary PIC S9999 BINARY
COMP
COMP-4
+ 1234
- 1234
D2 04
2E FB
COMP-5 + 1234
- 1234
D2 04
2E FB
PIC 9999 BINARY
COMP
COMP-4
1234 D2 04
COMP-5 1234 D2 04
Internal
decimal
PIC S9999 PACKED-DECIMAL
COMP-3
+ 1234
- 1234
01 23 4C
01 23 4D
PIC 9999 PACKED-DECIMAL
COMP-3
1234 01 23 4F
Internal
floating
point
COMP-1 + 1234
- 1234
00 40 9A 44
00 40 9A C4
Internal
floating
point
COMP-2 + 1234
- 1234
00 00 00 00 00 48 93 40
00 00 00 00 00 48 93 C0
External
floating
point
PIC +9(2).9(2)E+99 DISPLAY + 1234 2B 31 32 2E 33
34 45 2B 30 32
- 1234 2D 31 32 2E 33
34 45 2B 30 32
This table shows the internal representation of numeric items for System/390 data
types. Assume that the BINARY(S390), CHAR(EBCDIC), and FLOAT(HEX) compiler
options are in effect.
Chapter 3. Working with numbers and arithmetic 37
Numeric type
PICTURE and USAGE and
optional SIGN clause Value Internal representation
External
decimal
PIC S9999 DISPLAY + 1234
- 1234
1234
F1 F2 F3 C4
F1 F2 F3 D4
F1 F2 F3 C4
PIC 9999 DISPLAY 1234 F1 F2 F3 F4
PIC S9999 DISPLAY
SIGN LEADING
+ 1234
- 1234
C1 F2 F3