Solving FORTRAN Strange Issue with Iteration Program

  • Context: Fortran 
  • Thread starter Thread starter jelanier
  • Start date Start date
  • Tags Tags
    Fortran Strange
Click For Summary

Discussion Overview

The discussion revolves around a FORTRAN iteration program that produces unexpected outcomes based on the structure of conditional statements. Participants explore the implications of using "goto" statements and the reasons for continuing to use FORTRAN in modern programming contexts.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Meta-discussion

Main Points Raised

  • Jim presents two versions of an iteration program, noting that the first version works as intended while the second does not, leading to confusion about the impact of conditional statements.
  • One participant points out that in the second version, modifying the value of "guess" before testing it against "target" could lead to both conditions being true, which explains the differing outcomes.
  • Jim acknowledges the oversight in not branching after the decision, expressing a feeling of embarrassment over the mistake.
  • Another participant critiques the use of "goto" statements, suggesting that they are unnecessary with modern structured control statements available in later versions of FORTRAN.
  • There is a discussion about the continued use of FORTRAN, with one participant noting its performance advantages for intensive calculations and the historical significance of established algorithms and libraries.
  • Another participant emphasizes the practical reasons for using FORTRAN, such as easier optimization and protection against memory leakage.
  • Jim shares his experience of re-learning FORTRAN and updating legacy engineering programs, highlighting the challenges of rewriting complex software in newer languages.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of "goto" statements and the reasons for using FORTRAN, indicating that multiple competing perspectives exist without a clear consensus.

Contextual Notes

The discussion touches on the limitations of the provided code examples, particularly regarding the assumptions about variable values and the implications of modifying those values within conditional statements. There is also an acknowledgment of the historical context of FORTRAN usage.

Who May Find This Useful

Individuals interested in programming, particularly those working with FORTRAN, legacy code, or iterative algorithms, may find this discussion relevant.

jelanier
Messages
67
Reaction score
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
 

Attachments

Technology news on Phys.org
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.
 
Of course you are right. I forgot to branch after the decision. I feel really stupid right now :)

Thanks,

Jim
 
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.
 
@ Mark44, another question is why people still use Fortran?
 
MathematicalPhysicist said:
@ Mark44, another question is why people still use Fortran?

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.
 
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.
 
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:

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K