Sunteți pe pagina 1din 7

Computing in FORTRAN 95

means replace the content of variable Y by the current value of array


element A(I), where I is an integer variable whose value must be
known to the computer.

Dr A.P. Watkins
Mechanical, Aerospace & Manufacturing Engineering
Department
UMIST

(3)

means replace the content of variable J with the value of J+1, i.e. add
1 to the current content of J.

CHAPTER THREE

3.1

Arithmetic Expressions and Assignment Statements

Clearly here = is not being used in the mathematical sense.

Introduction

(4)

Arithmetic statements form the core of a scientific computing


language such as FORTRAN. It is by their use that the required
values of unknowns can be found. They are the programmed
equivalent of ordinary arithmetic and algebraic equations.

A=B*C/D

means replace the current content of variable A by the value of the


RHS when evaluated.
The last two examples are instances in which arithmetic operations
are required in order to evaluate the RHS of the expressions.

An arithmetic assignment statement has the form

3.2

V = arithmetic expression

Arithmetic Operations

There are 5 arithmetic operations allowed in FORTRAN 95 as


follows:

Here V is the name of a variable or array element of type REAL,


INTEGER, DOUBLE PRECISION or COMPLEX. When the
statement is executed the arithmetic expression on the RHS is
evaluated, and this value is assigned to the variable on the LHS.
Note that the = sign does not mean equal in the mathematical
sense. Its true meaning in FORTRAN is assigned to.
EXAMPLES:
(1)

J=J+1

Y=5.3

FORTRAN Symbol

Operation

add

subtract or negate

multiply

divide

**

exponentiate

means replace the content of variable Y by the number 5.3.


Operations involve direct manipulation of numbers or indirect
manipulation with variable names.
(2)

EXAMPLES:

Y=A(I)
1

Direct

Indirect

Y=2.1+3.6

EXAMPLES:

A=2.1
B=3.6
Y=A+B

1st
2nd
4th

Z1=A+B
Z2=A-B
Z3=A/B
Z4=A*B
Z5=-A
Z6=A**2
Z7=A**2.4

(3)

R=A/B*C

Consider difference in value obtained in EXAMPLE (3) if evaluation


were from right to left.
Use of Brackets

Order of Precedence
The order of operations can be altered by the use of brackets. They
may be inserted even when not strictly required if the expression is
then made clearer to the reader.

In evaluating an expression, the following order of precedence is


used:
Operator
**
- (negation)
*,/
+,-

EXAMPLE:

Precedence
highest

A**(B**C) is preferable to A**B**C

EXAMPLES:

lowest

Operators of equal precedence are applied from left to right, except


for ** which in an expression like A**B**C is applied from right to left.
EXAMPLES:
(1)

6th

1st 2nd

3.4
3.3

3rd
5th

(1)

R = A*B/C*D

means R =

AxB

(2)

R = A*B/(C*D)

means R =

AxB

(3)

R = A*(B+C*D)

xD

CxD

R = A + B / C**F
Note implied multiplication is not allowed, must always include * sign.
1st
Use of nested brackets is allowed.

2nd
3rd

EXAMPLES:
(1)
(2)

R = A + B / C + D**E * F - G
2

R = (A + (D * (F + G)) ** E) / B

A + [ D * ( F + G )] E
B

means

R=

(2)

I = (K + N * (N - 1)) * L / M

means

[ K + N ( N 1)]L
I=
M

(3)

Error message: infinite operand


(4)

Further errors may not become apparent at all, e.g. coding the
expression R =

R1 = D * (F + G)
R2 = R1 ** E
R = (A + R2) / B
Common Errors

1.

Arithmetic expressions must not contain consecutive


operators, e.g. A * - B is not allowed. Write A * (- B) or - A * B
instead.

2.

A ** B, when A 0 and B is type REAL


Error message: real exponentiation of negative number.

However, where many brackets become nested it is preferable for


clarity and for spotting mistakes, if the expression is broken up into a
number of sub-operations, thus EXAMPLE (1) might be written

3.5

A ** B, when A = 0 and B 0

AxB
AxB
as R = A * B / C * D which means R =
xD
C
CxD

This latter example illustrates the need for care in coding and careful
checking of the code before execution. The fact that a program runs
and gives an acceptable answer leads one to think (possibly falsely)
that everything must be OK.
Always check results very carefully. If possible, run a case for which
the answer is known.
3.6

The multiplication operator must never be implied as in


algebra,
e.g. (A + B) (C + D) is invalid, must write (A + B) * (C + D)

Mode of Arithmetic

When an operation is performed on two variables or constants A and


B, where the operation is either +, -, *, / or **, the mode of arithmetic
can be either:

The above 2 errors would be spotted by the FORTRAN compiler.


Other errors would not be. Some would become apparent during
execution of the statement.

(1)

Real, when both A and B are REAL

(2)

Integer, when both A and B are INTEGER

EXAMPLES:
A / B, when B = 0

A special case occurs here. Division of one integer by another


produces an integer result by truncating the mathematical quotient,
i.e. the fractional part is discarded.

Error message: infinite operand.

EXAMPLES:

A / B, when A = 0 and B = 0 or A or B not defined

(3)

(1)

(2)

Error message: indefinite operand


3

7 / 3 = 2,

3/5=0

7 / (-4) = -1

Mixed, when one is real and the other is integer. The integer
is converted to real form, and real arithmetic is performed to
produce a real result.

The only exception is the case A ** B, where A is REAL and B is


INTEGER. B is not converted, but a real results.

The variables involved on both the LHS or RHS of the arithmetic


assignment statement can be unsubscripted or subscripted variables
(array elements).

EXAMPLE:

EXAMPLES:

3.7

X ** 2 is preferable to X ** 2.0, because the


former is evaluated as X * X, whereas the latter
2.0ln(X)
.
is evaluated as e

R = A(I) + B(J) / C
R(3) = A(I) - RAD / D(J,I)

Note mixture of unsubscripted variables and array elements.

Assignment Mode

Recall that arithmetic assignment is of the form:

In addition arithmetic operations can take place within the subscript


brackets. By definition this must be INTEGER mode arithmetic.

V = arithmetic expression.
EXAMPLES:
Suppose now that the expression on the RHS has been evaluated.
From the last section we know that it is either real or integer. How is
this value assigned to the variable V on the LHS?
Type of V
on LHS
INTEGER

Type of result on
RHS
INTEGER

INTEGER

REAL

REAL

INTEGER

REAL

REAL

R = A(I + K * L) + B(J - 3) / C(K ** 2 / J)


R(K ** 3) = B(L * I - 3,J + 5) / D(I)

Action performed

3.9

Examples of Invalid Assignment Statements

(1)

