I have some Fortran code that performs a simulation. The elapsed time is stored in et, and the timestep is stored in dt. Both are defined as type real. There is another real variable tot which holds the maximum time the simulation should run. i is a counting variable of type integer. My first attempt was like this:
real, intent(in) :: dt
real, intent(in) :: tot
real :: et
integer :: i
et = 0.0
i = 0
do
i = i+1
et = real(i)*dt
if (et > tot) exit
! main code here
end do
I wanted to get rid of i since it was only used in the one place, however, when I tried this, the program hangs when the total time is large:
real, intent(in) :: dt
real, intent(in) :: tot
real :: et
et = 0.0
do
et = et + dt
if (et > tot) exit
! main code here
end do
What is the difference between the two code samples that causes the program to respond so differently? My compiler is g77.
EDIT: I have added the declarations and initializations to the code samples above.
EDIT 2: The initial values passed to the subroutine are dt = 1e-6 and tot = 100.
If dt is very small in relation to tot, it might also be that at one point dt is so small, that adding it to the, by then large, et has no effect (lost in numerical precision), and thus et does not grow beyond that point…