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

In summary, if you use a different kind of precision in a subroutine from what is declared in the main program, you can get drastically different results. This could be an error in the code, or an undeclared local variable in the subroutine. You can use additional debugging statements to determine the source of the problem.
  • #1
haroonjamia
9
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
  • #2
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?
 
  • #3
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).
 

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

Why does my fortran program produce different values when used as a subroutine?

There could be several reasons for this, such as differences in the input parameters, variations in the way the program is compiled or executed, or errors in the subroutine itself. It is important to carefully check all of these factors when troubleshooting the issue.

Can the compiler affect the output of a fortran subroutine?

Yes, the compiler can have a significant impact on the output of a fortran subroutine. Different compilers may interpret the code differently or optimize it in different ways, leading to variations in the output. It is recommended to use the same compiler for both the main program and the subroutine to ensure consistency.

How can I ensure that my fortran subroutine always produces the same output?

One way to ensure consistent output is to carefully control the input parameters and ensure that they are identical each time the subroutine is called. Additionally, using a stable and reliable compiler can help minimize variations in the output.

Are there any common programming mistakes that can cause differences in the output of a fortran subroutine?

Yes, there are several common mistakes that can lead to variations in the output of a fortran subroutine. These include errors in variable declarations, incorrect use of array indices, and uninitialized variables. Carefully reviewing the subroutine code for these types of mistakes can help identify and resolve the issue.

Is there a way to debug a fortran subroutine to identify the source of the output differences?

Yes, there are various debugging tools and techniques that can be used to identify the source of output differences in a fortran subroutine. These include using print statements to track the values of variables, stepping through the code with a debugger, and using a profiler to analyze the performance of the subroutine.

Similar threads

Replies
8
Views
4K
Replies
4
Views
2K
Replies
5
Views
3K
Replies
8
Views
1K
Replies
6
Views
3K
Replies
5
Views
2K
Replies
11
Views
1K
Replies
4
Views
3K
Back
Top