Arithmetic Expressions [ HP COBOL II/XL Reference Manual ] MPE/iX 5.0 Documentation
HP COBOL II/XL Reference Manual
Arithmetic Expressions
Arithmetic expressions are used in the COMPUTE statement and in relation
conditions. They enable you to use exponentiation, as well as the
addition, subtraction, multiplication, division, and negation operations
that can be performed using arithmetic statements.
Arithmetic expressions allow you to combine arithmetic operations without
the restrictions on "composites of operands" and receiving data items
that exist for arithmetic statements. (See "COMPUTE Statement" in
Chapter 9 for rules concerning calculation of intermediate results.)
For machine specific limitations on the maximum number of digits in
arithmetic expressions, refer to the HP COBOL II/XL Programmer's Guide.
The number of decimal places used in evaluating an arithmetic expression
is determined by the maximum number of decimal places within the
expression and within the operand of a COMPUTE statement intended to
receive the result.
An arithmetic expression can be any of the following:
[REV BEG]
* An identifier of a numeric elementary item or COBOL function.[REV
END]
* A numeric literal.
* Identifiers and literals as described above, separated by
arithmetic operators.
* Two arithmetic expressions separated by an arithmetic operator.
* An arithmetic expression enclosed in parentheses.
Any arithmetic expression may be preceded by a unary operator.
Arithmetic Operators
There are five binary and two unary arithmetic operators. Each is
represented by a specific character or characters. When an operator is
used, it must be preceded and followed by a space.
The binary operators are listed below:
+ symbolizes addition.
- symbolizes subtraction.
* symbolizes multiplication.
/ symbolizes division.
** symbolizes exponentiation.
The following are unary operators:
+ is equivalent to multiplying by +1.
- is equivalent to multiplying by -1.
An arithmetic expression may only begin with a left parenthesis, a plus
or minus sign, or an identifier or numeric literal. It may only end with
an identifier or numeric literal, or with a right parenthesis.
Also, there must be a one-to-one correspondence between left and right
parentheses, and each left parenthesis must be to the left of its
corresponding right parenthesis.
Hierarchy of Operations
When parentheses are not used or they do not entirely enclose an
arithmetic expression, the order in which the various operands are
applied in evaluating the expression is determined in the following
manner:
1. Any unary operator (+ or-) is executed first.
2. Following the execution of a unary operator, any exponent
specified in the expression is executed.
3. Next, if multiplication or division is specified, the
multiplication or division is executed. If consecutive
multiplications and/or divisions are specified, each operation is
performed in turn, starting from the left and continuing until the
rightmost multiplication or division has been performed.
4. Following the execution of multiplication or division, any
addition or subtraction specified in the expression is performed
next. As with multiplication and division, if any consecutive
combination of these operators is used, evaluation begins with the
leftmost and terminates with the execution of the rightmost
operator in the consecutive list.
In general, when the sequence of execution of an arithmetic expression is
not specified by parentheses, the order of execution of consecutive
operations of the same hierarchical level is from left to right.
For example, the following arithmetic expression:
-5 + 3 ** 2 * 4 + 7 - 21
Is evaluated as follows:
-5 is evaluated, resulting in -5.
3 ** 2 is evaluated, resulting in 9.
9 * 4 is evaluated, resulting in 36.
-5 + 36 is evaluated, resulting in 31.
31 + 7 is evaluated, resulting in 38.
38 - 21 is evaluated, resulting in 17.
Use of Parentheses
Parentheses may be used in arithmetic expressions to specify the order in
which elements are to be evaluated. Always use parentheses in pairs.
Expressions within parentheses are evaluated first. Within nested
parentheses, evaluation begins with the innermost set of parentheses, and
continues outward until the expression contained in the outermost set is
evaluated.
Use of parentheses allows you to eliminate ambiguities in logic where
consecutive operations of the same hierarchical level appear, or to
modify the normal hierarchical sequence of execution in an arithmetic
expression.
To illustrate the use of parentheses, the following example uses the
previous arithmetic expression. The following two expressions are
equivalent:
-5 + 3 ** 2 * 4 + 7 - 21
(((-5 + ((3 ** 2) * 4)) + 7) - 21)
Both expressions result in 17. The following two expressions are also
equivalent:
-5 + 3 ** (2 * 4) + 7 - 21
-5 + 3 ** 8 + 7 - 21
Both expressions result in 6542.
Valid Combinations in Arithmetic Expressions
The ways in which operators, variables, and parentheses may be combined
in an arithmetic expression are summarized in Table 8-3 below. The
word "Variable" means a numeric literal or an identifier of a numeric
elementary item.
Table 8-3. Valid Combinations of Symbols in Arithmetic Expressions
-------------------------------------------------------------------------------------------------------
| | |
| | Second Symbol |
| First | |
| Symbol | |
| | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| | Variable | + - * / ** | Unary + or - | ( | ) |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| Variable | Not valid. | Permissible | Not valid. | Not valid. | Permissible. |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| + - * / ** | Permissible. | Not valid. | Permissible. | Permissible. | Not valid. |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| Unary + or - | Permissible. | Not valid. | Not valid. | Permissible. | Not valid. |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| ( | Permissible. | Not valid. | Permissible. | Permissible. | Not valid. |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| ) | Not valid. | Permissible. | Not valid. | Not valid. | Permissible. |
| | | | | | |
-------------------------------------------------------------------------------------------------------
Exponentiation
ANSI COBOL'85 defines the following special cases of exponentiation:
* If a value less than or equal to zero is raised to a power of
zero, the size error condition results.
* If no real number exists as the result of the evaluation, the size
error condition results.
MPE/iX 5.0 Documentation