Clicky

Fortran Wiki
Arithmetic

Arithmetic Operators

There are five basic arithmetic operators in Fortran, addition (+), subtraction (-), multiplication (*), division (/), and exponentiation (**). Below is an example program which illustrates some of these operators:

program main
  real, parameter :: PI = 3.1416
  real :: rad, cir, area, dia

  radius = 5.0
  dia = radius + radius     ! addition
  cir = 2.0 * PI * radius   ! multiplication
  area = PI * radius**2     ! exponentiation

  print *, 'The radius of the circle is ', rad
  print *, 'The diameter of the circle is ', dia
  print *, 'The circumference of the circle is ', cir
  print *, 'The area of the circle is ', area
end program main

Operator Precedence

The order in which a sequence of arithmetic operations occurs is called precedence. This does not necessarily occur left to right, rather, operations are carried out in the following order:

  1. Exponentiation, from right to left
  2. Multiplication and division, from left to right
  3. Addition and subtraction, from left to right

Consider the calculation of the area in the example above:

area = PI * radius**2

Here, radius**2 occurs first and the result is then multiplied by PI. The order is actually right to left in this example.

Parentheses can be used to change the order of operations.

a*b + c     ! Multiplication first by default
a*(b + c)   ! Addition first due to parenthesis

In the following example, the multiplication expression in the innermost set of parenthesis is evaluated first, followed by the exponentiation:

x = (2.0 + (PI * 5.0))**2

Using parenthesis can help make code easier to read and prevent hard-to-find operator precedence bugs. Compare the following:

a = 3.0 * x + 2.0 * y + 4.0 * z
a = (3.0 * x) + (2.0 * y) + (4.0 * z)

Mixed-Mode Expressions

Expressions involving integer and real variables should be handled with care. When two integer operands or two real operands are involved, the result will be as expected. Arithmetic involving an integer and real operand will be carried out by converting the integer operand to a real value of the same kind as the real operand. However, since it is easy to find examples where these rules become confusing, it is best to use the real or int intrinsic procedures to perform the required conversions explicitly. Consider the following examples:

5.0 * (3 / 4) = 5.0 * 0 = 5.0 * 0.0 = 0.0
(5.0 * 3) / 4 = (5.0 * 3.0) / 4 = 15.0 / 4 = 15.0 / 4.0 = 3.75
5.0 + 3 / 4 = 5.0 + 0 = 5.0 + 0. = 5.0
5 + 3.0 / 4 = 5 + 3.0 / 4.0 = 5 + 0.75 = 5.0 + 0.75 = 5.75

Precision

As with integer and real calculations, care must be taken to make sure that real expressions are evaluated in the desired precision. For example, if x and y are real variables of kind dp and you want to multiply x by a number, say 4.2, unless the constant 4.2 is explicitly made to be a constant of kind dp, the calculation will be carried out in the default precision:

program main
  implicit none

  integer, parameter :: dp = kind(0.d0)
  real(dp) :: x, y

  x = 0.5_dp

  y = 2.0 * x      ! default precision
  y = 2.0_dp * x   ! double precision
end program main