What is the correct way to handle a potential divide by zero issue in FORTRAN?

  • Context: Fortran 
  • Thread starter Thread starter debbieanne
  • Start date Start date
  • Tags Tags
    Arithmetic Fortran
Click For Summary

Discussion Overview

The discussion revolves around handling potential divide by zero issues in FORTRAN programming, specifically in the context of modifying array elements and ensuring safe calculations. Participants explore various coding approaches and syntax issues related to conditional statements and assignments.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant suggests checking if N(J)-1 equals zero and setting it to 1.0 if true, but encounters a syntax error with their approach.
  • Another participant proposes using integer values instead of floating-point for the comparison and assignment, emphasizing the need for N to be declared as an INTEGER array.
  • Some participants express uncertainty about performing calculations on the left side of an assignment, indicating that expressions like N(J)-1 cannot be assigned values directly.
  • A different approach is suggested where a separate variable 'den' is used to store the value of N(J)-1, allowing for safer calculations without modifying N(J) directly.

Areas of Agreement / Disagreement

Participants generally agree on the importance of avoiding divide by zero errors and the need for proper syntax in FORTRAN. However, there are competing views on the best way to handle the assignment and modification of array elements, with no consensus on a single correct approach.

Contextual Notes

Some participants note limitations in their understanding of FORTRAN syntax and the implications of modifying array elements directly, which may affect the clarity of their proposed solutions.

debbieanne
Messages
12
Reaction score
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
 
Technology news on Phys.org
debbieanne said:
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

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).
 
debbieanne said:
I would like to check if N(J)-1 is equal to zero. If so make N(J)-1=1.0
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.
 
Hesch said:
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.
Good catch. I missed that. :frown:
 
SteamKing said:
Good catch. I missed that. :frown:

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:
SteamKing said:
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).
Hi SteamKing...I tried your solution but got the same error. However, Hesch's code worked. Thanks so much for helping.
 
debbieanne said:
Hi SteamKing...I tried your solution but got the same error. However, Hesch's code worked. Thanks so much for helping.
As Hesch said, you can't assign a value to an expression such as N(J) - 1.
 
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?
Fortran:
den = N(J) - 1
if (den .eq. 0) den = 1
EDEN = EDEN + STVS**2/den
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
6
Views
4K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 2 ·
Replies
2
Views
9K