Fortran 77 variable declaration issue

In summary, the code that the author was trying to update to be more rigorous caused different results with respect to the original version. It is unclear why this happened, but it seems that the change in declaration for a variable was the issue.
  • #1
AmenoParallax
11
0
Hi

I've been struggling with a fortran 77 code that I need for my astrophysics thesis... Since it's quite old, I'm trying to rewrite some features in order to make it more readable, and I'm trying to make it more rigorous by declaring all variables (I'm declaring "implicit none" everywhere), setting all real variable to double precision, and that kind of stuff. No main changes, and most importantly nothing that would alter the result of the computation.

However, something happened, and I got different results with respect to the original version. I spent hours debugging and looking for the exact place where the difference originated, and apparently the problem is due to the declaration of a variable (a double precision array) which is part of a common block.

More precisely, in the original version the variable was declared as:

IMPLICIT REAL*8 (A-H,L,M,O-Z)
COMMON /CMN1/ VA1(4),VA2(4),...

while in my updated version I changed it to:

IMPLICIT NONE
REAL*8 VA1(4),VA2(4),...
COMMON /CMN1/ VA1,VA2

Now, I wouldn't expect that this could change the calculations, but that's what happened... Anybody can explain why?

I don't think the rest of the code and the specific calculations performed are important in solving the issue, and beside that I can't post the code because it's not mine and anyway it's very long.

Any suggestion would be very appreciated. Thanks everybody!
 
Technology news on Phys.org
  • #2
That should have worked. Maybe the compiler is doing something to the common blocks (aligning, etc.) that is causing an issue. Are the common blocks declared the same everywhere (have you modified them everywhere they appear in the code)?
 
  • #3
DrClaude said:
That should have worked. Maybe the compiler is doing something to the common blocks (aligning, etc.) that is causing an issue. Are the common blocks declared the same everywhere (have you modified them everywhere they appear in the code)?
I have not modified the common blocks everywhere yet... I was trying to apply one change at a time in order to find the problematic part of the code. I'll try to uniform all the common block right away.
 
  • #4
DrClaude said:
That should have worked. Maybe the compiler is doing something to the common blocks (aligning, etc.) that is causing an issue. Are the common blocks declared the same everywhere (have you modified them everywhere they appear in the code)?
I changed the way common blocks are declared everywhere, meaning that keeping the implicit declaration of double precision variables I explicitly declared the types of all variables belonging to any common blocks everywhere in the code.

It actually seems to have worked! But still it seems quite strange that the two ways of declaring a variables (apparently equivalent) give different results...

Anyway thanks, I think this solves my problem!
 

Related to Fortran 77 variable declaration issue

1. What is the difference between implicit and explicit variable declaration in Fortran 77?

Implicit variable declaration is when variables are automatically assigned a type based on the first letter of their name, while explicit variable declaration is when the programmer explicitly specifies the type of each variable.

2. Can I mix implicit and explicit variable declaration in the same Fortran 77 program?

Yes, it is possible to mix implicit and explicit variable declaration in the same program, but it is not recommended as it can lead to confusion and errors.

3. How do I declare a variable as an array in Fortran 77?

To declare a variable as an array in Fortran 77, you need to specify the type of the array elements, followed by the variable name and the number of elements in parentheses. For example, INTEGER ARRAY(10) declares an array of 10 integers.

4. What is the purpose of the COMMON statement in Fortran 77?

The COMMON statement in Fortran 77 allows multiple program units to share the same variables. It is commonly used when working with large programs that require multiple subroutines or functions to access the same data.

5. Can I declare the same variable name with different types in different program units in Fortran 77?

No, in Fortran 77, variables with the same name must have the same type in all program units. This can lead to errors and should be avoided.

Similar threads

  • Programming and Computer Science
Replies
22
Views
341
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
634
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
9
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top