Single Mode Arithmetic Expressions
An arithmetic expression is an expression using additions +,
subtractions -, multiplications *, divisions /, and
exponentials **. A single mode arithmetic expression is an
expression all of whose operands are of the same type (i.e.
INTEGER, REAL or COMPLEX). However, only
INTEGER and REAL will be covered in this note. Therefore,
those values or variables in a single mode arithmetic expression are all
integers or real numbers.
In single mode arithmetic expressions, the result of an operation is
identical to that of the operands. The following is a table showing this
fact. The empty entries will be discussed in
mixed mode arithmetic expressions.
Simple Examples:
- 1 + 3 is 4
- 1.23 - 0.45 is 0.78
- 3 * 8 is 24
- 6.5/1.25 is 5.2
- 8.4/4.2 is 2.0 rather than 2, since the result
must be of REAL type.
- -5**2 is -25
- 12/4 is 3
- 13/4 is 3 rather than 3.25. Since
13/4 is a single mode arithmetic expression and
since all of its operands are of INTEGER type, the result
must also be of INTEGER type. The computer will
truncate the mathematical result (3.25) making it an integer.
Therefore, the result is 3.
- 3/5 is 0 rather than 0.6.
Rules for Evaluating Expressions
The following are rules of evaluating a more complicated single mode
arithmetic expression:
- Expressions are always evaluated from left to right
- If an operator is encountered in the process of evaluation, its
priority is compared with that
of the next one:
- if the next one is lower, evaluate the current
operator with its operands
3 * 5 - 4
In the above expression, in the left to right scan,
operator * is encountered first. Since the the
operator - is lower, 3 * 5 is evaluated
first transforming the given expression to
15 - 4. Hence, the result is 11.
- if the next one is equal to the current, the
associativity rules
are used to determine which one should be evaluated.
For example, if both the current and the next operators
are *, then 3 * 8 * 6 will be evaluated
as (3 * 8) * 6. On the other hand, if the
operator is **, A ** B ** C will be
evaluated as A ** (B ** C).
- if the next one is higher than the current, the
scan should continue with the next operator. For example,
consider the following expression:
4 + 5 * 7 ** 3
if the current operator is +,
since the next operator * has higher priority, the
scan continues to *. Once the scan arrives at
*, since the next operator ** is higher,
7 ** 3 is evaluated first, transforming the given
expression to
4 + 5 * 343
Then, the new expression is scan again. The next
operator to be evaluated is *, followed by
+. Thus, the original expression is
evaluated as 4 + (5 * (7 ** 3)).
More Complicated Examples:
In the following examples, brackets are used to indicated the order
of evaluation.
- The result is 4 rather than 4.444444 since the operands are all
integers.
2 * 4 * 5 / 3 ** 2
--> [2 * 4] * 5 / 3 ** 2
--> 8 * 5 / 3 ** 2
--> [8 * 5] / 3 ** 2
--> 40 / 3 ** 2
--> 40 / [3 ** 2]
--> 40 / 9
--> 4
- As in mathematics, subexpressions in parenthesis must be evaluated
first.
100 + (1 + 250 / 100) ** 3
--> 100 + (1 + [250 / 100]) ** 3
--> 100 + (1 + 2) ** 3
--> 100 + ([1 + 2]) ** 3
--> 100 + 3 ** 3
--> 100 + [3 ** 3]
--> 100 + 27
--> 127
- In the following example, x**0.25 is equivalent to computing
the fourth root of x. In general, taking the
k-th root of x is equivalent to
x**(1.0/k) in Fortran, where k is a real number.
1.0 + 2.0 * 3.0 / ( 6.0*6.0 + 5.0*44.0) ** 0.25
--> 1.0 + [2.0 * 3.0] / (6.0*6.0 + 5.0*44.0) ** 0.25
--> 1.0 + 6.0 / (6.0*6.0 + 5.0*55.0) ** 0.25
--> 1.0 + 6.0 / ([6.0*6.0] + 5.0*44.0) ** 0.25
--> 1.0 + 6.0 / (36.0 + 5.0*44.0) ** 0.25
--> 1.0 + 6.0 / (36.0 + [5.0*44.0]) ** 0.25
--> 1.0 + 6.0 / (36.0 + 220.0) ** 0.25
--> 1.0 + 6.0 / ([36.0 + 220.0]) ** 0.25
--> 1.0 + 6.0 / 256.0 ** 0.25
--> 1.0 + 6.0 / [256.0 ** 0.25]
--> 1.0 + 6.0 / 4.0
--> 1.0 + [6.0 / 4.0]
--> 1.0 + 1.5
--> 2.5
Click here to continue with mixed mode
arithmetic expressions.