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

Arithmetic Issue in FORTRAN

  1. Oct 28, 2015 #1
    Good day,
    I'm working with a FORTRAN program that has the line:
    EDEN=EDEN+(STVS**2)/(N(J)-1)

    I would like to check if N(J)-1 is equal to zero. If so make N(J)-1=1.0

    The following doesn't work:
    IF ((N(J)-1).EQ.0.0) THEN
    N(J)-1=1.0
    END IF

    I'm using gfortran and am getting unclassified statement at 1. The 1 is pointing to the beginning of the line N(J)-1=1.0
     
  2. jcsd
  3. Oct 28, 2015 #2

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    It would probably be better coding to do
    IF ((N(J)-1).EQ.0) THEN
    N(J)-1=1
    END IF

    Make sure N is declared as an INTEGER array, otherwise the compiler won't know how to handle N(J).
     
  4. Oct 28, 2015 #3

    Hesch

    User Avatar
    Gold Member

    IF ((N(J)-1).EQ.0.0) THEN
    N(J) = 2
    END IF
    EDEN=EDEN+(STVS**2)/(N(J)-1)

    I'm not familiar with FORTRAN, but I don't think you may do calculations to the left of the equality ( = ) in: N(J)-1 = 1
    That' s why you get an error.
     
  5. Oct 28, 2015 #4

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    Good catch. I missed that. :frown:
     
  6. Oct 28, 2015 #5
    Thanks for this.

    IF ((N(J)-1).EQ.0.0) THEN
    N(J) = 2
    END IF
    EDEN=EDEN+(STVS**2)/(N(J)-1)

    Worked perfect.
     
    Last edited by a moderator: Oct 28, 2015
  7. Oct 28, 2015 #6

    Hi SteamKing....I tried your solution but got the same error. However, Hesch's code worked. Thanks so much for helping.
     
  8. Oct 28, 2015 #7

    Mark44

    Staff: Mentor

    As Hesch said, you can't assign a value to an expression such as N(J) - 1.
     
  9. Oct 28, 2015 #8
    Yes, it is good to be careful with denominators. But, N(J) may be needed on its own later on and should probably be left unmodified, maybe something like this?
    Code (Fortran):

    den = N(J) - 1
    if (den .eq. 0) den = 1
    EDEN = EDEN + STVS**2/den
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Arithmetic Issue in FORTRAN
  1. FORTRAN strange issue (Replies: 7)

Loading...