Arithmetic Operators
Fortran has four types of operators: arithmetic,
relational,
logical,
and
character.
The following is a table of these operators, including their priority
and associativity.
Type |
Operator |
Associativity |
Arithmetic |
** |
right to left |
* |
/ |
left to right |
+ |
- |
left to right |
Relational |
< |
<= |
> |
>= |
== |
/= |
none |
Logical |
.NOT. |
right to left |
.AND. |
left to right |
.OR. |
left to right |
.EQV. |
.NEQV. |
left to right |
Some Useful Notes:
- In the table, the operator on the top-most row (**) has the
highest priority (i.e., it will be evaluated first) while
the operators on the bottom-most row (i.e.,
.EQV.
and .NEQV.)
have the lowest priority. The operators on the
same row have the same priority. In this case, the order of
evaluation is based on their associativity law.
- In addition to addition +, subtraction -,
multiplication * and division /, Fortran has an
exponential operator **. Thus, raising X to the
Y-th power is written as X**Y. For example,
the square of 5 is 5**2, and the square root of 5 is
5**0.5. The exponential operator has the highest priority.
- Operators + and - can also be used as
unary operators, meaning that they only need one operand.
For example, -A and +X. The former means change the
sign of A, while the latter is equivalent to X.
- Unary operators + and - have the same priority
as their binary counterparts (i.e., addition +
and subtraction -). As a result, since ** is higher
than the negative sign -, -3**2 is equivalent to
-(3**2), which is -9.
- For arithmetic operators, the exponential operator ** is
evaluated from right to left. Thus, A**B**C is equal to
A**(B**C) rather than (A**B)**C
Click here for single mode arithmetic
expressions
Click here for mixed mode arithmetic
expressions