Numeric Literals

A numeric literal is a character string selected from the digits, the plus sign, the minus sign, and the decimal point.

The following rules govern the formation of numeric literals:

If a literal conforms to the rules for formation of a numeric literal, but is enclosed in quotation marks, it is a nonnumeric literal.

Numeric literals may also be specified using binary, octal, or hexadecimal notation. To specify a numeric literal in one of these forms, preface the number with one of the following prefixes:

Binary "B#"
Octal "O#"
Hexadecimal “X#” or “H#”

For example:

Number Binary Octal Hexadecimal
3 B#11 O#3 X#3 or H#3
8 B#1000 O#10 X#8 or H#8
12 B#1100 O#14 X#C or H#C
128 B#10000000 O#200 X#80 or H#80
255 B#11111111 O#377 X#FF or H#FF

Leading zeroes after the # are ignored. For example, X#00FF and X#FF are equivalent.

The Compiler converts each numeric literal specified in this way to an unsigned long integer. In most cases, this is a 32-bit unsigned number, so the maximum value of a numeric literal that can specified with this notation is 4294967295, or (2**32) -1.

LENGTH OF expression

The LENGTH OF expression can be used anywhere you would use a numeric literal, except as a subscript or reference modifier. The Compiler treats this expression as if you have coded a numeric literal. The LENGTH OF expression is written as follows:

LENGTH OF data-name

data-name can be a numeric or nonnumeric literal or the name of a data item of any type. Data-name may include subscripts if it refers to a table item. The Compiler calculates the value of the LENGTH OF expression and replaces it with a numeric literal equivalent to the current number of bytes of storage used by the data item or literal referenced in LENGTH OF. For example:

77 my-item PIC x(10).
78 my-item-length value LENGTH OF my-item.

becomes:

77 my-item PIC x(10).
78 my-item-length value 10.

The LENGTH OF expression can also be used in the procedure division as demonstrated in this example:

01 my-data.
   03 my-table occurs 20 times.
      05 my-element-1 pic x(10).
   05 my-element-2 pic 99.

MOVE LENGTH OF my-element-1 TO data-size.
MOVE LENGTH OF my-table TO data-size.
MOVE LENGTH OF my-table(1) TO data-size.

In this example the Compiler treats the first MOVE as MOVE 10 TO data-size, the second MOVE as MOVE 240 TO data-size, and the third MOVE as MOVE 12 TO data-size.

Note: This expression (when used on a table) works differently in ACUCOBOL-GT than in other COBOL Compilers, such as IBM Enterprise COBOL. ACUCOBOL-GT returns the size of the entire table, while IBM returns the size of a single element of the table. You can use the IBM method by compiling the program with -Cv, which turns on the Compiler’s IBM compatibility mode.

Floating-Point Literals

  1. A floating-point literal has the following format:
    [ + ] k.m { E } [ + ] n
    [ - ]     { e } [ - ]

    In the above:

    • k.m represents a number with at least one digit.
    • n represents one or more digits.
    • If the functions of the decimal point and comma are switched with DECIMAL IS COMMA, then k.m will be k,m.

    Here are a few examples of floating-point numbers:

    -12.345e12
    .0123E-6
    123.E1
  2. Floating-point literals in the Procedure Division are stored internally as USAGE DOUBLE.
  3. The legal range of floating-point values is determined by the target machine. If you express a literal that is out of range for a particular machine, the runtime reports a warning message and substitutes the closest boundary value--either zero or the maximum floating-point value for the machine.
  4. On some computers, floating-point computations may give imprecise results. This is a hardware limitation; some floating point numbers cannot be precisely represented on some machines.