Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tricky Fortran if-statement

Status
Not open for further replies.

PJ7571

Programmer
Apr 5, 2016
4
0
0
SE
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
 
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
 
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
 
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
 
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...
 
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... :-\
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top