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

Click For Summary
The discussion centers on a Fortran programming issue where a subroutine for calculating the area under a function produces significantly different results compared to when it runs independently. The independent program yields an area of approximately 20 units, while the subroutine version returns a value around 10 units. The entire program is written in double precision, utilizing double kind functions for sine and cosine.Key points raised include the potential for a serious bug, possibly due to an undeclared local variable. It is recommended to use "implicit none" to help identify errors. Participants suggest verifying which of the two results is correct and emphasize that the code should be structured to function as a subroutine in both contexts. Debugging techniques such as printing input parameters and results are advised, along with ensuring all variables are explicitly declared and avoiding integer division errors. Additionally, it is suggested to pass all necessary parameters through the subroutine's arguments rather than relying on common blocks or include statements.
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).
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

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