Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Begginer fortraner: stuck on arithmatic

Status
Not open for further replies.

Molymoles

Technical User
Sep 29, 2008
3
US
Hi All.
I am trying to rewrite a program that i have in matlab to one in fortran. as i chug along, i am comparing the results and i am getting funny answers. This is the program in have in fortran so far:

ccccccccccccccccccccccccccccccccccccccccccccccccc
program main
implicit none
ccccccccccccccccccccccccccccccccccccccccccccccccc
integer Nstep, Xstep
double precision x, t, dt, dx
double precision K1, K2, Vmax, KM, P, Po
double precision Co, Qo, Cop,DeltaQ
double precision A1, A2, A3
cccccccccccccccccccccccccccccccccccccccccccccccccc
t = 1.0d0
x = 0.5d0
Xstep=2**8
dx=1/Xstep
dt=0.0005d0
Nstep=t/dt
Co = 275.0d-6
Vmax = 14.50d-9
cccccccccccccccccccccccccccccccccccccccccccccccccc ccccccccccccccccc
print *, "t, x,Nstep, Xstep dx,dt",t,x,Nstep, Xstep,dx,dt
print*, "Co, Vmax", Co, Vmax
cccccccccccccccccccccccccccccccccccccccccccccccccc
return
end

the results i get when i run it with f77 f1.f (f1.f is what i called the file) is this:
t, x,Nstep, Xstep dx,dt 1. 0.5 1999 256 0. 0.0005
Co, Vmax 0.000275 1.45E-08

with the first line i have major problmes: Nstep=1/.0005 and should be 2000, why is it 1999 ............and dx=1/Xstep=0 but it should be equal to .0039

why am i getting such funny results?
 
Now I am on computer which has g77 compiler :)
So here is your corrected example
Code:
program main
implicit none
integer Nstep, Xstep
double precision x, t, dt, dx, Nstep_Double
double precision K1, K2, Vmax, KM, P, Po
double precision Co, Qo, Cop,DeltaQ
double precision A1, A2, A3
t = 1.0d0
x = 0.5d0
Xstep=2**8
! 1. says the compiler that result should by double,
! otherwise the result will be Integer, because 1 and Xstep are Integers.
dx=1./Xstep
dt=0.0005d0
! Compute result with Double Datatypes
Nstep_Double=t/dt
! Store result to Integer
Nstep = Nstep_Double 
Co = 275.0d-6
Vmax = 14.50d-9
print *, "t, x,Nstep, Xstep dx,dt",t,x,Nstep, Xstep,dx,dt
print*, "Co, Vmax", Co, Vmax
return
end
 
Works beautifully, your help is most appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top