format.f
c<html>
c<head><title>format.f</title></head>
c<body>
c<pre>
program format
c
c Examples of various uses of the Format Statement, and a small
c warning about precision of constants.
c
c John Mahaffy 2/26/96
c
integer n
parameter (n=5)
real r,cv,t(n),p(n),rho(n),u(n),tc(n),tf(n)
c<a name="dp"><font color="FF0000">
double precision rd,cvd,td,pd,rhod
double precision rd1
c</font></a>
parameter (r=287.0478,cv=1004.832-r)
c
c Watch the output to see what happens when I casually move
c single precision numbers into double precision parameters
c (or constants). The computer is pretty causual about what
c it fills into the extra digits of precision.
c
parameter (rd1=r)
c
c Here I carefully define constants as DOUBLE PRECISION
c
parameter (rd=287.0478d0,cvd=1.004832d3-r)
c
c In Fortran90 I could have used a different notation in conjuction
c with KIND. Comment out old code and uncomment the following to try.
c
c integer, parameter :: r8=selected_real_kind(15,30)
c real (r8) rd,cvd,td,pd,rhod,ud
c parameter (rd=287.0478_r8, cvd=1.004832e3_r8)
c
data p,t /n*1.e5,n*300./
data pd,td/1.d5,3.d2/
open(11,file='format.out')
print *, ' Casual Definition of Double Precision Gas Constant ='
& ,rd1
print *, ' Careful Definition of Double Precision Gas Constant ='
& ,rd
c
print *
c
print 2222
2222 format ('Look what I''ve got to do to include a single quote')
c
print *
c
rho(1)= p(1)/(r*t(1))
rhod=pd/(rd*td)
c
c Start by writing a header
c
write(*,1000)
1000 format(14x,' Pressure',13x,'Temperature',17x,'Density')
write(*,2000) p(1),t(1),rho(1)
write(*,2001) pd,td,rhod
write(*,2002) pd,td,rhod
c
c Note how numbers in the following are right justified in the output
c
2000 format(1p,3(1x,e23.7))
2001 format(1p,3(1x,d23.15))
c
c You can get away with "e" edit descriptor on DOUBLE PRECISION
c
2002 format(1p,3(1x,e23.15))
tc(1)=t(1)-273.16
tf(1)=32.+1.8*tc(1)
u(1)=cv*t(1)
rho(1)=p(1)/(r*t(1))
do 100 i=2,n
t(i)=t(i-1)+100.
tc(i)=t(i)-273.16
tf=32.+1.8*tc(i)
p(i)=p(i-1)+1.e4
u(i)=cv*t(i)
100 rho(i)=p(i)/(r*t(i))
write(11,2020) 2005
write(11,2004)
2004 format(//,63x,'Internal',/,
& 1x,'Cell',3(5x,'Temp '), 7x,'P',8x,'Density',6x,'Energy',
& /,2x,'Num',6x,'(K)',7x,'(C)',7x,'(F)',6x,'(Pa)',6x,
& '(kg/m**3)',5x,'(J/kg)' )
write(11,2005) (i,t(i),tc(i),tf(i),p(i),rho(i),u(i),i=1,n)
2005 format(1x,i4,0p,3f10.1,1p,3e12.3)
c
c Watch What gets reused in the 2006 FORMAT
c
write(11,2020) 2006
write(11,2004)
write(11,2006) (i,t(i),tc(i),tf(i),p(i),rho(i),u(i),i=1,n)
2006 format(71('-'),/,(1x,i4,0p,3f10.1,1p,3e12.3))
c
c Watch What gets reused in the 2007 FORMAT, containing an extra
c bounding parentheses, and a repeat factor on the main interior
c block of the FORMAT
c
write(11,2020) 2007
write(11,2004)
write(11,2007) (i,t(i),tc(i),tf(i),p(i),rho(i),u(i),i=1,n)
2007 format((71('-'),/,1(1x,i4,0p,3f10.1,1p,3e12.3)))
c
c Next I get fancy with Repeat Operators and some vertical lines
c
write(11,2020) 2008
write(11,2004)
write(11,2008) (i,t(i),tc(i),tf(i),p(i),rho(i),u(i),i=1,n)
2008 format((1x,i4,0p,3(2x,'|',2x,f5.1),1p, 3(1x,'|',1x,e9.3)))
c
write(11,2020) 2009
write(11,2004)
c
c Look how the seemingly harmless drop of one pair of parentheses
c can totally wreck the pattern of reuse in the FORMAT
c
write(11,2009) (i,t(i),tc(i),tf(i),p(i),rho(i),u(i),i=1,n)
2009 format(1x,i4,0p,3(2x,'|',2x,f5.1),1p, 3(1x,'|',1x,e9.3))
c
c Try replacing the above failed format with one that produces a
c table containing both vertical and horizontal bounding lines.
c
2020 format(//,' Format Number ',i4)
stop
end
c</pre>
c</body>
c</html>