![]() |
Department of
Mathematics and Computer Science |
|
If one uses the extension .f or .for the contents are assumed to be in "fixed source format" even though the code may, in fact, contain Fortran-90 structures/features.
f90 mpfor.f90 a.outOf course, standard Unix practice is to redirect the executable into a file other than a.out and this is done, e.g., by
f90 -o mpfor mpfor.f90In this case, typing mpfor will run the program. To redirect input and output (assuming 5 and 6 were used in READ and WRITE statements), one uses the standard Unix file redirection, namely:
mpfor < forinp > forout
c234567890...
c FILE mp1a.f
PROGRAM MP1A
C
C SAMPLE FORTRAN PROGRAM
C BY DENNIS C. SMOLARSKI, S.J.
C COMPUTE POWERS OF A COMPLEX NUMBER
C TO SHOW THE EXPONENTIATION OPERATOR IN
C FORTRAN AND TO SHOW THE VARIABLE
C TYPE 'COMPLEX'
C
COMPLEX X,OUT(10)
INTEGER I
X = (0.0,2.0)
CALL POWERS(X,OUT)
CALL PRNTOT(X,OUT)
STOP
END
C
SUBROUTINE POWERS(A,B)
COMPLEX A,B(10)
INTEGER I
DO 10 I=1,10
B(I) = A**I
10 CONTINUE
RETURN
END
C
SUBROUTINE PRNTOT(X,OUT)
COMPLEX X,OUT(10)
INTEGER J
WRITE(21,100)
100 FORMAT(1X,' THE OUTPUT IS AS FOLLOWS:')
WRITE(21,101) X
101 FORMAT(1X,' THE POWERS OF ',F9.3,' +',F9.3,' I ARE:')
WRITE(21,102)
102 FORMAT(' POWER VALUE ')
DO 10 I=1,10
WRITE(21,103) I,OUT(I)
103 FORMAT(1X,I5,3X,F9.3,' +',F9.3,' I')
10 CONTINUE
RETURN
END
! file mp1b.f90
program mp1b
! Sample program for Fortran-90
!**************************************
!*** Declaration of Variables ******
!
integer :: i
! next two lines compute the "kind" value for extended precision
integer, parameter :: dpkind = kind(0.0d0)
integer, parameter :: cmpxdpkind = kind((0.0d0,0.0d0))
complex (kind=cmpxdpkind) :: a, root
real (kind=dpkind) :: cost, sint
real (kind=dpkind), parameter :: pi = 3.141592653589793238462d0
!
!**************************************
! main part of the program -- output header
!**************************************
!
write(6,*) " Practice Machine Problem"
write(6,*) " Root Number Value"
!
! *** the following two lines compute the basic 16th root of 1
! *** See Appendix H, p A51 of Stewart's Calculus book
!
cost = cos(pi/8.0d0)
sint = sin(pi/8.0d0)
a = cmplx(cost,sint,dpkind)
!
! **** loop from 1 to 16 by increments of 1 ****
! the loop uses the value of a to compute all other 16th roots of 1
! complex values are printed as two real values in parentheses
!
root = a
do i=1,16
write(6,*) i, root
root = root*a
end do
!
! **** final section ***
! the following shows "formatted" output
!
write(6,101) pi, pi
101 format (1x, "pi = ",f5.2," or ",f20.15)
!
end program mp1
math: f90 -o mp1a mp1a.f
math: mp1a
math: more ftn21
THE OUTPUT IS AS FOLLOWS:
THE POWERS OF 0.000 + 2.000 I ARE:
POWER VALUE
1 0.000 + 2.000 I
2 -4.000 + 0.000 I
3 0.000 + -8.000 I
4 16.000 + 0.000 I
5 0.000 + 32.000 I
6 -64.000 + 0.000 I
7 0.000 + -128.000 I
8 256.000 + 0.000 I
9 0.000 + 512.000 I
10 -1024.000 + 0.000 I
math: f90 -o mp1b mp1b.f90
math: mp1b > mp1bout.out
math: more mp1bout.out
Practice Machine Problem
Root Number Value
1 (0.923879532511287,0.38268343236509)
2 (0.707106781186548,0.707106781186548)
3 (0.38268343236509,0.923879532511287)
4 (-1.110223024625157E-16,1.0)
5 (-0.38268343236509,0.923879532511287)
6 (-0.70710678118655,0.707106781186548)
7 (-0.92387953251129,0.38268343236509)
8 (-1.0,-1.665334536937735E-16)
9 (-0.92387953251129,-0.38268343236509)
10 (-0.70710678118655,-0.70710678118655)
11 (-0.38268343236509,-0.92387953251129)
12 (2.775557561562891E-16,-1.0)
13 (0.38268343236509,-0.92387953251129)
14 (0.707106781186548,-0.70710678118655)
15 (0.923879532511287,-0.38268343236509)
16 (1.0,3.885780586188048E-16)
math:
My email address is
dsmolarski@math.scu.edu
This page last updated 2 April 2002.