# Arithmetic Issue in FORTRAN

1. Oct 28, 2015

### debbieanne

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. Oct 28, 2015

### SteamKing

Staff Emeritus
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).

3. Oct 28, 2015

### Hesch

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.

4. Oct 28, 2015

### SteamKing

Staff Emeritus
Good catch. I missed that.

5. Oct 28, 2015

### debbieanne

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
6. Oct 28, 2015

### debbieanne

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

7. Oct 28, 2015

### Staff: Mentor

As Hesch said, you can't assign a value to an expression such as N(J) - 1.

8. Oct 28, 2015

### gsal

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