Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing C++ array to Fortran subroutine causes nan values to appear in result
    primarykey
    data
    text
    <p>I'm trying to use Fortran code from a C++ application. Specifically, I am trying to interface with SLATEC's <a href="http://www.netlib.org/slatec/src/drc3jj.f" rel="nofollow" title="drc3jj.f source code">drc3jj.f</a>. However, the Fortran subroutine returns an array which size depends on the parameters passed to the function. </p> <p>If the size of the array is 1, then the C++ array that I print contains the appropriate value. However, if this size is greater than one, the C++ array contains NaNs where there should be output values. </p> <p>Below is the code I use. This merely links the Fortran subroutine to the C++ application.</p> <pre><code>#ifndef FORTRANLINKAGE_H #define FORTRANLINKAGE_H extern "C" { extern void drc3jj_(double*,double*,double*,double*,double*, double*,double [],int*,int*); } #endif // FORTRANLINKAGE_H </code></pre> <p>The meat is down here. We actually call the Fortran subroutine from C++ and print the output:</p> <pre><code>#include "fortranLinkage.h" #include &lt;iostream&gt; #include &lt;stdlib.h&gt; using namespace std; void wigner3j(double l2, double l3, double m2, double m3, double coeff []) { double l1min,l1max; int ierr,size(3); drc3jj_(&amp;l2,&amp;l3,&amp;m2,&amp;m3,&amp;l1min,&amp;l1max,coeff,&amp;size,&amp;ierr); cout &lt;&lt; "Min: " &lt;&lt; l1min &lt;&lt; "\t Max: " &lt;&lt; l1max &lt;&lt; "\t Err: " &lt;&lt; ierr &lt;&lt; endl; } int main(int argc, char const *argv[]) { int l1(atoi(argv[1])),l2(atoi(argv[2])),m2(atoi(argv[3])),m3(atoi(argv[4])); double coeff [3]; wigner3j(l1,l2,m2,m3,coeff); for (int i=0;i&lt;3;i++) { cout &lt;&lt; coeff[i] &lt;&lt; endl; } return 0; } </code></pre> <p>If we call the program with <code>./myProgram 2 8 2 8</code>, it properly outputs 1/sqrt(21). However, if we try <code>./myProgram 2 8 2 7</code>, where the size of the array is actually 2, we get this result:</p> <pre><code>Min: 9 Max: 10 Err: 0 -nan -nan 2.08175e-317 </code></pre> <p>The NaNs actually have the proper sign. </p> <p>Anyway, is there another (proper) way to pass C++ arrays to Fortran? Is this even the issue? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload