Documente Academic
Documente Profesional
Documente Cultură
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
Introduction
(4)
A=B*C/D
3.2
V = arithmetic expression
Arithmetic Operations
J=J+1
Y=5.3
FORTRAN Symbol
Operation
add
subtract or negate
multiply
divide
**
exponentiate
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
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.
EXAMPLE:
Precedence
highest
EXAMPLES:
lowest
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)
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.
2.
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
Mode of Arithmetic
(1)
(2)
EXAMPLES:
A / B, when B = 0
EXAMPLES:
(3)
(1)
(2)
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.
EXAMPLE:
EXAMPLES:
3.7
R = A(I) + B(J) / C
R(3) = A(I) - RAD / D(J,I)
Assignment Mode
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
Action performed
3.9
(1)
X = (A + B) * (C - D ** (E / F)
missing bracket
(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
y = a - ln[sin(b - c)]
z
+
=
(sin
Z=SQRT((SIN(THETA))**2+(COS(THETA))**2)
2
(1)
(2)
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
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
MAXVAL(X)
MINVAL(X)
MAXLOC(X)
MINLOC(X)
PRODUCT(X)
SUM(X)
LBOUND(X,I)
UBOUND(X,I)
C=A+B
C=S*B
A=1.0
OPERATION
Matrix Multiplication
Vector Multiplication
Transpose
Inspection
FUNCTION
NAME
OPERATION
7