FORTRAN strange issue

  • Fortran
  • Thread starter jelanier
  • Start date
  • #1
43
0

Main Question or Discussion Point

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

Answers and Replies

  • #2
AlephZero
Science Advisor
Homework Helper
6,993
291
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.
 
  • #3
43
0
Of course you are right. I forgot to branch after the decision. I feel really stupid right now :)

Thanks,

Jim
 
  • #4
33,072
4,773
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.
 
  • #5
MathematicalPhysicist
Gold Member
4,162
160
@ Mark44, another question is why people still use Fortran?
 
  • #6
gabbagabbahey
Homework Helper
Gold Member
5,002
6
@ 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.
 
  • #7
1,065
53
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.
 
  • #8
43
0
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:

Related Threads for: FORTRAN strange issue

  • Last Post
Replies
5
Views
4K
  • Last Post
Replies
7
Views
639
  • Last Post
Replies
2
Views
608
Replies
3
Views
3K
Replies
5
Views
5K
Replies
2
Views
2K
  • Last Post
Replies
1
Views
2K
Replies
6
Views
3K
Top