X = (A + B) * (C - D ** (E / F)

Assign integer result to V

missing bracket

Truncate result to nearest smaller


integer and assign to V
Convert result to real form and
assign to V
Assign real result to V

(2)

X = A / (B(C + D))
missing multiplication sign *

(3)

X+3=Y+Z
LHS must be a variable name only

(4)

X = Q ** - B
Two operations in sequence

3.8

(5)

X = Y = 3.0 *A

3.10

Multiple assignment not allowed


Intrinsic or Library Functions

Arithmetic Operations with Subscripted Variables

In order to simplify writing of programs involving standard


-1
mathematical functions such as sin(x), ln(x), tan (x), etc. these are
provided as library functions and are held in the computer memory.

y = a - ln[sin(b - c)]
z

Reference to a function, by its name, in a program, diverts control


from the main program to the function program. Once the value of
the function has been evaluated, control returns to the main program
at the point it left.

+
=
(sin
Z=SQRT((SIN(THETA))**2+(COS(THETA))**2)
2

The functions may be used in arithmetic assignment statements in


the same manner as real or integer constants or variables.
Appropriate arguments must be inserted in brackets after the name
of the function.

(1)

Trig functions SIN, COS and TAN require arguments in


radians. Thus if ANG is in degrees, must write:
PI=4.0*ATAN(1.0)
C=COS(ANG*PI/180.0)

(2)

Argument values must be in range of meaningful values, e.g.


Y=LOG(-2.0) would fail.

The arguments may be arbitrary arithmetic expressions, or variable


or constant names of the correct type specified in the library list
under type of argument.
The value returned by calling a function will be of type specified in the
library list under type of function.
EXAMPLES:

a+b

y = (e

+ 3)(cos(t) - t4)

FORTRAN 95 equivalent
Y=(EXP(A+B)+3)*(COS(T)-T**4)

2
x = (- b + (b - 4 a c))/2a

X=(-B+SQRT(B*B-4*A*C))/(2*A)

y = a + b/ln(c)

Y=ABS(A+B)/LOG(C)

= 4 tan (1)

PI=4.0*ATAN(1.0)

-1

Arguments of functions may themselves be functions, e.g.


Mathematical Operations
x = (integer part of d) + c

cos2)

Note

There are over 100 standard library functions. A small subset is given
at the end of this section.

Mathematical formula

Y=A-LOG(SIN(B-C))

X=ABS(INT(D)+C)
5

TASK

DEFINITION

Square root
Exponentiat
ion
Natural
logarithm
Common
log
Sine of an
angle
Cosine of
angle
Tangent of
angle
Hyperbolic
sine
Hyperbolic
cosine
Hyperbolic
tangent
Arcsine
Arccosine
Arctangent

x
exp(x)
loge(x)
ln(x)
log10(x)

FUNCTION
NAME
SQRT(X)
EXP(X)

or LOG(X)

TYPE
of
ARGUMENT
REAL
REAL

TYPE of
RESULT
REAL
REAL

REAL

REAL

TASK

DEFINITION

Absolute
value
Truncation

Nearest
LOG10(X)

REAL

REAL

sin(x)

SIN(X)

REAL

REAL

cos(x)

COS(X)

REAL

REAL

tan(x)

TAN(X)

REAL

REAL

sinh(x)

SINH(X)

REAL

REAL

Nearest

Nearest

cosh(x)

COSH(X)

REAL

REAL

Conversion
to real

tanh(x)

TANH(X)

REAL

REAL

Testing

-1

ASIN(X)
ACOS(X)
ATAN(X)

REAL
REAL
REAL

REAL
REAL
REAL

TASK

sin (x)
cos-1(x)
-1
tan (x)

Remainder

Integer
part of x
Nearest
integer to
x
Nearest
integer not
less than x
Nearest
integer not
greater
than x

DEFINITION

Remainder

FUNCTION
NAME
ABS(X)

TYPE
of TYPE of
ARGUMENT RESULT
REAL
REAL

INT(X)

REAL

INTEGER

NINT(X)

REAL

INTEGER

CEILING(X)

REAL

INTEGER

FLOOR(X)

REAL

INTEGER

REAL(I)

INTEGER

REAL

FUNCTION
NAME
MOD(X,Y)

of x/y
MODULO(X,Y)
Largest
value
Smallest
value

max(x,y,z)

MAX(X,Y,Z)

min(x,y,z)

MIN(X,Y,Z)

of TYPE of
RESULT
INTEGER INTEGER
REAL
REAL
INTEGER INTEGER
REAL
REAL
INTEGER INTEGER
REAL
REAL
INTEGER INTEGER
REAL
REAL
TYPE

ARGUMENT

MOD(X,Y)=X-Y*INT(X/Y)
MODULO(X,Y)=X-Y*FLOOR(X/Y)
They differ when negative arguments are used, e.g.
MOD(3.5, -2.0) = 1.5,
Note

Conversion

MODULO(3.5, -2.0) = -0.5.


3.11

MAXVAL(X)
MINVAL(X)
MAXLOC(X)
MINLOC(X)
PRODUCT(X)
SUM(X)
LBOUND(X,I)
UBOUND(X,I)

Intrinsic Functions Involving Arrays

Arithmetic operations can be applied to arrays as a whole, thus if A,


B and C are arrays and S is a scalar, then we can write:
(1)
(2)
(3)

C=A+B
C=S*B
A=1.0

Returns maximum value of elements


Returns minimum value of elements
Returns location of maximum value element
Returns location of minimum value element
Returns the product of all elements
Returns the sum of all elements
Returns the Ith lower bound
Returns the Ith upper bound

Other intrinsic functions available require an argument, thus, e.g.


As long as the arrays A, B and C are conformable, i.e. in these
examples have the same number of dimensions and the same lower
and upper bounds on the subscripts, then the results of these
operations are:
(1)
(2)
(3)

COUNT(X>0.0) would return the number of positive elements in X


ANY(X>0.0) would return the value .TRUE. if any element in X is
positive, otherwise .FALSE.
ALL(X>0.0) would return the value .TRUE. if all elements in X are
positive, otherwise .FALSE.

The elements of C are the sum of the corresponding


elements of A and B.
Each element of B is multiplied by the scalar S and assigned
to the corresponding element of C.
All elements of A are set to 1.0.

Additionally there are a number of intrinsic functions stored on the


computer which involve arrays.
Manipulation
FUNCTION NAME
MATMUL(X,Y)
DOT_PRODUCT(X,Y)
TRANSPOSE(X)

OPERATION
Matrix Multiplication
Vector Multiplication
Transpose

Inspection
FUNCTION
NAME

OPERATION
7

S-ar putea să vă placă și