INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Tricky Fortran if-statement

Tricky Fortran if-statement

(OP)
Hi

I am trying to translate Fortran to C++, and I encountered a Fortran line I can't understand:

DOUBLE PRECISION FUNCTION ROUNDN (NUMBER,DEC)
DOUBLE PRECISION ARG,FACT,NUMBER,ROUND
ARG = DABS (NUMBER)
IF(DEC-1.) 1,2,2

1 <calculations on label 1>

2 <calculations on label 2 >

My problem is the IF statement. Obviously it goes to label 1 or 2 depending on the outcome of DEC-1. But how does it work? What is the logic?

It is a rounding function. I don't want to paste in the very code since it is not mine.
For example, if NUMBER is 39.99 and DEC 1.0, it goes to 2.
Thankful for any help
Perry

RE: Tricky Fortran if-statement

It is an arithmetic if statement. Goes to first label if < 0, second label if = 0, third label if > 0. Comes from the old (circa 1954) IBM instruction set. In your case it is basically

CODE

if ((DEC - 1.) < 0)
{
    calculations on label 1
}
else
{
    calculations on label 2
} 
That is provided calculations on label1 don't fall into calculations on label 2

CODE

if (DEC-1) 1, 2, 2
1    do something and fall into 2
2    do something

or

      if (DEC-1) 1, 2, 2
2     do something and fall into 1
1     do something 

RE: Tricky Fortran if-statement

(OP)
Hi xwb
Thank you for splendid answer! And no, it doesn't fall through since it does RETURN before next label.
I suspected this was REALLY old stuff... :-\
Thanks again!
Perry

RE: Tricky Fortran if-statement

Fortran, unlike C, does not have keywords. I have seen a common block, and array called if. You may come across something weird like

CODE

program main
      integer if(20)
      
      if(1) = 10
      if(2) = 20
      
      if (if(1).gt. 5) if (if(2)) 10, 20, 30
      print *, "Less than 5"
      stop
      
10    print *, "Less than 0"
      stop
20    print *, "Equal to 0"
      stop
30    print *, "Greater than 0"
      stop
      end 

RE: Tricky Fortran if-statement

(OP)
Thank you again xwb! Yes that one is even worse, really hope something like it never comes up.
Seems there is an old math functions library I have here, where we have this old stuff...

RE: Tricky Fortran if-statement

(OP)
The function finishes like this:

ROUND N = DSIGN (ROUND,NUMBER)

It should be (meaning it is interpreted as):

ROUNDN = DSIGN (ROUND,NUMBER)
No 'N' of type 'ROUND', as it seems.
Don't understand how things like this even compile... :-\

RE: Tricky Fortran if-statement

Spaces are allowed in fortran identifiers. Can lead to errors

CODE

DO 10 I = 1, 5, 1   ! start of a do loop
DO 20 J = 1, 5      ! start of a do loop
!
! Problem is the , is next to the . on the keyboard so we can get
DO 30 K = 1.5       ! assignment  DO30K = 1.5

! Sometimes you can stare at that for ages and wonder why the loop doesn't execute
! The above can be avoided if the increment is always added
DO 40 L = 1.5,1     ! compilation error
DO 50 M = 1,5.1     ! compilation error

! Alternatively, use the modern do loop
DO I = 1.5  ! Assignment
   ...
ENDDO ! this will get a compilation error - no matching DO 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close