Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Beginner fortraner: messing up arithmatic

  1. Sep 29, 2008 #1
    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:

    program main
    implicit none
    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
    t = 1.0d0
    x = 0.5d0
    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

    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?
  2. jcsd
  3. Sep 29, 2008 #2


    User Avatar

    Staff: Mentor

    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.
  4. Sep 29, 2008 #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
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Beginner fortraner: messing up arithmatic
  1. Beginner Fortran Help (Replies: 4)