Why fortran program produces different values, when used as a subroutine ?

Click For Summary
SUMMARY

The discussion centers on a Fortran program that produces inconsistent results when a subroutine for calculating the area under a function is integrated into a larger program. The independent execution yields an area of 20 units, while the subroutine execution results in an area of approximately 10 units. Key issues identified include the potential use of undeclared local variables and the importance of using "implicit none" to avoid bugs. Debugging strategies such as adding print statements and ensuring all variables are explicitly declared are recommended to resolve the discrepancies.

PREREQUISITES
  • Understanding of Fortran programming language
  • Familiarity with subroutines and function integration in Fortran
  • Knowledge of double precision arithmetic in Fortran
  • Experience with debugging techniques in programming
NEXT STEPS
  • Implement "implicit none" in your Fortran subroutines to catch undeclared variables
  • Learn how to use debugging statements in Fortran to trace variable values
  • Research the implications of integer division in Fortran and how to avoid it
  • Explore best practices for passing parameters to subroutines in Fortran
USEFUL FOR

This discussion is beneficial for Fortran developers, programmers working with numerical methods, and anyone troubleshooting issues related to subroutine integration and precision in computational programs.

haroonjamia
Messages
9
Reaction score
0
Hello
fortran users
I, have a problem regarding values that my program produces. Let me detailed it to you.
See, i have createrd a separate fortran program to calculete area under a given function i.e.
it perfroms integration. When this program runs independently it gives me area of the function say 20units for some limits. Now, if i use same programe in another big program which uses this as a subroutine and it calculate area of the same function between the same limits and gives area near about half (bit less or bit more) of the value it calculated independently, e.g. i can say it gives me area less than 10units.
One more thing is that, whole program is written in the Double Precession mode, and the function to be integrated contains sine and cosine function, i am using their double kind i.e. Dsin, Dcos etc.
Why i am getting this drastic change? mind it when i use it as a subroutine, i simply copy and paste it in bigger program and i do not put any extra dot to modify it?
How can copy and patse change the value ?
Plz. answer?
 
Technology news on Phys.org
haroonjamia said:
Hello
fortran users
I, have a problem regarding values that my program produces. Let me detailed it to you.
See, i have createrd a separate fortran program to calculete area under a given function i.e.
it perfroms integration. When this program runs independently it gives me area of the function say 20units for some limits. Now, if i use same programe in another big program which uses this as a subroutine and it calculate area of the same function between the same limits and gives area near about half (bit less or bit more) of the value it calculated independently, e.g. i can say it gives me area less than 10units.
One more thing is that, whole program is written in the Double Precession mode, and the function to be integrated contains sine and cosine function, i am using their double kind i.e. Dsin, Dcos etc.
Why i am getting this drastic change? mind it when i use it as a subroutine, i simply copy and paste it in bigger program and i do not put any extra dot to modify it?
How can copy and patse change the value ?
Plz. answer?

If it was just a small difference then it could just something going on with the precision (kind) used. With the huge differences you're reporting however, it sounds like a more serious bug. Usually with this type of thing it's an undeclared local variable. Are you using "implicit none" in your subroutine? If not, then do it now, it might pinpoint the error.

Anther really important question, can you verify if either of the two different results you obtain are actually the correct value for the integral?
 
Do not use the code as main in one program and as a subroutine in another...make it so that it is a subroutine in BOTH programs.

Add some debugging statements, like printing the input parameters as soon as you enter the subroutine and printing the result before you exit...maybe you are not actually getting what you think you are.

Make sure everything is declared explicitly as INTEGER or REAL as necessary.
Make sure you are not missing out by accidentally carrying out integral division between two integers, if that is not what you want.
Try to pass EVERYTHING the subroutine needs through its arguments...no other way (common block or include statement or anything like that).
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K