Beginner fortraner: messing up arithmatic

  • Fortran
  • Thread starter Moly
  • Start date
  • Tags
    Beginner
In summary, the conversation discusses the process of rewriting a program from MATLAB to Fortran and the issues encountered, such as incorrect results and rounding/truncation errors. The user seeks help from an expert, who suggests changing the data types from integers to floats in order to resolve the issues. Grateful for the assistance, the user anticipates future questions and expresses appreciation for the help.
  • #1
Moly
21
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
  • #2
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.
 
  • #3
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
 

1. Why am I getting incorrect results when performing arithmetic in Fortran?

There could be several reasons for this. It could be due to incorrect syntax, using the wrong data type, or not properly initializing variables. Make sure to carefully check your code and use the appropriate data types for your calculations.

2. How do I fix a "floating point exception" error in Fortran?

This error occurs when you try to divide by zero or take the square root of a negative number. Make sure to check your calculations and avoid these operations that are mathematically undefined.

3. Why is my Fortran program giving me a "segmentation fault" error?

This error typically occurs when you try to access memory that your program is not supposed to access. Make sure to properly allocate and deallocate memory, and avoid going beyond the bounds of your arrays.

4. How can I improve the efficiency of my Fortran code?

There are several ways to optimize your code, such as avoiding unnecessary loops, using efficient algorithms, and minimizing I/O operations. It is also helpful to use compiler flags and parallel programming techniques to take advantage of multi-core processors.

5. Can I use Fortran for scientific computing on modern computers?

Yes, Fortran is still widely used in scientific computing and is well-suited for handling complex mathematical operations. It also has many modern features, such as support for object-oriented programming and parallel computing, making it a powerful tool for scientific applications.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Advanced Physics Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
17
Views
4K
  • Programming and Computer Science
Replies
1
Views
944
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
26
Views
3K
  • Programming and Computer Science
Replies
8
Views
3K
Back
Top