Fortran Operators
Fortran's operators look much like mathematics. This lesson covers arithmetic, comparison and logical operators, operator precedence and the rules for mixing types.
Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
** |
power | 2**10 |
1024 |
* |
multiply | 6 * 7 |
42 |
/ |
divide | 7.0 / 2.0 |
3.5 |
+ |
add | 1 + 2 |
3 |
- |
subtract / negate | 5 - 8 |
-3 |
program arithmetic
implicit none
integer :: a = 17, b = 5
real :: x = 17.0, y = 5.0
print '(a, i0)', 'a + b = ', a + b
print '(a, i0)', 'a * b = ', a * b
print '(a, i0)', 'a / b = ', a / b ! integer division!
print '(a, i0)', 'mod = ', mod(a, b) ! remainder
print '(a, i0)', 'b ** 3 = ', b ** 3
print '(a, f0.2)', 'x / y = ', x / y
print '(a, f0.4)', 'sqrt(x) = ', sqrt(x)
end program arithmetica + b = 22
a * b = 85
a / b = 3
mod = 2
b ** 3 = 125
x / y = 3.40
sqrt(x) = 4.1231
Integer division
Dividing two integers gives an integer: the fraction is discarded (truncation towards zero). 7 / 2 is 3 and -7 / 2 is -3. If you want a real result, make at least one operand real:
program division
implicit none
integer :: correct = 7, total = 9
print *, correct / total ! 0 -- probably not what you want
print *, real(correct) / total ! convert first
print *, 100 * correct / total ! order matters: 700 / 9 = 77
print *, correct / total * 100 ! 0 * 100 = 0
print *, -7 / 2, mod(-7, 2), modulo(-7, 2)
end program division 0
0.777777791
77
0
-3 -1 1
mod(a, p) takes the sign of a, while modulo(a, p) takes the sign of p (the mathematical modulus). For cyclic indices such as wrapping around a ring, modulo is usually what you want.
Precedence
From highest to lowest: **, then * and /, then unary and binary + -, then comparisons, then .not., .and., .or., .eqv./.neqv.. Operators of equal precedence are evaluated left to right, except **, which is evaluated right to left.
program precedence
implicit none
print *, 2 + 3 * 4 ! 14
print *, (2 + 3) * 4 ! 20
print *, 2 ** 3 ** 2 ! 2 ** 9 = 512 (right to left)
print *, -2 ** 2 ! -(2**2) = -4
print *, 2.0 ** 0.5, 2 ** (-1)
end program precedence 14
20
512
-4
1.41421354 0
The last line shows another integer surprise: 2 ** (-1) is 1/2, evaluated with integers, so 0.
Mixed-mode expressions
When an operation combines different types, the "weaker" operand is converted to the stronger one: integer → real → complex, and a lower kind to a higher kind. Conversion happens operation by operation, not for the whole expression at once, so 1 / 2 * 4.0 is 0 * 4.0 = 0.0.
Comparison operators
Each comparison has a modern symbol and an older dotted form that you will still see in legacy code:
| Modern | Old | Meaning |
|---|---|---|
== |
.eq. |
equal |
/= |
.ne. |
not equal |
< <= |
.lt. .le. |
less (or equal) |
> >= |
.gt. .ge. |
greater (or equal) |
Note that "not equal" is /=, not != (the ! would start a comment).
Logical operators
program logic
implicit none
integer :: age = 34
logical :: member = .false.
logical :: adult, discount
adult = age >= 18
discount = (age < 12 .or. age >= 65) .or. member
print *, 'adult:', adult, ' discount:', discount
print *, .not. adult, adult .and. .not. member
print *, .true. .eqv. .true., .true. .neqv. .true.
end program logic adult: T discount: F
F T
T F
.eqv. is true when both sides are equal; .neqv. is an exclusive or.
Note: Fortran does not guarantee short-circuit evaluation. In
if (i > 0 .and. a(i) > 0)the compiler may evaluatea(i)even wheniis 0. Use a nestedifwhen the second test is only safe after the first one succeeds.
Useful math intrinsics
abs, sqrt, exp, log, log10, sin, cos, tan, asin, atan2(y, x), max(a, b, ...), min(...), mod, modulo, sign(a, b), hypot(x, y) and many more are built in and work for every real kind.
Test yourself
What is the value of 7 / 2 in Fortran?
- 3.5
- 3
- 4
- A compile error
Which operator means "not equal"?
!=<>/==/=
What is 2 ** 3 ** 2?
- 64
- 512
- 36
- 12