Arithmetic Expressions in Fortran - Fortran

An arithmetic expression specifies a numeric computation that yields a numeric value on evaluation. The simplest form of an arithmetic expression can be:

  • an unsigned arithmetic constant
  • a symbolic name of an arithmetic constant
  • an arithmetic variable reference
  • an arithmetic array element reference
  • an arithmetic function reference

You can form more complicated arithmetic expressions from one or more operands together with arithmetic operators and parentheses.

An arithmetic element can include logical entities because logical data is treated as integer data when used in an arithmetic context. When both arithmetic and logical operands exist for a given operator, the logical operand is promoted to type INTEGER of the same byte length as the original logical length. For example, a LOGICAL*2 will be promoted to INTEGER*2 and a LOGICAL*4 will be promoted to INTEGER*4.

Arithmetic Operators
Table below shows the arithmetic operators.

Table: Arithmetic Operators

Arithmetic Operators
Use the exponentiation, division, and multiplication operators between exactly two operands. You can use the addition and subtraction operators with one or two operands; in the latter case, specify the operator before the operand; for example, –TOTAL.

Do not specify two operators in succession. (Note that the exponentiation operator consists of the two characters (**), but is a single operator.) Implied operators, as in implied multiplication, are not allowed.

Interpretation of Arithmetic Expressions
Table below showsinterprets sample arithmetic expressions.

Table : Interpretation of Arithmetic Expressions

Interpretation of Arithmetic Expressions
An arithmetic expression containing two or more operators is interpreted based on a precedence relation among the arithmetic operators. This precedence, from highest to lowest, is

  • ( )
  • **
  • * and /
  • + and –

Use parentheses to override the order of precedence.
The following is an example of an arithmetic expression:


The operators are executed in the following sequence:

  1. C**D is evaluated first.
  2. A/B is evaluated next.
  3. The result of C**D is subtracted from the result of A/B to give the final result.

A unary operator (–) can follow another operator. Specifying the unary operator after the exponentiation operator produces a variation on the standard order of operations. The unary operator is evaluated first in that case, resulting in exponentiation taking a lower precedence in the expression.

For example, the following expression

A ** - B * C
is interpreted as
A ** ( - B * C )

Arithmetic Operands
Arithmetic operands must specify values with integer, real, double-precision, complex, or double-complex data types. You can combine specific operands in an arithmetic expression. The arithmetic operands, in order of increasing complexity, are

  • primary
  • factor
  • term
  • arithmetic expression

A primary is the basic component in an arithmetic expression. The forms of a primary are

  • an unsigned arithmetic constant
  • a symbolic name of an arithmetic constant
  • an arithmetic variable reference
  • an arithmetic array element reference
  • an arithmetic function reference
  • an arithmetic expression enclosed in parentheses

A factor consists of one or more primaries separated by the exponentiation operator. The forms of a factor are

  • primary
  • primary ** factor

Factors with more than one exponentiation operator are interpreted from right to left. For example, I**J**K is interpreted as I**(J**K), and I**J**K**L is interpreted as I**(J**(K**L)).

The term incorporates the multiplicative operators into arithmetic expressions. Its forms are

  • factor
  • term/factor
  • term * factor

The above definition indicates that factors are combined from left to right in a term containing two or more multiplication or division operators.

Finally, at the highest level of the hierarchy, are the arithmetic expressions. The forms of an arithmetic expression are

  • term
  • + term
  • – term
  • arithmetic expression + term
  • arithmetic expression – term

An arithmetic expression consists of one or more terms separated by an addition operator or a subtraction operator. The terms are combined from left to right. For example, A+B-C has the same interpretation as the expression (A+B)-C. Expressions such as A*-B and A+-B are not allowed. The correct forms are A*(-B) and A+(-B).

An arithmetic expression can begin with a plus or minus sign.

Arithmetic Constant Expressions

An arithmetic constant expression is an arithmetic expression containing no variables. Therefore, each primary in an arithmetic constant expression must be one of the following:

  • arithmetic constant
  • symbolic name of an arithmetic constant
  • arithmetic constant expression enclosed in parentheses

In an arithmetic constant expression, do not specify the exponentiation operator unless the exponent is of type integer. Variable, array element, and function references are not allowed. Examples of integer constant expressions are
x+3 (where x is the symbolic name of a constant)

Integer Constant Expressions
An integer constant expression is an arithmetic constant expression containing only integers. It can contain constants or symbolic names of constants, provided they are of type integer. As with all constant expressions, no variables, array elements, or function references are allowed.

Evaluating Arithmetic Expressions
The data type of an expression is determined by the data types of the operands and functions that are referenced. Thus, integer expressions, real expressions, double-precision expressions, complex expressions, and double expressions have values of type integer, real, double-precision, complex, and double-complex, respectively.

Single-Mode Expressions
Single-mode expressions are arithmetic expressions in which all operands have the same data type. The data type of the value of a single-mode expression is thus the same as the data type of the operands. When the addition operator or the subtraction operator is used with a single operand, the data type of the resulting expression is the same as the data type of the operand.

Mixed-Mode Expressions
Mixed-mode expressions contain operands with two or more data types.
The data type of the result of a mixed-mode expression depends on the rank associated with each data type, as shown in Table below.
Table: Data Type Ranks

Data Type Ranks

Except for exponentiation (discussed below), the result of a mixed-mode expression is assigned the data type of the highest-ranked element in the expression. The lower-ranked operand is converted to the type of the higher-ranked operand so that the operation is performed on values with equivalent data types. For example, an operation on an integer operand and a real operand produces a result of type real.

Operations that combine REAL*8 (DOUBLE PRECISION) and COMPLEX*8 (COMPLEX) are not allowed. The REAL*8 operand must be explicitly converted (for example, by using the SNGL intrinsic function).

Exponentiation is an exception to the above rules for mixed-mode expressions. When raising a value to an integer power, the integer is not converted. The result is assigned the type of the left operand.

When a complex value is raised to a complex power, the value of the
expression is defined as follows:

xy = EXP (y * LOG(x))

Integer Division
One operand of type integer can be divided by another operand of type integer. The result of an integer division operation is a value of type integer, referred to as an integer quotient. The integer quotient is obtained as follows:

  • If the magnitude of the mathematical quotient is less than one, then the integer quotient is zero. For example, the value of the expression
    (18/30) is zero.
  • If the magnitude of the mathematical quotient is greater than or equal to one, then the integer quotient is the largest integer that does not exceed the magnitude of the mathematical quotient and whose sign is the same as that of the mathematical quotient. For example, the value of the expression (–9/2) is –4.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Fortran Topics