Arithmetic operators
Five binary arithmetic operators and two unary arithmetic operators can be used in arithmetic expressions. These operators are represented by specific characters that must be preceded and followed by a space.
These binary and unary arithmetic operators are shown in Table 1.
Binary operator | Meaning | Unary operator | Meaning |
---|---|---|---|
+ | Addition | + | Multiplication by +1 |
- | Subtraction | - | Multiplication by -1 |
* | Multiplication | ||
/ | Division | ||
** | Exponentiation |
Limitation: Exponents in fixed-point exponential expressions cannot contain more than nine digits. The compiler will truncate any exponent with more than nine digits. In the case of truncation, the compiler will issue a diagnostic message if the exponent is a literal or constant; if the exponent is a variable or data-name, a diagnostic message is issued at run time.
Parentheses can be used in arithmetic expressions to specify the order in which elements are to be evaluated.
Expressions within parentheses are evaluated first. When expressions are contained within nested parentheses, evaluation proceeds from the least inclusive to the most inclusive set.
When parentheses are not used, or parenthesized expressions are at the same level of inclusiveness, the following hierarchic order is implied:
- Unary operator
- Exponentiation
- Multiplication and division
- Addition and subtraction
Parentheses either eliminate ambiguities in logic where consecutive operations appear at the same hierarchic level, or modify the normal hierarchic sequence of execution when this is necessary. When the order of consecutive operations at the same hierarchic level is not completely specified by parentheses, the order is from left to right.
An arithmetic expression can begin only with a left parenthesis, a unary operator, or an operand (that is, an identifier or a literal). It can end only with a right parenthesis or an operand. An arithmetic expression must contain at least one reference to an identifier or a literal.
There must be a one-to-one correspondence between left and right parentheses in an arithmetic expression, with each left parenthesis placed to the left of its corresponding right parenthesis.
If the first operator in an arithmetic expression is a unary operator, it must be immediately preceded by a left parenthesis if that arithmetic expression immediately follows an identifier or another arithmetic expression.
The following table shows permissible arithmetic symbol pairs. An arithmetic symbol pair is the combination of two such symbols in sequence. In the table:
- Yes
- Indicates a permissible pairing.
- No
- Indicates that the pairing is not permitted.
Identifier or literal second symbol | * / ** + - second symbol | Unary + or unary - second symbol | ( second symbol | ) second symbol | |
---|---|---|---|---|---|
Identifier or literal first symbol | No | Yes | No | No | Yes |
* / ** + - first symbol |
Yes | No | Yes | Yes | No |
Unary + or unary - first symbol | Yes | No | No | Yes | No |
( first symbol |
Yes | No | Yes | Yes | No |
) first symbol |
No | Yes | No | No | Yes |