Logical operators and comparisons
A logical value is what a condition is made of, and Fortran has five operators for them. Two of the five are unusual, and one rule is the opposite of C: both sides of .and. and .or. are always evaluated. This lesson shows the operators, proves the evaluation rule with a counter that should not move and does, and finishes with the comparison a numerical program actually needs.
The five operators
- .not. negates one logical value.
- .and. is true when both operands are true.
- .or. is true when at least one is true.
- .eqv. is true when the two operands are equal, so true with true or false with false.
- .neqv. is the negation of that, the exclusive or.
The first three need no explanation to a reader of any language. The last two are Fortran's way of writing the two-way test without a comparison: passed .eqv. expected is clearer than (passed .and. expected) .or. (.not. passed .and. .not. expected), and it is the idiom a test suite uses.
The truth tables
Written out, the five operators are small enough to keep in one table:
- .not. .true. is false and .not. .false. is true.
- .and. is true only for true with true.
- .or. is false only for false with false.
- .eqv. is true for the two equal pairs, .neqv. for the two different ones.
The precedence between them is the one from the operators lesson: relational first, then .not., then .and., then .or., then .eqv. and .neqv.. A comparison therefore needs no parentheses around it, and a mixed chain of .and. and .or. does.
No short circuit
In C the expression a && b stops at a when a is false, and the second operand never runs. Fortran does not allow that: the standard says both operands of a binary logical operator are evaluated, because either of them may be a function with a side effect and a program is entitled to that effect. The program below measures it with a counter.
The file bothsides.f90:
module side
implicit none
integer :: calls = 0
contains
logical function bump()
calls = calls + 1
bump = .true.
end function bump
end module side
program bothsides
use side, only: bump, calls
implicit none
print *, ".false. .and. bump():", .false. .and. bump()
print *, "calls :", calls
print *, ".true. .or. bump() :", .true. .or. bump()
print *, "calls :", calls
end program bothsides
gfortran -Wall -Wextra -std=f2018 bothsides.f90 -o bothsides
./bothsides
.false. .and. bump(): F
calls : 1
.true. .or. bump() : T
calls : 2
The counter went from zero to one on an .and. whose left side was already false, and from one to two on an .or. whose left side was already true. A C programmer reads that as two wasted calls; a Fortran programmer reads it as the reason the guard cannot be written in one expression. if (n > 0 .and. x(n) > 0) reads x(n) even when n is zero, so the bounds check must come first, in its own statement.
De Morgan
The two identities that rewrite a condition hold exactly, and they are the tool for turning an .or. whose parts are expensive into an .and. whose first part is cheap:
- .not. (a .and. b) is (.not. a) .or. (.not. b).
- .not. (a .or. b) is (.not. a) .and. (.not. b).
The parentheses in those lines are not decoration. .not. a .and. b means (.not. a) .and. b, because .not. binds tighter, and that is a different condition from the one a reader who meant .not. (a .and. b) has in mind.
Comparing reals
The comparison a program needs is rarely equality. It is "are these two numbers the same for my purposes", and the answer is a function with a tolerance in it. The one below scales the tolerance by the size of the operands, so it behaves the same for a value near 1 and for a value near a billion.
The file tolerance.f90:
program tolerance
use iso_fortran_env, only: dp => real64
implicit none
print *, "thirds sum to one :", nearly(1.0_dp / 3 + 1.0_dp / 3 + 1.0_dp / 3, 1.0_dp)
print *, "0.1 + 0.2 vs 0.3 :", nearly(0.1_dp + 0.2_dp, 0.3_dp)
print *, "1e-9 against zero :", nearly(1.0e-9_dp, 0.0_dp)
print *, "1e-16 against zero :", nearly(1.0e-16_dp, 0.0_dp)
print *, "0.1 + 0.2 == 0.3 :", 0.1_dp + 0.2_dp == 0.3_dp
contains
logical function nearly(a, b) result(ok)
real(dp), intent(in) :: a, b
real(dp), parameter :: tol = 1.0e-12_dp
ok = abs(a - b) <= tol * max(1.0_dp, abs(a), abs(b))
end function nearly
end program tolerance
gfortran -Wall -Wextra -std=f2018 tolerance.f90 -o tolerance
./tolerance
thirds sum to one : T
0.1 + 0.2 vs 0.3 : T
1e-9 against zero : F
1e-16 against zero : T
0.1 + 0.2 == 0.3 : F
The first line is the one that matters: three thirds add up to one within the tolerance, and the equality test in the last line says the same sum is not the constant. Between them the two lines state the whole argument. The third and fourth lines show the limit of the function: a tolerance of 1e-12 accepts an error of 1e-16 and refuses one of 1e-9, so the number is a decision about the data and not a constant to copy.
The function is a contains procedure, so it lives beside its only caller. In a real project it goes into a module of its own, which the lesson on tests does.
Exercises
- Print .true. .neqv. .true. and .true. .eqv. .true.. Which one is the exclusive or?
- Add a third call to bump in the first program and print the counter again.
- Change the tolerance to 1.0e-6 and rebuild. Which of the four lines changes?
- Write the guard for an array bound as two nested if statements and explain why one condition cannot do it.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How many operands does
.not.take?Answer
One
.not. is the only unary logical operator in Fortran.
-
Which operator is the exclusive or?
Answer
.neqv.
.neqv. is true when the operands differ, which is the exclusive or.
-
Does
.and.short-circuit in Fortran?Answer
No: both operands are always evaluated
The standard requires both sides to be evaluated, side effects included.
-
Why can
if (n > 0 .and. x(n) > 0)read out of bounds?Answer
The right operand is evaluated even when the left one is false
Without short circuit the subscript is computed whatever the first test said.
-
What is
.not. (a .and. b)equal to?Answer
(.not. a) .or. (.not. b)
That is the first De Morgan identity.
-
What does this program print?
program p implicit none print *, (2 > 1) .eqv. (1 > 2) end program pAnswer
F
One comparison is true and the other false, so the two sides differ.
-
Why is a tolerance better than equality for reals?
Answer
It states how close is close enough, instead of depending on rounding order
Equality of computed reals is a question about bits; a tolerance is about the program.
-
What does
max(1.0_dp, abs(a), abs(b))do in the tolerance function?Answer
It scales the tolerance by the larger of the two values and 1
A relative tolerance must grow with the magnitude of the operands.
-
Which operator binds tighter,
.not.or.and.?Answer
.not.
The precedence list puts .not. above .and., which is why the parentheses matter.
-
What is the result of
(2 > 1) .neqv. (2 > 1)?Answer
False
Two equal logicals make .neqv. false.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev