Information Technology Reference
In-Depth Information
will in some languages imply
integer
division,
5
which yields
h D 0
in the present
case. It is a good habit to always convert an integer in division operations to real,
regardless of how the computer language handles the division operation.
A test program calling up the
trapezoidal
function can look like the following:
C
test function to integrate:
real
*
8 function f1 (x)
real
*
8x
f1 = exp(-x
*
x)
*
log(1+x
*
sin(x))
return
end
C
main program:
program integration
integer n
real
*
8 a, b, result
external f1
a=0
b=2
n = 1000
result = trapezoidal (a, b, f1, n)
write (
*
,
*
) result
end
Compiling, Linking, and Executing the Program
Suppose the code segments above are stored in a file
int.f
. Fortran programs must
be compiled and linked. On a Unix system this can take the form
unix> f77 -O3 -c int.f
# compilation
unix> f77 -o int int.o
# linking
The compilation step translates
int.f
into object (machine) code. The resulting file
has the name
int.o
. The linking step combines
int.o
with the standard libraries
of Fortran to form an executable program
int
.The
-O3
option means optimization
level 3 when compiling the code. What a certain optimization level means depends
on the compiler. On Linux systems the common Fortran compiler is the
g77
GNU
compiler, not
f77
. You will normally find
g77
on other Unix systems too.
To run the program, we write the name of the executable, in this case
int
.On
Unix systems you probably need to write
./int
, unless your
PATH
environment
variable contains a dot
6
(the current working directory).
5
The result of dividing the integer
p
by the integer
q
is the largest integer
r
such that
rq p
.
Note that if
q>p
,
r D 0
.
6
There is a significant security risk in having a dot in the
PATH
variable, so this is not
recommended.