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

In summary, the conversation discusses troubleshooting a FORTRAN program and checking if a certain expression is equal to zero. Suggestions are given on how to properly handle the expression and avoid errors. It is also mentioned to be careful with denominators and to potentially use a separate variable for calculations.
  • #1
debbieanne
12
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
  • #2
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).
 
  • #3
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.
 
  • #4
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:
 
  • #5
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:
  • #6
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.
 
  • #7
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.
 
  • #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?
Fortran:
den = N(J) - 1
if (den .eq. 0) den = 1
EDEN = EDEN + STVS**2/den
 

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

1. What is the arithmetic issue in FORTRAN?

The arithmetic issue in FORTRAN refers to a potential problem that can occur when performing calculations using the FORTRAN programming language. This issue arises due to the way FORTRAN handles integer division, which can result in unexpected and incorrect results.

2. How does FORTRAN handle integer division?

In FORTRAN, integer division always truncates the result to the nearest integer. This means that if the result of a division operation is a decimal number, it will be rounded down to the nearest whole number. This can lead to unexpected results when performing calculations involving fractions or remainders.

3. What is the solution to the arithmetic issue in FORTRAN?

The best solution to the arithmetic issue in FORTRAN is to use the correct data types when performing calculations. FORTRAN offers different data types for handling integers and real numbers, and using the appropriate data type can prevent the truncation issue. For example, using the REAL data type instead of INTEGER for division operations can ensure accurate results.

4. Can the arithmetic issue in FORTRAN be avoided?

While the arithmetic issue in FORTRAN cannot be completely avoided, it can be minimized by following good programming practices. This includes using the correct data types, avoiding unnecessary conversions between data types, and checking for potential division by zero errors.

5. Are there any other common issues in FORTRAN programming?

Yes, there are several other common issues in FORTRAN programming, such as implicit type conversion, array indexing errors, and precision errors with real numbers. It is important for FORTRAN programmers to be aware of these issues and to use proper programming techniques to avoid them.

Similar threads

  • Programming and Computer Science
Replies
4
Views
681
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
6
Views
4K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
14
Views
1K
Back
Top