FORTRAN 77 Language Reference

Arithmetic Expressions

An arithmetic expression evaluates to a single arithmetic value, and its operands have the following types. @ indicates a nonstandard feature.

The operators for an arithmetic expression are any of the following:

Table 3-1 Arithmetic Operators

Operator  

Meaning  

**

*

/

-

+

Exponentiation  

Multiplication  

Division  

Subtraction or Unary Minus  

Addition or Unary Plus  

If BYTE or LOGICAL operands are combined with arithmetic operators, they are interpreted as integer data.

Each of these operators is a binary operator in an expression of the form:


	
a
   b

where a and b are operands, and is any one of the **, *, /, -, or + operators.

Examples: Binary operators:


	A-Z 
	X*B 

The operators + and - are unary operators in an expression of the form:


	 b

where b is an operand, and is either of the - or + operators.

Examples: Unary operators:


	-Z 
	+B 

Basic Arithmetic Expressions

Each arithmetic operator is shown in its basic expression in the following table:

Table 3-2 Arithmetic Expressions

Expression  

Meaning  

a ** z

a / z

a * z

a - z

-z

a + z

+z

Raise a to the power z

Divide a by z

Multiply a by z

Subtract z from a

Negate z

Add z to a

Same as z

In the absence of parentheses, if there is more than one operator in an expression, then the operators are applied in the order of precedence. With one exception, if the operators are of equal precedence, they are applied left to right.

Table 3-3 Arithmetic Operator Precedence

Operator  

Precedence  

**

* /

+ -

  First Second Last

For the left-to-right rule, the one exception is shown by the following example:


F ** S ** Z

The above is evaluated as


F ** (S ** Z)

:

f77 allows two successive operators. @

Example: Two successive operators:


	X ** -A * Z 

The above expression is evaluated as follows:


	X ** (-(A * Z))

In the above example, the compiler starts to evaluate the **, but it needs to know what power to raise X to; so it looks at the rest of the expression and must choose between - and *. It first does the *, then the -, then the **.

Mixed Mode

If both operands have the same type, then the resulting value has that type. If operands have different types, then the weaker of two types is promoted to the stronger type, where the weaker type is the one with less precision or fewer storage units. The ranking is summarized in the following table:

Data Type  

Rank  

BYTE or LOGICAL*1  

LOGICAL*2  

LOGICAL*4  

INTEGER*2  

INTEGER*4  

INTEGER*8 

LOGICAL*8 

REAL*4 (REAL)  

REAL*8 (DOUBLE PRECISION) 

REAL*16 (QUAD PRECISION) (SPARC only)

COMPLEX*8 (COMPLEX)  

COMPLEX*16 (DOUBLE COMPLEX)  

COMPLEX*32 (QUAD COMPLEX) (SPARC only)

1 (Weakest) 

2  

3  

4  

5  

6  

7  

8  

9  

10  

11 (Strongest) 


Note -

REAL*4, INTEGER*8, and LOGICAL*8 are of the same rank, but they can be the results of different pairs of operands. For example, INTEGER*8 results if you combine INTEGER*8 and any of the types between 1-5. Likewise, REAL*4 results if one of the operands is REAL*4, and the other is any of the types between 1-5. LOGICAL*8 dictates only the 8-byte size of the result.


Example of mixed mode: If R is real, and I is integer, then the expression:


	R * I 

has the type real, because first I is promoted to real, and then the multiplication is performed.

Rules

Note these rules for the data type of an expression:

Resultant Type

For integer operands with a logical operator, the operation is done bit by bit. The result is an integer.

If the operands are mixed integer and logical, then the logicals are converted to integers, and the result is an integer.

Arithmetic Assignment

The arithmetic assignment statement assigns a value to a variable, array element, or record field. The syntax is:

v = e

e

Arithmetic expression, a character constant, or a logical expression 

v

Numeric variable, array element, or record field 

Assigning logicals to numerics is allowed, but nonstandard, and may not be portable. The resultant data type is, of course, the data type of v. @

Execution of an arithmetic assignment statement causes the evaluation of the expression e, and conversion to the type of v (if types differ), and assignment of v with the resulting value typed according to the table below.

Character constants can be assigned to variables of type integer or real. Such a constant can be a Hollerith constant or a string in apostrophes or quotes. The characters are transferred to the variables without any conversion of data. This practice is nonstandard and may not be portable. @

Type of v

Conversion of e

INTEGER*2, INTEGER*4, or INTEGER*8

REAL

REAL*8

REAL*16 (SPARC only)

DOUBLE PRECISION

COMPLEX*8

COMPLEX*16

COMPLEX*32 (SPARC only)

INT(e)

REAL(e)

DBLE(e)

QREAL(e) (SPARC only)

DBLE(e)

CMPLX(e)

DCMPLX(e)

QCMPLX(e) (SPARC only)


Note -

Compiling with any of the options -i2, -dbl, -r8, or -xtypemap will have an effect on the assumed type of e. This is discussed in Chapter 2. See also the Fortran User's Guide for a description of these options.


Example: Arithmetic assignment:


	INTEGER I2*2, J2*2, I4*4 
	LOGICAL L1, L2
	REAL R4*4, R16*16 
	DOUBLE PRECISION DP 
	COMPLEX C8, C16*16 
	J2 = 29002 
	I2 = J2 
	I4 = (I2 * 2) + 1 
	DP = 6.4D0 
	QP = 9.8Q1 
	R4 = DP 
	R16 = QP 
	C8 = R1 
	C8 = ( 3.0, 5.0 ) 
	I2 = C8 
	C16 = C8 
	C8 = L1
	R4 = L2