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

FORTRAN strange issue

  1. Sep 27, 2012 #1
    I have a simple iteration program that does something I can't explain. It has to do with the use of if then statements. I can't see why this change would change the outcome. Here are the 2 examples:

    This works:
    if (guess.eq.target) then
    lp=3
    write (*,*) vv,lp,guess,target
    go to 20 !go to end, guess = target
    end if

    if (guess.gt.target) then
    guess = guess / vv !decrease guess
    lp=1
    else if (guess.lt.target) then
    guess = guess * vv !increase guess
    lp=2
    end if
    ******************************************************
    This doesn't work:
    if (guess.eq.target) then
    lp=3
    write (*,*) vv,lp,guess,target
    go to 20 !go to end, guess = target,
    end if

    if (guess.gt.target) then
    guess = guess / vv !decrease guess
    lp=1
    end if

    if (guess.lt.target) then
    guess = guess * vv !increase guess
    lp=2
    end if

    *************************************************
    Any ideas?

    Here are the 2 programs:attached

    Thanks,

    Jim
     

    Attached Files:

  2. jcsd
  3. Sep 27, 2012 #2

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    Your first code tests "guess > target" and "guess < target" using the same values of guess and target for both tests.

    In your second code, if guess > target, you change the value of guess, and then you test if the new (smaller) value of guess < target. So it is possible that both tests are true, and that will give different results from the first code.
     
  4. Sep 27, 2012 #3
    Of course you are right. I forgot to branch after the decision. I feel really stupid right now :)

    Thanks,

    Jim
     
  5. Sep 27, 2012 #4

    Mark44

    Staff: Mentor

    The "goto 20" statements are really questionable, IMO. Back in the old days of Fortran 4, they were probably necessary, but with all of the structured control statements that are available from F77 (or so) and later, I doubt very much that they are needed.

    This is not to say that goto statements should always be avoided (pace Edsger Dijkstra). There are good reasons to include them in some cases, but this ain't one of them.
     
  6. Sep 27, 2012 #5

    MathematicalPhysicist

    User Avatar
    Gold Member

    @ Mark44, another question is why people still use Fortran?
     
  7. Sep 27, 2012 #6

    gabbagabbahey

    User Avatar
    Homework Helper
    Gold Member

    Performance for some intensive calculations is naturally much better in Fortran than in Languages like C, since Fortran has a strict no-aliasing policy. You can write code in C and C++ that is just as optimized, but it requires very careful attention to aliasing, since restricting aliasing in C has been known to cause serious errors in otherwise valid code.
     
  8. Sep 28, 2012 #7
    I was actually not sure if MathematicalPhycisist was asking a rhetorical question...for being a MathematicalPhysicist he/she should know better.

    Fortran has been around for a while and sooo many of important algorithms, libraries have been debugged to perfection...nobody wants to translate all that software and start introducing bugs back in.

    There are several other nice reasons to use Fortran, like easier optimization, protection against memory leakage, etc.
     
  9. Sep 28, 2012 #8
    I am trying to re-learn FORTRAN. I haven't really dealt with it much since the 70s (other than SPICE models) Thanks for the help.

    Oh, a reason to use FORTRAN? Some of the best engineering programs were written in FORTRAN in the old days. I have edited a couple of them to make them work on modern 64 bit computer systems. I was certainly not going to re-write these elaborate programs in C. Updating code is much easier :)

    Jim
     
    Last edited: Sep 28, 2012
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: FORTRAN strange issue
  1. A strange fortran code (Replies: 5)

Loading...