我正在尝试学习如何在R中使用fortran代码。我能够关注this tutorial。现在,我正在尝试将其作为基础加this fortran program to calculate pi。我使用以下代码创建了一个文件Fpi.f90:
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
if (tabControl1.SelectedTab == tabPage1)
{
//Attach only once
if (tabPage1.HasChildren)
return;
myDialog1 dg = new myDialog1();
dg.TopLevel = false;
dg.Dock = DockStyle.Fill;
dg.Parent = tabPage1;
dg.Show();
}
else if (tabControl1.SelectedTab == tabPage2)
{
if (tabPage2.HasChildren)
return;
myDialog2 dg = new myDialog2();
dg.TopLevel = false;
dg.Dock = DockStyle.Fill;
dg.Parent = tabPage2;
dg.Show();
}
else if (tabControl1.SelectedTab == tabPage3)
{
//and so on
}
}
当
subroutine pi(avepi, DARTS, ROUNDS)
double precision, intent(out) :: avepi
integer, intent(in) :: DARTS, ROUNDS
integer :: MASTER, rank, i, n
integer, allocatable :: seed(:)
double precision :: pi_est, homepi, pirecv, pisum
! we set it to zero in the sequential run
rank = 0
! initialize the random number generator
! we make sure the seed is different for each task
call random_seed()
call random_seed(size = n)
allocate(seed(n))
seed = 12 + rank*11
call random_seed(put=seed(1:n))
deallocate(seed)
avepi = 0
do i = 0, ROUNDS-1
pi_est = dboard(DARTS)
! calculate the average value of pi over all iterations
avepi = ((avepi*i) + pi_est)/(i + 1)
end do
end subroutine pi
double precision function dboard(darts)
integer, intent(in) :: darts
double precision :: x_coord, y_coord
integer :: score, n
score = 0
do n = 1, darts
call random_number(x_coord)
call random_number(y_coord)
if ((x_coord**2 + y_coord**2) <= 1.0d0) then
score = score + 1
end if
end do
dboard = 4.0d0*score/darts
end function
我做错了什么?
将$ R CMD SHLIB ./Fortran/Fpi.f90
gfortran -fpic -g -O2 -fstack-protector-strong -c Fortran/Fpi.f90 -o Fortran/Fpi.o
Fortran/Fpi.f90:22.15:
pi_est = dboard(DARTS)
1
Error: Return type mismatch of function 'dboard' at (1) (REAL(4)/REAL(8))
/usr/lib/R/etc/Makeconf:161: recipe for target 'Fortran/Fpi.o' failed
make: *** [Fortran/Fpi.o] Error 1
添加到double precision :: dboard
后,我收到了不同的错误。
pi
答案 0 :(得分:3)
您不使用implicit none
。那太糟糕了!由于隐式输入dboard
被认为是默认真实的pi
。
将其声明为双精度,或者如果可能,使用R,使用模块。接口块也可用于在dboard
内声明pi
。