Information Technology Reference
In-Depth Information
extent, in the direction of C CC and Java. However, Fortran 77 is normally faster
than the newer versions and therefore preferred by many computational scientists.
Fortran 77 is very well suited for implementing algorithms with CPU-intensive
loops, especially when the loops traverse array structures. Applications involving
sophisticated data structures, text processing, or large amounts of code will normally
benefit from being implemented in more modern languages such as C CC or Fortran
90/95/2000.
Algorithm 6.2 can be implemented in a Fortran 77 function as illustrated below:
real * 8 function trapezoidal (a, b, f, n)
real * 8a,b,f
external f
integer n
real * 8s,h,x
integer i
h = (b-a)/float(n)
s=0
x=a
do i = 1, n-1
x=x+h
s = s + f(x)
end do
s = 0.5 * (f(a) + f(b)) + s
trapezoidal = h * s
return
end
Fortran 77 has some limitations on the layout of the statements, which seem a bit
old-fashioned now:
-
No statement must begin before column 7.
-
There can only be one statement per line.
-
Comments start in the first column, usually with the character C , and extend to
the end of the line.
Some other rules are as follows:
- Fortran is case-insensitive, so whether you write trapezoidal or TraPEZoidal
does not matter.
- All variables, i.e., arguments and local variables, must be declared before the first
computational statement in the function.
- Real variables can be of different lengths; real * 4 denotes a four-byte single-
precision variable, and real * 8 denotes an eight-byte double-precision variable.
- For loops are implemented with the do - end do construction (and hence are
referred to as “do loops”).
-
The external keyword is used to indicate an external function to be called.
Note that we compute ba
n
by converting n to a real variable (the float function
performs this operation in Fortran). The reason is because division by an integer
Search WWH ::




Custom Search