Fortran Beginner fortraner: messing up arithmatic

  • Thread starter Thread starter Moly
  • Start date Start date
  • Tags Tags
    Beginner
AI Thread Summary
The discussion revolves around the challenges faced while converting a MATLAB program to Fortran. The user encounters discrepancies in the output, specifically with the values of Nstep and dx. Initially, Nstep is calculated as 1999 instead of the expected 2000 due to integer truncation in Fortran, where 1 divided by 0.0005 results in a truncated integer rather than a rounded one. Similarly, the calculation of dx yields 0 because integer division is used (1 divided by 256). The user resolves these issues by changing the data types of Xstep and Nstep from integers to real numbers, which allows for accurate floating-point calculations. The discussion highlights the importance of understanding data types and division behavior in Fortran to avoid such errors in numerical computations.
Moly
Messages
20
Reaction score
0
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
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
print *, "t, x,Nstep, Xstep dx,dt",t,x,Nstep, Xstep,dx,dt
print*, "Co, Vmax", Co, Vmax
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
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?
 
Technology news on Phys.org
When converting to int numer is not rounded, but truncated, so if for a given accuracy of calculations 1/.0005=1999.9999999999999999 it gets truncated to 1999.

dx is calculated as a result of integer division - 1/256, in integers it gives 0. My memory on Fortran is fuzzy here, but obviously you have to force it to calculate float result; in C 1./Xstep will do the trick.
 
Thanks a lot Borek. I changed Xstep and Nstep from integers to real and now the calculations come out right. I am sure i will have another silly question in a few minutes and your help is muuuuuuuuuuuuuuuuuuuuuuuuuuuuch appreciated
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
8
Views
2K
Replies
17
Views
6K
Replies
4
Views
2K
Replies
8
Views
4K
Replies
4
Views
2K
Back
Top